Config Server
和 Config Client
分别引入 spring-boot-starter-actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
SpringBoot2.0+
版本的刷新接口为 /actuator/refresh
,并且在bootstrap.yml
中添加配置
management:
endpoints:
web:
exposure:
include: '*'
current profile:update before
current:
profile: '{cipher}c7b4ba3110b228ffe3f1b477504be3a8c24cb44c1a5a9b942c79148084efefed'
profile1: update after
["current.profile1","config.client.version"]
current profile:update after
但是这种方式是主动请求更新配置,适合管理员操作管理控制台更新配置。如果要实现多个客户端同步更新的话,就需要spring cloud bus
登场了。首先安装rabbitmq
。
添加spring-cloud-start-bus-amqp
引用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
Config Server
和Config Client
添加rabbitmq
配置
management:
endpoints:
web:
exposure:
include: '*'
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: panzi
password: 123456
virtual-host: fly
启动Config Server
,Config Client
,访问两个Client
current profile:default-v1
使用curl
请求Config Server
刷新配置
curl -X POST http://panzi:123456@localhost:8000/actuator/bus-refresh
再次查看客户端,配置已经刷新,这里要注意认证的Config Server
,需要增加配置:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}