1、远程配置文件(默认git)变动时,提交post请求到/bus/refresh接口(SpringCloud Bus会向外提供一个http接口)
2、/bus/refresh接口收到请求后通知Config Server
3、Config Server接到通知后,从git上获取最新配置,并将接收到的请求通知Bus总线
4、Bus总线接到消息并将消息通知给连接总线的服务
5、服务收到通知,从Config Server获取最新配置信息
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.boot
spring-boot-starter-actuator
#server端服务端口
server:
port: 9901
#server端服务名称
spring:
application:
name: cloud-config-server
#cloud-config-git配置信息
cloud:
config:
server:
git:
uri: https://gitee.com/hykai/springCloudConfigTest.git
search-paths: cloud-config
username: username
password: password
#暴露/bus-refresh接口
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-bus-amqp
#client端服务端口
server:
port: 9902
#client端服务名称
spring:
application:
name: cloud-config-client
#cloud-config配置信息
cloud:
config:
name: springcloud-config-client
profile: dev
uri: http://localhost:9901/
label: master
#开启mq的日志追踪
bus:
trace:
enabled: true
注意:
@RefreshScope
@RestController
public class ConfigController {
@Value("${myName}") // 对应配置中心文件中的userName
private String testData;
@GetMapping("/test")
public String configTest() {
String conString = "";
try {
// ISO 8859-1转UTF-8,解决中文乱码问题
conString = new String(this.testData.getBytes("ISO8859-1"), "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return conString;
}
}
1、首先启动server端,然后启动client端。
2、目前git配置文件信息为 “myName=自动刷新测试”,浏览器输入client端接口:http://localhost:9902/test
输出信息与配置信息正确,测试正常。
3、git配置文件信息改为 “myName=自动刷新测试8888”,cmd命令窗口 post 方式调用 /bus/refresh 接口(也可以使用postman工具)
cmd命令:curl -X POST http://localhost:9901/actuator/bus-refresh(ip:server端口/actuator/bus-refresh)
命令窗口无报错信息,后台控制台也会显示提示信息,表示刷新成功。
然后刷新浏览器,输出信息与配置信息正确,测试正常。
1、我看好多人都加了rabbitMQ,但是加上之后不配置的话会报错,也有可能是本地的缘故,因此我就给去除了,起码目前本地是挺好使的。
2、这种方式属于手动刷新,还有一种方式是自动刷新:git配置webhook。webhook在配置url时需要公网IP,因为是本地服务,所以无法配置,以后买了云服务后再来更新。