构建高可用的Config Server

        当服务实例很多时,所有的服务实例需要同时从配置中心Config-Server读取配置文件,这时可以考虑将配置中心Config Server做成一个微服务,并且将其集群化,从而达到高可用。配置中心Config Server高可用的架构图如下图所示。Config Server和Config Client向Eureka Server注册,且将Config Server多实例部署。

 

改造Config Server

        Config Server作为Eureka Client,需要在工程中的pom文件加入spring-cloud-starter-netflix-eureka-client起步依赖,代码如下:

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

        在config-server的启动类ConfigServerApplication 上加上@EnableEurekaClient注解,开启EurekaClient的功能,代码如下:

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {

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

}

在config-server工程的配置文件中怎加服务注册地址,代码如下:

server:
  port: 8769
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/yangyy/springcloudconfig.git
          username: 
          password: 
          search-paths: respo
      label: master
  application:
    name: config-server

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

改造Config Client

        和Config Server一样作为Eureka Client,在pom文件加上spring-cloud-starter-netflix-eureka-client起步依赖,在启动类上加上@EnableEurekaClient注解,开启EurekaClient的功能。

        在工程的配置文件application.yml加上相关配置,指定服务组成中心的地址为http://127.0.0.1:8761/eureka,向Service Id为config-server的配置服务读取配置文件,代码如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        service-id: config-server

  profiles:
    active: dev
server:
  port: 8768

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

        依次启动eureka-server、config-server、config-client工程。注意需要config-server启动成功并且向eureka-server注册完成后,才能启动config-client,否则config-client找不到config-server。

通过控制台可以发现,config-client向地址为http://localhost:8769的config-server读取了配置文件。访问http://localhost:8768/foo,浏览器显示:

配置文件远程读取测试:version 1

        可见,config-server从远程Git仓库读取了配置文件,config-client从config-server读取了配置文件。

猜你喜欢

转载自blog.csdn.net/u010277958/article/details/88829962