中国领先的工业平台

返回贤集网 返回微头条
贤集网技术微头条APP获取

spring cloud config分布式配置中心的高可用问题

 山东大明消毒科技有限公司

下载贤集网APP入驻自媒体

在前面的文章中,我们实现了配置文件统一管理的功能,但是我们可以发现,我们仅仅只用了一个server,如果当这个server挂掉的话,整个配置中心就会不可用,下面,我们就来解决配置中心的高可用问题。


下面我们通过整合Eureka来实现配置中心的高可用,因为作为架构内的配置管理,本身其实也是可以看作架构中的一个微服务,我们可以把config server也注册为服务,这样所有客户端就能以服务的方式进行访问。通过这种方法,只需要启动多个指向同一Gitlab仓库位置的config server端就能实现高可用了。

一、Config Server端加入Eureka

1、加入Eureka依赖org.springframework.cloudspring-cloud-starter-eureka复制代码2、添加Eureka支持,将配置服务注册到Eureka上去

@EnableEurekaClient

3、修改Server端的配置文件

加入Eureka注册的配置,bootstrap.ymlserver:port: 8889eureka:client:service-url:defaultZone: http://localhost:8761/eurekainstance:prefer-ip-address: truespring:application:name: foocloud:config:server:git:uri: https://gitlab.xxx.com/xxxxx/xxxxx.git # 配置gitlab仓库的地址search-paths: /config-repo # gitlab仓库地址下的相对地址,可以配置多个,用,分割。username: your name # gitlab仓库的账号password: your password # gitlab仓库的密码复制代码启动Config Server

4、修改端口号,再启动一个Config Server

5、查看服务是否注册成功

二、Config 客户端加入Eureka

1、加入Eureka的pom依赖,同Config Server

2、修改Config客户端的配置文件

bootstrap.properties经过测试,此处客户端只能是bootstrap.properties,否则会一直连端口8888server.port=8890spring.application.name=configserverspring.cloud.config.name=configserverspring.cloud.config.profile=devspring.cloud.config.label=master# 开启Config服务发现支持spring.cloud.config.discovery.enabled=true# 此处需要设置成Config Server在Eureka上注册的服务名spring.cloud.config.discovery.service-id=foo# 指定服务发现中心eureka.client.service-url.defaultZone=http://localhost:8761/eureka复制代码其中,通过eureka.client.service-url.defaultZone参数指定服务注册中心,用于服务的注册与发现,再将spring.cloud.config.discovery.enabled参数设置为true,开启通过服务来访问Config Server的功能,最后利用spring.cloud.config.discovery.serviceId参数来指定Config Server注册的服务名。这里的spring.application.name和spring.cloud.config.profile和前面博客中的意义一致。

3、在Application主类上添加Eureka支持,同Config Server一致

三、验证

1、查看Eureka Server上的服务


2、查看Config Server是否正常

在浏览器中分别输入:http://localhost:8888/configserver/dev/master,http://localhost:8889/configserver/dev/master,看是否有返回结果如下:{"name": "configserver","profiles": ["dev"],"label": "master","version": "8949024814dcb6d61f97dc49db7e9dadcfc724b1","state": null,"propertySources": [{"name": "https://gitlab.xxx.com/xxxxx/xxxxx/project/config-repo/configserver.properties","source": {"name": "chhliuxyh","hello": "i'm the king of the world!!!","profile": "profile-default"}}]}复制代码3、查看客户端是否正常

在浏览器中输入http://localhost:8890/hello,看是否有如下返回i'm the king of the world!!!复制代码如果上面几步正常,则说明是ok的。

4、将8888端口对应的Config Server停掉,然后重新输入http://localhost:8890/hello,看是否ok!我们发现仍然是ok的,这样我们就完成了配置中心的高可用了!




复制代码总结

以上所述是小编给大家介绍的spring cloud config分布式配置中心的高可用实现方法,希望对大家有所帮助,

最新回复

还没有人回复哦,抢沙发吧~

发布回复

为您推荐

热门交流