Spring Cloud Consul
項目是針對Consul的服務(wù)治理實現(xiàn)遭商。Consul是一個分布式高可用的系統(tǒng),具有分布式插佛、高可用产弹、高擴展性
Consul
Consul 是 HashiCorp 公司推出的開源工具,用于實現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置床估。與其他分布式服務(wù)注冊與發(fā)現(xiàn)的方案含滴,Consul的方案更“一站式” ,內(nèi)置了服務(wù)注冊與發(fā)現(xiàn)框 架丐巫、具有以下性質(zhì):
● 分布一致性協(xié)議實現(xiàn)
● 健康檢查
● Key/Value存儲
● 多數(shù)據(jù)中心方案
不再需要依賴其他工具(比如ZooKeeper等)
添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
consul-all依賴提供了哪些功能谈况?
<!--消息總線,提供配置實時刷新递胧,不再依賴中間件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-bus</artifactId>
</dependency>
<!--consul的配置中心功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<!--服務(wù)注冊和發(fā)現(xiàn)功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
bootstarp.yml
這里要注意是要配置在 bootstarp.yml
spring:
application:
name: pig-consul
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
format: KEY_VALUE
watch:
enabled: true
prefix: pig-config
安裝consul
下載: https://www.consul.io/downloads.html
使用:(dev模式碑韵,生成建議cluster模式)
-dev表示開發(fā)模式運行,使用-client 參數(shù)可指定允許客戶端使用什么ip去訪問谓着,例如-client 127.0.0.1 表示可以使用泼诱。
consul agent -dev -client 127.0.0.1
生產(chǎn)配置參考:
https://www.consul.io/intro/getting-started/join.html
http://127.0.0.1:8500/ui/ 去訪問
配置config
demo
@RestController
public class DemoController {
@Value("${author}")
private String author;
@GetMapping("/demo")
public String demo() {
return author;
}
}
關(guān)于實時刷新配置
spring:
cloud:
consul:
config:
watch:
enabled: true
然后應(yīng)用要開啟定時任務(wù)
@EnableScheduling
總結(jié)
- 相較于spring cloud config 的配置中心,使用起來較為麻煩赊锚,但是對于實時刷新治筒,這塊要優(yōu)于spring cloud config 的屉栓,不依賴于中間件的消息通知,也不會出現(xiàn)服務(wù)下線的問題耸袜。
- 這篇文章主要是入門友多,更高級的使用Consul Config 結(jié)合 Git 來做版本控制,請參考我后邊的文章
- 關(guān)于pig:基于Spring Cloud堤框、oAuth2.0開發(fā)基于Vue前后分離的開發(fā)平臺域滥,支持賬號、短信蜈抓、SSO等多種登錄启绰,提供配套視頻開發(fā)教程