https://www.pianshen.com/article/7120340042/
概述
Spring Cloud实战-06使用/actuator/bus-refresh端点手动刷新配置 + 使用Spring Cloud Bus自动更新配置 中说到了@RefreshScope实现配置刷新,这里我们来通过一个例子再来感受下。
4个微服务工程:
- Eureka Server : https://github.com/yangshangwei/springcloud-o2o/tree/master/eureka-server 8762端口
- Artisan Config (Config Server):https://github.com/yangshangwei/springcloud-o2o/tree/master/artisan_config 9898端口
- Artisan Order (Config Client) :https://github.com/yangshangwei/springcloud-o2o/tree/master/artisan_order 8081端口
- 配置文件存储中心: https://github.com/yangshangwei/spring-cloud-config-center/blob/master/artisan-order-dev.yml
配置属性给artisan-order模块使用
我们在远端Git上增加几个自定义的属性
通过config server来访问下 ,确保能正常访问
http://localhost:9898/artisan-order-dev.yml
配置文件
@ConfigurationProperties 参考之前的博客: Spring Boot2.x-03Spring Boot基础-基于properties的类型安全的配置
注解说明 见注释
package com.artisan.order.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Data
@Component
//@ConfigurationProperties:告诉springboot将本类中所有属性和配置文件中相关的配置进行绑定;
// prefix="customized":指出将配置文件中customized下的所有属性进行一一映射
@ConfigurationProperties(prefix = "customized")
// 需要动态刷新配置,加上该注解
@RefreshScope
public class CustomizedConfig {
private String apiUrl;
private String apiCode;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
测试下
package com.artisan.order.controller;
import com.artisan.order.config.CustomizedConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class CustomizedController {
@Autowired
private CustomizedConfig customizedConfig;
@GetMapping("/customizedPro")
public String getCustomizedProperties(){
String apiUrl = customizedConfig.getApiUrl();
String apiCode = customizedConfig.getApiCode();
log.info("apiUrl:{}, apiCode:{}",apiUrl,apiCode);
return "apiUrl:" + apiUrl + " , apiCode:" + apiCode;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
启动服务,访问 http://localhost:8081/customizedPro
将远端的配置修改下
再次访问 http://localhost:8081/customizedPro
还是没变。。。。
接下来通过curl POST手工刷新下吧,或者在git上设置webhooks 自动更新
使用curl 手工刷新配置
curl -v -X POST http://localhost:9898/actuator/bus-refresh
- 1
Artisan Config 日志
2019-04-11 10:33:09.331 INFO 6120 --- [io-9898-exec-10] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2019-04-11 10:33:09.361 INFO 6120 --- [io-9898-exec-10] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@29501753: startup date [Thu Apr 11 10:33:09 CST 2019]; root of context hierarchy
2019-04-11 10:33:09.388 INFO 6120 --- [io-9898-exec-10] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-04-11 10:33:09.389 INFO 6120 --- [io-9898-exec-10] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$616b51b6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-04-11 10:33:10.775 INFO 6120 --- [io-9898-exec-10] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2019-04-11 10:33:10.814 INFO 6120 --- [io-9898-exec-10] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2019-04-11 10:33:10.816 INFO 6120 --- [io-9898-exec-10] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3db4246e: startup date [Thu Apr 11 10:33:10 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@29501753
2019-04-11 10:33:10.817 INFO 6120 --- [io-9898-exec