分布式配置中心高可用


 

传统作法

 

在之前实现的config-server基础上来实现高可用非常简单,不需要我们为这些服务端做任何额外的配置,只需要遵守一个配置规则:将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时,只要配置Config Server外的均衡负载即可,就像如下图所示的结构

 

 

注册为服务

把config-server也注册为服务,这样所有客户端就能以服务的方式进行访问。通过这种方法,只需要启动多个指向同一Git仓库位置的config-server就能实现高可用了。

 

config-server配置

pom.xml依赖

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

 

 

application.properties中配置参数eureka.client.serviceUrl.defaultZone以指定服务注册中心的位置

spring.application.name=config-server server.port=7001 # 配置服务注册中心 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
 # git仓库配置 spring.cloud.config.server.git.uri=xx spring.cloud.config.server.git.searchPaths=xx spring.cloud.config.server.git.username=username spring.cloud.config.server.git.password=password

 

 

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

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

 

启动该应用,并访问http://localhost:1111/,可以在Eureka Server的信息面板中看到config-server已经被注册了

 

config-client配置

pom.xml依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

 

bootstrap.properties中,按如下配置

spring.application.name=didispace server.port=7002 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
 spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=config-server spring.cloud.config.profile=dev

 

通过eureka.client.serviceUrl.defaultZone参数指定服务注册中心,用于服务的注册与发现

spring.cloud.config.discovery.enabled参数设置为true,开启通过服务来访问Config Server的功能

spring.cloud.config.discovery.serviceId参数来指定Config Server注册的服务名

spring.application.namespring.cloud.config.profile如之前通过URI的方式访问时候一样,用来定位Git中的资源。

 

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

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

 

 

创建的Controller来加载Git中的配置信息

@RefreshScope @RestController public class TestController { @Value("${from}") private String from; @RequestMapping("/from") public String from() { return this.from; } }

 

访问客户端应用提供的服务:http://localhost:7002/from,此时,我们会返回在Git仓库中didispace-dev.properties文件配置的from属性内容:”git-dev-1.0”

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM