前言:
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用。
继续使用上一篇文章的工程,
一、创建一个eureka-server工程,用作服务注册中心。
第1步:在其pom.xml文件引入Eureka的起步依赖spring-cloud-starter-netflix- eureka-server,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.gblfy</groupId><artifactId>sc-f-chapter6</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.gblfy</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-server</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>
</project>
第2步:在配置文件application.yml上,指定服务端口为8761,加上作为服务注册中心的基本配置,代码如下:
server:port: 8761eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:application:name: eurka-server
第2步:入口类:
@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}
二、改造config-server
第1步:在其pom.xml文件加上EurekaClient的起步依赖spring-cloud-starter-netflix-eureka-client,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.gblfy</groupId><artifactId>sc-f-chapter6</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.gblfy</groupId><artifactId>config-server</artifactId><version>0.0.1-SNAPSHOT</version><name>config-server</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies>
</project>
第2步:配置文件application.yml,指定服务注册地址为http://localhost:8761/eureka/
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
spring:application:name: config-servercloud:config:server:git:uri: git@github.com:gb-heima/config-rep.gitusername: password: basedir: /Users/Administrator.PC-20180929LWLP/Desktop/springcloud-practical-column/sc-f-chapter6/config/basedir
第3步:最后需要在程序的启动类Application加上@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}}
三、改造config-client
第1步:将其注册微到服务注册中心,作为Eureka客户端,需要pom文件加上起步依赖spring-cloud-starter-netflix-eureka-client,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.gblfy</groupId><artifactId>sc-f-chapter6</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.gblfy</groupId><artifactId>config-client</artifactId><version>0.0.1-SNAPSHOT</version><name>config-client</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>
</project>
第2步:配置文件bootstrap.yml,注意是bootstrap。
加上服务注册地址为:http://localhost:8761/eureka/
spring:application:name: config-clientcloud:config:discovery:enabled: trueservice-id: CONFIG-SERVERprofile: dev
eureka:client:service-url:defaultZone: http://localhost:8761/eureka
server:port: 8881
- spring.cloud.config.discovery.enabled 是从配置中心读取文件。
- spring.cloud.config.discovery.serviceId 配置中心的servieId,即服务名。
这时发现,在读取配置文件不再写ip地址,而是服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用。
依次启动:
eureka-server,端口号为8761,
config-server的3个实例,端口号为8888,9999,0000
config-client:的2个实例,端口号为8080,8081
访问网址:http://localhost:8761/,就会出现eureka页面
注册的应用有:
1个eureka-server(默认隐藏)
3个config-server
2个config-client
共计6个
- 在config-client启动之前先把控制台清除
- 分别启动config-client的2个实例,观察控制台输出
Fetching config from server at : http://PC-20180929LWLP:8888/
Fetching config from server at : http://PC-20180929LWLP:9999/
Fetching config from server at : http://PC-20180929LWLP:0000/
以上3种场景都有可能出出现,由于client端从server端拉取信息,采用的时轮训策略
访问http://localhost:8881/hi?name=gblfy,
浏览器显示:
hi,i am from port 8080
访问http://localhost:8880/hi?name=gblfy,
浏览器显示:
hi,i am from port 8081
本文源码下载:
dev分支(最新企业实战版本):
与master代码一样只是启动的实例多几个而已,参看master即可
https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter6
master分支(入门版本):
https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter6