在《Spring Cloud Finchley.SR1 的學習與應用 2 - Consul》中講述來consul和git2consul的安裝。這邊文章講述如下基於基於consul和git的配置中心。html
啓動consul server 和 git2consul,在git倉庫中新建common-server-config-respo模塊,用來存儲配置文件。將common-server-config-respo/business-a-woqu/application.yml文件推送至git倉庫。application.yml內容以下:java
extend: info: desc: i am business a dev modify
關於配置文件,請看這裏git
以server-businessa-woqu爲例,添加配置中心模塊web
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-config</artifactId> </dependency>
分離配置文件,創建application.yml、bootstrap.yml兩個配置文件spring
關於bootstrap.yml,請看這裏json
bootstrap.ymlbootstrap
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-key server: port: 9001
application.ymlapp
feign: hystrix: enabled: true management: endpoints: web: exposure: include: "*" exclude: dev logging: level: root: info com.woqu: debug hystrix: command: default: execution: isolation: strategy: THREAD hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000 ribbon: ConnectTimeout: 10000 ReadTimeout: 60000
編寫測試controllerspring-boot
@RestController public 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/desc HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 26 Date: Tue, 20 Nov 2018 07:51:26 GMT i am business a dev modify Response code: 200; Time: 176ms; Content length: 26 bytes
須要給加載變量的類上面加載@RefreshScope,在客戶端執行/actuator/refresh的時候就會更新此類下面的變量值。
@RestController @RefreshScope public 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/desc HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 26 Date: Tue, 20 Nov 2018 07:51:26 GMT i am business a dev modify Response 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/refresh HTTP/1.1 200 Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8 Transfer-Encoding: chunked Date: Tue, 20 Nov 2018 07:54:01 GMT [] Response code: 200; Time: 710ms; Content length: 2 bytes
發送請求:
GET http://127.0.0.1:9001/desc HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 39 Date: Tue, 20 Nov 2018 08:07:44 GMT i am business a dev modify after commit Response code: 200; Time: 11ms; Content length: 39 bytes
以上是經過git commit來實現更新的,也能夠在consul控制檯直接更改value來更新。