05 配置中心的搭建

搭建Config Server

Spring Cloud的三部曲

  1. 增加依赖

  2. 启用注解@EnableXXX

  3. 配置文件

Step1: 新建Config Server微服务,添加依赖

   <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>

Step2: 将Config Server 注册到Eureka Server上

  启动类增加 @EnableEurekaClient 注解 ,作为Eureka Client 注册到注册中心上去

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }

}

  application.yml 新增配置如下

启动@EnableConfigServer,将服务变更为Config Server

  启动类增加 @EnableConfigServer注解

  application.yml 新增配置如下

spring:
    application:
       name: config
    cloud:
      config:
        server:
          git:
            uri: https://github.com/XiangYuXiHu/spring-cloud-config-center
            username: XiangYuXiHu
            password: 
            basedir: E:/cloud-config  //本地缓存的文件地址
            force-pull: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

  Cannot pull from remote the working tree is not clean. 这种报错可以通过配置强制拉属性force-pull: true 。 由于Spring Cloud配置服务器会复制远程git存储库,如果本地副本变得不干净,那么Spring Cloud配置服务器就不能更新远程存储库中的本地副本。通过设置强制拉属性为true,使Spring Cloud配置服务器从远程存储库中强制pull。

  测试:

  搭建Config Client

  Step1.添加spring-cloud-config-client依赖

  在Order工程中添加 spring-cloud-config-client 依赖

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

  Step2. 新建bootstrap.yml

spring:
  application:
    name: order
  cloud:
    config:
      profile: dev
      # 可配置多个,不推荐使用,因为需要设置具体的ip.服务端修改或者新增IP后,要同步修改
      # uri: http://localhost:9898/,http://localhost:9999/
      discovery:
        # 指定Config Server在服务发现中的service Id ,默认为configserver
        service-id: CONFIG
        # 表示使用服务发现组件中的Config Server,而不自己指定Config Server的uri,默认为false
        enabled: true

  配置信息一定要写到bootstrap.yml中。 如果配到application.yml中,spring.cloud.config.uri 就会访问默认的8888端口,而非配置的端口。

  bootstrap.yml 是被一个父级的 Spring ApplicationContext 加载的,在加载application.yml 的 ApplicationContext之前。配置在 bootstrap.yml 中的属性优先级更高,默认情况下不能被本地配置覆盖。

配置中心的高可用

Config Server注册到注册中心上的场景

  启动多个Config Server即可。 下面我们来验证下:

 

   配置中心再启动另外一个端口:

  去Eureka Server上看也可以:

  然后重启下order微服务,观察日志 中的 server url

 

Multiple Config Server Urls found listed.
Fetching config from server at : http://localhost:8083/
Located environment: name=artisan-order, profiles=[dev], label=null, version=06a183a4820a618cc1dc53b33d77e22106a9c4e4, state=null
Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/spring-cloud-config-center/order-dev.yml'}, MapPropertySource {name='https://github.com/yangshangwei/spring-cloud-config-center/order.yml'}]}

 

  再次重启下 ,或者多重启几次:

Multiple Config Server Urls found listed.
Fetching config from server at : http://localhost:8082/
Located environment: name=artisan-order, profiles=[dev], label=null, version=06a183a4820a618cc1dc53b33d77e22106a9c4e4, state=null
Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/spring-cloud-config-center/order-dev.yml'}, MapPropertySource {name='https://github.com/yangshangwei/spring-cloud-config-center/order.yml'}]}

  Config Server未注册到注册中心上的场景

  借助负载均衡设备即可 ,比如 Nginx , F5,A10等等

 

 

  

 

posted on 2019-09-25 15:12  溪水静幽  阅读(188)  评论(0)    收藏  举报