SpringCloud | Config(2)-Config Server高可用

271 阅读1分钟

环境

SpringCloud:Finchley.RELEASE

SpringBoot:2.0.0.RELEASE

JDK:1.8

1. Config Server高可用

借助Eureka把Config Server注册进去,形成Config Server集群,Config client通过微服务名称去请求Server,小实现高可用

1.1 Config Server改造

  1. 增加eureka client的依赖配置

            <!--Eureka client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
  2. 配置eureka和info

    #配置eureka
    eureka:
      client:
        service-url:
          defaultZone: http://server7001:7001/eureka/,http://server7002:7002/eureka/,http://server7003:7003/eureka/
      instance:
        instance-id: ${spring.cloud.client.ip-address}:${server.port}
        prefer-ip-address: true
    
    #解决eureka控制台中的Info页面错误问题
    info:
      app.name: com.xyz.microservice
      build.artifactId: $project.artifactId$ #使用maven内置变量project.artifactId和project.version完成对变量的赋值
      build.version: $project.version$
    

1.2 Config Client改造

  1. 增加eureka client依赖

            <!--eureka client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <!--eurake info页面-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  2. 配置eureka和info信息,并修改为通过微服务名称获取配置信息

    spring:
      cloud:
        config:
          name: application   #从github上获取的资源名称,没有yml后缀
          profile: test #访问的配置项
          label: master  #访问的分支
          discovery: # 开启通过微服务名称获取Config Server的信息
            enabled: true
            service-id: microserver-config
    #      uri: http://localhost:5001   #SpringCloud Config Server端的地址
    
    #开启actuator的refresh点
    management:
      endpoint:
      endpoints:
        web:
          exposure:
            include: ["refresh","info"]
    
    #配置eureka
    eureka:
      client:
        service-url:
          defaultZone: http://server7001:7001/eureka/,http://server7002:7002/eureka/,http://server7003:7003/eureka/
      instance:
        instance-id: configclient
        prefer-ip-address: true
    
    #解决eureka控制台中的Info页面错误问题
    info:
      app.name: com.xyz.microservice
      build.artifactId: $project.artifactId$ #使用maven内置变量project.artifactId和project.version完成对变量的赋值
      build.version: $project.version$
    

代码示例-github