2.4 Config AutoRefresh - fanpan26/Fly.SpringCloud GitHub Wiki

Config ServerConfig 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: '*'
分别开启服务端和客户端,访问客户端 http://localhost:8081/api/profile

current profile:update before
更新配置文件,并上传到git

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 ServerConfig 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
更新config

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();
    }
}
⚠️ **GitHub.com Fallback** ⚠️