項目是針對Consul的服務治理實現。Consul是一個分佈式高可用的系統,具備分佈式、高可用、高擴展性html
Consul 是 HashiCorp 公司推出的開源工具,用於實現分佈式系統的服務發現與配置。與其餘分佈式服務註冊與發現的方案,Consul的方案更「一站式」 ,內置了服務註冊與發現框 架、具備如下性質:
● 分佈一致性協議實現
● 健康檢查
● Key/Value存儲
● 多數據中心方案
再也不須要依賴其餘工具(好比ZooKeeper等)git
<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>
複製代碼
<!--消息總線,提供配置實時刷新,再也不依賴中間件-->
<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>
<!--服務註冊和發現功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
複製代碼
這裏要注意是要配置在 bootstarp.ymlspring
spring:
application:
name: pig-consul
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
format: KEY_VALUE
watch:
enabled: true
prefix: pig-config
複製代碼
下載: https://www.consul.io/downloads.html
使用:(dev模式,生成建議cluster模式)bash
-dev表示開發模式運行,使用-client 參數可指定容許客戶端使用什麼ip去訪問,例如-client 127.0.0.1 表示能夠使用。
consul agent -dev -client 127.0.0.1
複製代碼
生產配置參考:
https://www.consul.io/intro/getting-started/join.html
http://127.0.0.1:8500/ui/ 去訪問 app
@RestController
public class DemoController {
@Value("${author}")
private String author;
@GetMapping("/demo")
public String demo() {
return author;
}
}
複製代碼
spring:
cloud:
consul:
config:
watch:
enabled: true
複製代碼
而後應用要開啓定時任務分佈式
@EnableScheduling
複製代碼