spring-cloud版本:Finchley.SR1
本文是用數(shù)據(jù)庫的方式存儲配置信息的棋枕,本人覺得 git 的方式實在不適合后期維護與管理
客戶端的pom 配置如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
客戶端yml配置 材部,注意要配置上mq,需要用mq傳消息:
spring:
cloud:
config:
discovery:
enabled: true # 通過服務發(fā)現(xiàn)的方式去找配置中心
serviceId: config_server # 配置中心的名字杏节,直接配置名稱可以在配置中心集群的時候實現(xiàn)負載均衡
profile: dev # 對應配置中心文件的${profile}部分
bus:
enabled: true
rabbitmq:
host: xxx.xxx.190.69
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: bus-refresh
客戶端代碼 :
@RestController
@RefreshScope
public class UserController {
@Value("${mysql}")
private String name;
@RequestMapping(value = "/demo/getValue",method = RequestMethod.GET)
public @ResponseBody Object getBook(){
return name;
}
數(shù)據(jù)庫中要新增一條配置唬渗,注意application 列要寫上服務名
image.png
調(diào)用查詢配置的接口查詢,沒有問題ok
image.png
修改配置后奋渔,調(diào)刷新接口
image.png
再次調(diào)用查詢接口镊逝,發(fā)現(xiàn)配置文件已經(jīng)更新成功了,done.