1 高可用问题
   1.1传统方式解决高可用
  在生产环境中,Config Server与服务注册中心一样需要扩展为高可用集群。在先前实现的config-server基础上来实现高可用非常简单,不需要为这些服务端做任何额外配置,只需要遵守一个配置规则:将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server地址时,只要配置Config Server外的负载均衡即可,就像如下图所示的结构:
  
   1.2注册为服务方式
  虽然通过服务端负载均衡已经能够实现,但是作为架构内的配置管理,本身其实也可以看作架构中的一个微服务。所以,另外一种方式更为简单的方法就是把config-server也注册为服务,这样所有客户端就能以服务的方式进行访问。通过这种方法,只需要启动多个指向同一Git仓库的config-server就能实现高可用。
   2 创建注册中心(Eureka Server)
  2.1基于之前工程,创建新模块eureka-server,用作服务注册中心,选择Spring Initializr->Cloud Discovery-> Eureka Server,并添加以下依赖:
  <dependencies>    <dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-starter-eureka-server</artifactId>    </dependency>    <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-test</artifactId>       <scope>test</scope>    </dependency> </dependencies> 
  2.2 添加服务注册中心配置:
  server:   port: 8889  eureka:     instance:       hostname: localhost     client:       register-with-eureka: false # 是否注册到eureka       fetch-registry: false # 是否从eureka获取注册信息       serviceUrl:           # eureka服务器地址(注意:地址最后面的 /eureka/ 是固定值)           defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 
  2.3 启动类添加@EnableEurekaServer注解
  @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication {      public static void main(String[] args) {         SpringApplication.run(EurekaServerApplication.class, args);     } } 
   3 改造配置中心(Config Server)
  3.1 修改config-server模块,pom文件添加spring-cloud-starter-eureka依赖:
  <dependencies>    <dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-config-server</artifactId>    </dependency>    <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-test</artifactId>       <scope>test</scope>    </dependency>    <dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency> </dependencies> 
  3.2 配置文件添加服务注册中心地址:http://localhost:8889/eureka/
  server:     port: 8888  spring:     application:       name: config-server     # 配置中心     cloud:       config:         server:           git:             uri: https://github.com/laravelshao/spring-cloud-config-repo # cloud配置仓库地址             search-paths: spring-cloud-learning # 配置仓库路径             username: # 公开仓库可不填写             password: # 公开仓库可不填写         label: master # 配置仓库分支  eureka:     client:       service-url:         defaultZone: http://localhost:8889/eureka/ # 配置eureka服务器地址 
  3.3 启动类添加@EnableEurekaClient注解
  @SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigServerApplication {      public static void main(String[] args) {         SpringApplication.run(ConfigServerApplication.class, args);     } } 
   4 改造配置客户端(Config Client)
  4.1 修改config-client模块,pom文件添加spring-cloud-starter-eureka依赖:
  <dependencies>    <dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-starter-config</artifactId>    </dependency>    <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-test</artifactId>       <scope>test</scope>    </dependency>    <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency> </dependencies> 
  4.2 配置文件bootstrap.yml中添加以下配置:
     - spring.cloud.config.discovery.enabled:配置是否从配置中心读取文件
- spring.cloud.config.discovery.serviceId:配置中心服务id
- eureka.client.serviceUrl.defaultZone:配置eureka服务器地址
server:     port: 8881  spring:     application:       name: config-client     cloud:       config:         uri: http://localhost:8888/ # 配置服务中心地址         label: master # 远程仓库分支         profile: dev # 指定环境         discovery:           enabled: true # 从配置中心读取文件           service-id: config-server # 配置中心服务id eureka:   client:     serviceUrl:       defaultZone: http://localhost:8889/eureka/ # 配置eureka服务器地址 
  4.3 依次启动eureka-server、config-server、config-client模块,访问http://localhost:8889/
  
  访问http://localhost:8881/hi,结果:
  
  说明从配置中心获取到配置数据。