文章目录
一. 修改码云上的配置文件,增加自定义配置
- 一. 修改码云上的配置文件,增加自定义配置
- 二.tensquare_base工程中新建接口
- 三.运行测试看是否能够读取配置信息
- 四.修改码云上的配置文件中的自定义配置
- 五.完成十次方工程的配置集中管理
在LabelController 中,新建如下的接口,并且用@Value注解注入配置文件中sms.ip的值
@Value("${sms.ip}")
private String ip;
@RequestMapping(value = "/ip", method = RequestMethod.GET)
public String ip() {
return ip;
}
三.运行测试看是否能够读取配置信息
发送如下的get请求 http://localhost:9001/label/ip
得到的响应数据如下 127.0.0.1
sms:
ip: 192.168.184.134
通过postman刷新配置 Url: http://127.0.0.1:12000/actuator/bus-refresh
Method: post
再次发送获取sms.ip的请求, 发现获取到的数据还是为127.0.0.1
这是因为我们的 controller少了一个注解@RefreshScope 此注解用于刷新自定义的配置,而Spring自己的配置,是不需要加上此注解就能自动刷新的. 包为 import org.springframework.cloud.context.config.annotation.RefreshScope;
添加此注解后,重启base微服务项目,再次进行测试,发现能够刷新sms.ip的配置了.
将每一个工程的配置文件添加rabbitmq地址,然后把配置文件提取出来,重命名 (2)将这些文件上传到码云 (3)修改每一个微服务工程,pom.xml中添加如下的依赖
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-bus
org.springframework.cloud
spring-cloud-stream-binder-rabbit
org.springframework.boot
spring-boot-starter-actuator
删除每一个微服务的application.yml 为每一个微服务添加bootstrap.yml (参考tesquare_base工程)