一、问题分析及解决方案
1.问题分析
在 第十二章 Spring Cloud Config 统一配置中心详解-客户端动态刷新 中提到一个问题:
每个微服务如果需要加载最新配置信息,必须向每个微服务手动发送post请求,才能加载最新配置信息。因为微服务一般是集群方式,所以此种方式不方便。
2.解决方案
我们可以使用一个组件Bus消息总线,实现自动刷新。Bus消息总线支持RubbitMQ和kafak消息代理。
二、Bus消息总线
1.Bus消息总线概念
Bus消息总线概念:在一个分布式服务系统中,使用轻量级消息中间件,链接所有微服务 。
详细解释:在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。
2.Bus消息总线作用
利用bus的广播属性进行消息传播。当某个状态发生改变,通知到bus中所有的服务节点更新当前状态。一般用于配置中心。
3.Bus消息总线的工作原理
它是利用消息中间件Rubbitmq,把config server 作为生产者,把所有的微服务例如订单服务,商品服务等,作为消费者。多消费者可以对同一消息消费。远程仓储修改后,Bus会进行自动向config server 发送 POST 请求:actuator/bus-refresh,config server将Bus发送来的请求打包为生产者消息,发送到Rubbitmq。消费者(所有微服务) ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus)。当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。,会自动的拉取一下更新后的配置信息。
三、Bus消息总线使用步骤:
1.搭建并启动消息中间件环境
搭建消息中间件环境 Rubbitmq,参考:
https://blog.csdn.net/qq_41946216/article/details/127491635https://blog.csdn.net/qq_41946216/article/details/127491635注意:MQ的主机:127.0.0.0 ,端口号15672(用于web访问),端口号5672(用于java代码端访问)
用户名密码默认都是 guest
2.统一配置服务中心 config server 连接Bus组件
统一配置服务中心 config server 中引入 Bus 组件依赖,pom文件修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloudbase</artifactId>
<groupId>com.hwadee.springcloud2022</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>configServer7009</artifactId>
<dependencies>
<!-- 统一配置中心config server 端 添加Bus消息总线 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- 添加统一服务配置中心 config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--Eureka Client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
3..config server 的配置文件中配置MQ
通过bus组件连接到MQ服务,在config server的配置文件中配置mq,修改如下:
server:
port: 7009
spring:
application:
name: config-service # 为当前商品服务命名
cloud:
config:
server:
git:
# username: xiashanzhu 如果是私有库需要配置用户名和密码
# password: 123456 如果是私有库需要配置用户名和密码
uri: https://gitee.com/xiashanzhu/config-repo #要读取的远程仓库的配置文件的地址。
default-label: master # 指定分支
# 通过bus消息总线组件连接到 MQ 服务
rubbitmq: # 通过bus消息总线组件连接到 MQ 服务
host: 192.168.56.21
port: 5672
username: guest
password: guest
eureka:
client:
service-url: # 配置服务注册地址,与 eureka-server 中暴露地址保持一致
defaultZone: http://localhost:8000/eureka
instance:
prefer-ip-address: true # 是否使用 IP 地址注册,默认 false
# instance-id: product-service # 实例 id,服务的唯一标识
instance-id: ${spring.cloud.client.ip-address}:${server.port} # 如果想在控制页面看到服务地址与端口,可以将 instance-id 这样配置
lease-renewal-interval-in-seconds: 5 # 发送心跳的间隔,单位秒,默认 30
lease-expiration-duration-in-seconds: 10 # 续约到期时间,单位秒,默认90
3.所有微服务 引人 Bus 依赖
订单服务和商品服务分别引入 Bus 消息总线组件 依赖。
<!-- 消费者端 添加Bus消息总线 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
4.所有微服务 配置 Bus 组件连接 MQ
注意:所有微服务 配置 Bus 组件连接 MQ 的配置,例如商品服务和订单服务对Bus 组件连接 MQ 的配置,需要在远程仓库上修改。不是在本地的配置文件中修改。因为这些微服务启动时候首先加载 bootstrap 配置文件,通过 bootstrap 配置文件加载后拉取远端各微服务的配置。因此需要配置在远端的各个微服务的配置中。
# 通过bus消息总线组件连接到 MQ 服务
rubbitmq: # 通过bus消息总线组件连接到 MQ 服务
host: 192.168.56.21
port: 5672
username: guest
password: guest
# virtual-host: / 指定虚拟主机的根目录,如果使用虚拟主机的话。
此时注意:引入消息组件的依赖时候,只要微服务一启动,就要求立即去找本地 bootstrap 配置文件中Rabbitmq的连接信息进行连接,但此时本地配置中还没有连接信息,都在远端仓库,没有bus消息组件MQ,连接不上,因此启动微服务时候会报如下错误:
解决方案:
在本地配置文件 bootstrap 中 添加一行配置,表示在启动时候还没有拉取远端配置完成之前的失败都是允许的。
spring: cloud: config: fail-fast: true
启动测试 省略