基于consul和git的配置中心
在《Spring Cloud Finchley.SR1 的学习与应用 2 - Consul》中讲述来consul和git2consul的安装。这边文章讲述以下基于基于consul和git的配置中心。
准备工作
启动consul server 和 git2consul,在git仓库中新建common-server-config-respo模块,用来存储配置文件。将common-server-config-respo/business-a-woqu/application.yml文件推送至git仓库。application.yml内容如下:
extend: info: desc: i am business a dev modify
关于配置文件,请看
添加配置中心模块
以server-businessa-woqu为例,添加配置中心模块
- POM 在pom.xml中加入以下依赖:
org.springframework.cloud spring-cloud-starter-consul-config
- 配置文件
分离配置文件,建立application.yml、bootstrap.yml两个配置文件
关于bootstrap.yml,请看
bootstrap.yml
spring: application: name: business-a-woqu cloud: consul: host: woqu.consul port: 8500 discovery: #instance-id: ${spring.application.name}:${server.port} instance-group: ${spring.application.name} register: true config: enabled: true #默认是true format: YAML # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES fail-fast: true watch: enabled: true default-context: ${spring.application.name} #指定consul配置的配置文件父路径 # 指定consul配置的文件夹前缀为config prefix: woqu_configuration/master/common-server-config-respo data-key: application.yml#consul config 路径:prefix defaultContext data-keyserver: port: 9001
application.yml
feign: hystrix: enabled: truemanagement: endpoints: web: exposure: include: "*" exclude: devlogging: level: root: info com.woqu: debughystrix: command: default: execution: isolation: strategy: THREADhystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000ribbon: ConnectTimeout: 10000 ReadTimeout: 60000
- 测试
编写测试controller
@RestControllerpublic class DescController { @Value("${extend.info.desc:error}") private String desc; @GetMapping("/desc") public String desc() { return desc; }}
启动consul server,server-businessa-woqu,发送请求:
GET http://127.0.0.1:9001/descHTTP/1.1 200 Content-Type: text/plain;charset=UTF-8Content-Length: 26Date: Tue, 20 Nov 2018 07:51:26 GMTi am business a dev modifyResponse code: 200; Time: 176ms; Content length: 26 bytes
开启更新机制
需要给加载变量的类上面加载@RefreshScope,在客户端执行/actuator/refresh的时候就会更新此类下面的变量值。
@RestController@RefreshScopepublic class DescController { @Value("${extend.info.desc:error}") private String desc; @GetMapping("/desc") public String desc() { return desc; }}
启动consul server,server-businessa-woqu,发送请求:
GET http://127.0.0.1:9001/descHTTP/1.1 200 Content-Type: text/plain;charset=UTF-8Content-Length: 26Date: Tue, 20 Nov 2018 07:51:26 GMTi am business a dev modifyResponse code: 200; Time: 176ms; Content length: 26 bytes
更改common-server-config-respo/business-a-woqu/application.yml中信息:
extend: info: desc: i am business a dev modify after commit
请求刷新接口
POST http://127.0.0.1:9001/actuator/refreshHTTP/1.1 200 Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8Transfer-Encoding: chunkedDate: Tue, 20 Nov 2018 07:54:01 GMT[]Response code: 200; Time: 710ms; Content length: 2 bytes
发送请求:
GET http://127.0.0.1:9001/descHTTP/1.1 200 Content-Type: text/plain;charset=UTF-8Content-Length: 39Date: Tue, 20 Nov 2018 08:07:44 GMTi am business a dev modify after commitResponse code: 200; Time: 11ms; Content length: 39 bytes
以上是通过git commit来实现更新的,也可以在consul控制台直接更改value来更新。