高可用配置中心

高可用配置中心

为了实现配置中心的高可用,主要有两种方式

  1. 传统模式:不需做任何额外配置,起多个配置中心服务,所有配置中心都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,客户端通过负载均衡进行调用配置中心服务,从而实现服务的高可用
006tNc79ly1fqhz6kt3joj30ry0fa3z2.jpg
  1. 服务模式:将配置中心作为为微服务注册到注册中心,通过Eureka的服务治理进行负载均衡,而且也实现了自维护,只需要启动多个指向同一Git仓库位置的config-server就能实现高可用。
1550547015343.png
Snipaste_2019-02-14_22-34-40.png

Config Server高可用 做成一个微服务,将其集群化,Config Server和Config Client向Eureka Server注册,且将Config Server多实例部署。

上手

config-server配置

pom.xml的dependencies节点中引入如下依赖,相比之前的config-server就,加入了spring-cloud-starter-eureka,用来注册服务


    
        org.springframework.cloud
        spring-cloud-config-server
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    

应用主类中,新增@EnableDiscoveryClient注解,用来将config-server注册到上面配置的服务注册中心上去。

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

在 application.yml 里新增 Eureka 的配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

这样 Server 端的改造就完成了。先启动 Eureka 注册中心,在启动 Server 端,在浏览器中访问:

006tNc79ly1fqhzv4ouhqj31kw0bkwgn.jpg

config-client配置

添加依赖


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

应用主类中,增加@EnableDiscoveryClient注解,用来发现config-server服务,利用其来加载应用配置

@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

配置文件bootstrap.yml

spring:
  cloud:
    config:
      name: config-client
      profile: dev
      label: master
      discovery:
        enabled: true
        service-id: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

主要是去掉了spring.cloud.config.uri直接指向 Server 端地址的配置,增加了最后的三个配置:

  • spring.cloud.config.discovery.enabled:开启 Config 服务发现支持
  • spring.cloud.config.discovery.serviceId:指定 Server 端的 name, 也就是 Server 端spring.application.name的值
  • eureka.client.service-url.defaultZone:指向注册中心的地址

这**三个配置文件都需要放到bootstrap.yml**的配置中。

启动 Client 端,在浏览器中访问:http://localhost:7001 就会看到 Server 端和 Client 端都已经注册了到注册中心了。

006tNc79ly1fqhzvwx9rdj31kw0dctbj.jpg

模拟生产集群环境,我们启动两个 Server 端,端口分别为 8769和 8768,提供高可用的 Server 端支持。

# 启动两个 Server
java -jar target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar --server.port=8769
java -jar target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar --server.port=8768

浏览器中访问:http://localhost:7001 就会看到有两个 Server 端同时提供配置中心的服务。

1552356999085.png

你可能感兴趣的:(高可用配置中心)