在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。 由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。
ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus)。 当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。
F:\software\RabbitMQ Server\rabbitmq_server-3.7.14\sbin
rabbitmq-plugins enable rabbitmq_management
<?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>cloud2023</artifactId>
<groupId>top.ljzstudy.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client-3366</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
server:
port: 3366
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
package top.ljzstudy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class, args);
}
}
package top.ljzstudy.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${server.port}")
private String serverPort;
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String configInfo() {
return "serverPort: " + serverPort + "\t\n\n configInfo: " + configInfo;
}
}
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
##rabbitmq相关配置,暴露bus刷新配置的端点
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh'
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
- 发送post请求刷新配置,实现一次修改,处处生效
POST http://localhost:3344/actuator/bus-refresh
HTTP/1.1 204
Date: Thu, 21 Sep 2023 02:26:34 GMT
Keep-Alive: timeout=60
Connection: keep-alive
<Response body is empty>
Response code: 204; Time: 5503ms; Content length: 0 bytes
GET http://config-3344.com:3344/master/config-dev.yml
HTTP/1.1 200
Content-Type: text/plain
Content-Length: 75
Date: Thu, 21 Sep 2023 02:29:31 GMT
Keep-Alive: timeout=60
Connection: keep-alive
config:
info: master branch,springcloud-config/config-dev.yml version=24
Response code: 200; Time: 1312ms; Content length: 75 bytes
GET http://localhost:3355/configInfo
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 58
Date: Thu, 21 Sep 2023 02:56:25 GMT
Keep-Alive: timeout=60
Connection: keep-alive
master branch,springcloud-config/config-dev.yml version=24
- 访问[http://localhost:3366/configInfo](http://localhost:3366/configInfo)
GET http://localhost:3366/configInfo
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 90
Date: Thu, 21 Sep 2023 02:55:20 GMT
Keep-Alive: timeout=60
Connection: keep-alive
serverPort: 3366
configInfo: master branch,springcloud-config/config-dev.yml version=24
[http://localhost:](http://localhost:)配置中心的端口号/actuator/bus-refresh/{destination}
GET http://config-3344.com:3344/master/config-dev.yml
HTTP/1.1 200
Content-Type: text/plain
Content-Length: 75
Date: Thu, 21 Sep 2023 03:18:22 GMT
Keep-Alive: timeout=60
Connection: keep-alive
config:
info: master branch,springcloud-config/config-dev.yml version=10
Response code: 200; Time: 1703ms; Content length: 75 bytes
POST http://localhost:3344/actuator/bus-refresh/config-client:3355
HTTP/1.1 204
Date: Thu, 21 Sep 2023 03:21:04 GMT
Keep-Alive: timeout=60
Connection: keep-alive
<Response body is empty>
Response code: 204; Time: 5392ms; Content length: 0 bytes
GET http://localhost:3355/configInfo
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 58
Date: Thu, 21 Sep 2023 03:22:04 GMT
Keep-Alive: timeout=60
Connection: keep-alive
master branch,springcloud-config/config-dev.yml version=10
Response code: 200; Time: 16ms; Content length: 58 bytes
GET http://localhost:3366/configInfo
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 90
Date: Thu, 21 Sep 2023 03:22:29 GMT
Keep-Alive: timeout=60
Connection: keep-alive
serverPort: 3366
configInfo: master branch,springcloud-config/config-dev.yml version=24
Response code: 200; Time: 16ms; Content length: 90 bytes