博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
构建高可用的Config Server、使用Spring Cloud Bus刷新配置
阅读量:2443 次
发布时间:2019-05-10

本文共 4641 字,大约阅读时间需要 15 分钟。

构建高可用的Config Server

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

在这里插入图片描述

构建Eureka Server

依赖:

org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-web

配置:

server:  port: 8761spring:  application:    name: eureka-clienteureka:  client:    fetch-registry: false    register-with-eureka: false    service-url:       defaultZone: http://localhost:${
server.port}/eureka/

启动类:

@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args); }}

构建Config Server

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

配置:

server:  port: 8769spring:  application:    name: config-server  cloud:    config:      server:        git:          uri: https://github.com/ActonZhang1024/springcloud-config.git          search-paths: springcloud-config      label: mastereureka:  client:    service-url:       defaultZone: http://localhost:8761/eureka/

构造Config Client

org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-eureka

配置:bootstrap.yml

spring:  application:    name: config-client  cloud:    config:      label: master      name: eureka-client      profile: dev      discovery:        service-id: config-server        enabled: true

配置:application.yml

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

依次启动eureka-server、config-server 和config-client工程,注意这里需要config-server启动成功并且向eureka-server 注册完成后,才能启动config-client, 否则config-client 找不到config-server。通过控制台可以发现,config-client向地址为htp://localhost:8769的config-server读取了配置文件。访问htp://localhost:8762/foo,浏览器显示:

在这里插入图片描述

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

那么如何搭建高可用的Config Server 呢?只需要将Config Server 多实例部署,用IDEA开启多个Config Server 实例,端口分别为8769和8768。在浏览器上访问Eureka Server 的主页htp://ocalhost:8761/,界面如图

在这里插入图片描述
多次启动config-client 工程,从控制台可以发现它会轮流地从ht:///clthost:/8768 和htp://ocalhost:8769的Config Server读取配置文件,并且做了负载均衡。

在这里插入图片描述

在这里插入图片描述

使用Spring Cloud Bus刷新配置

Spring Cloud Bus是用轻量的消息代理将分布式的结点连接起来,可以用于广播配置文件的更改或者服务的监管管理。一个关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序之间相互通信。Spring Cloud Bus可选的消息代理组建包括RabbitMQ、AMQP和Kafka等。本节讲述的是用RabbitMQ作为SpringCloud的消息组件去刷新更改微服务的配置文件。

为什么需要用SpringCloudBus去刷新配置呢?

如果有几十个微服务,而每一个服务又是多实例,当更改配置时,需要重新启动多个微服务实例,会非常麻烦。Spring Cloud Bus的一个功能就是让这个过程变得简单,当远程Git仓库的配置更改后,只需要向某一个微服务实例发送一个 Post请求,通过消息组件通知其他微服务实例重新拉取配置文件。如下图所示,当远程Git 仓库的配置更改后,通过发送“/bus/refresh" Post请求给某一个微服务实例,通过消息组件,通知其他微服务实例,更新配置文件。
在这里插入图片描述
本节是在上一节的例子上进行改造的,只需要改造configclient工程。首先,需要在pom文件中引入用RabbitMQ实现的Spring Cloud Bus的起步依赖spring-loud-starter-bus-amqpo如果读者需要自己实践,则需要安装RabbitMQ服务器。pom文件添加的依赖如下:

org.springframework.cloud
spring-cloud-starter-bus-amqp

在工程的配置文件application.yml添加RabbitMQ的相关配置,host 为RabbitMQ服务器的IP地址,port 为RabbitMQ服务器的端口,username 和password为RabbitMQ服务器的用户名和密码。通过消息总线更改配置,需要经过安全验证,为了方便讲述,先把安全验证屏蔽掉,也就是将management. security. enabled改为false。 代码清单如下:

spring:  rabbitmq:    host: localhost    port: 5672    username: guest    password: guestmanagement:  security:    enabled: false

最后,需要在更新的配置类上加@ RefreshScope注解,只有加上了该注解,才会在不重启服务的情况下更新配置,如本例中更新配置文件foo变量的值。代码清单如下:

@RestController@RefreshScopepublic class Controller {
@Value("${foo}") String foo; @RequestMapping("/foo") public String hi() {
System.out.println(foo); return foo; }}

依次启动工程,其中config-client开启两个实例,端口分别为8762和8763。启动完成后,在浏览器上访问htp://ocalhost:8762/foo或者htp://calhost:8763/foo,浏览器显示:

在这里插入图片描述
更改远程Git仓库,将foo的值改为“foo version 2”。通过Postman或者其他工具发送一个Post请求htp://ocahost:8762/bus/re/resh 请求发送成功,再访问htp://ocalhost:8762/foo或者http://localhost:8763/foo,浏览器都会显示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

可见,通过向8762端口的微服务实例发送Post请求http://localhost:8762/bus/refresh,请求刷新配置,由于使用了Spring Cloud Bus,其他服务实例(如案例中的8763端口的服务实例)会接收到刷新配置的消息,也会刷新配置。

另外,“a/bus/refresh" API接口可以指定服务,即使用“destination”参数,例如“/bus/refresh?destination=eureka-client:**”,即刷新服务名为eureka-client的所有服务实例。

转载地址:http://uopqb.baihongyu.com/

你可能感兴趣的文章
人工神经网络导论_神经网络导论
查看>>
C ++ STL无序多集– std :: unordered_multiset
查看>>
深度学习导论
查看>>
go-back-n_iMyFone D-Back iPhone数据恢复
查看>>
MailboxValidator –批量电子邮件列表清理服务
查看>>
机器学习中常见的最优化算法_最常见的机器学习算法
查看>>
css图片和边框之间有间隔_CSS和CSS3之间的区别
查看>>
iphone浏览器劫持修复_修复iPhone卡在Apple徽标问题上的问题
查看>>
5个最佳Python机器学习IDE
查看>>
c++中将字符串转化为数字_在C和C ++中将十进制数转换为罗马数字
查看>>
unity 粒子系统反弹_零反弹-最佳电子邮件验证系统
查看>>
rail_deviceid_C和C ++中的Rail Fence密码程序[加密和解密]
查看>>
数字转日期 pl/sql_交换两个数字的PL / SQL程序
查看>>
stl set容器_C ++ STL设置容器– std :: set
查看>>
HTML和HTML5之间的区别
查看>>
android mvp示例_Android使用SwipeRefreshLayout示例向下拉或向下滑动以刷新
查看>>
在Android中获取当前日期的4种方法
查看>>
使用Firebase教程的Android实时聊天应用程序
查看>>
evernote 云笔记_屏幕快照之旅:Windows版Evernote 4使记笔记变得很愉快
查看>>
windows便笺_如何将便笺提醒附加到Windows和应用程序
查看>>