Service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.
服務發現和配置,分布式,高可用性,數據中心。
-
服務注冊 - 服務端注冊相應的的主機、端口號及其它身份驗證信息,協議,版本號,以及運行環境等詳細資料。
-
服務發現 - 客戶端應用通過向注冊中心查詢,獲取可用服務列表,相應服務詳細信息。
-
基本服務格式:
1 { 2 "service":{ 3 "id": "myservice", 4 "name": "myservice", 5 "address": "servicehost", 6 "port": serviceport, 7 "tags": ["tag"], 8 "checks": [ 9 { 10 "http": "http://host.port/health", 11 "interval": "5s" 12 } 13 ] 14 } 15 }
-
健康檢查(checks):
script check:consul主動去檢查服務的健康狀況1 { 2 "check": { 3 "id": "scheck", 4 "name": "scheck", 5 "script": "/*.py", //必須 6 "interval": "10s", //必須 7 "timeout": "1s" 8 } 9 }
ttl check:服務主動向consul報告自己的健康狀況
1 { 2 "check": { 3 "id": "scheck", 4 "name": "scheck", 5 "notes": "scheck", 6 "ttl": "30s" 7 } 8 }
http check:
1 { 2 "check": { 3 "id": "scheck", 4 "name": "scheck", 5 "http": "http://host:port/health", 6 "interval": "10s", 7 "timeout": "1s" 8 } 9 }
tcp check:
1 { 2 "check": { 3 "id": "scheck", 4 "name": "scheck", 5 "tcp": "host:22", 6 "interval": "10s", 7 "timeout": "1s" 8 } 9 }
-
服務注冊:
-
配置文件靜態注冊:/etc/consul.d/myserver.json,
添加如上服務配置,重啟consul,並將配置文件的路徑給consul(指定參數:-config-dir /etc/consul.d)
-
HTTP API接口來動態注冊:/v1/agent/service/register http put方法注冊。
curl -X PUT -d '{"id": "myserver","name": "myserver","address": "serverhost","port": serverport,"tags": ["tag"],"checks": [{"http": "http://healthhost:port/health","interval": "5s"}]}' http://host:8500/v1/agent/service/register
-
-
maven dependency:
1 <dependency> 2 <groupId>com.orbitz.consul</groupId> 3 <artifactId>consul-client</artifactId> 4 <version>xxx</version> 5 </dependency>
1 <dependency> 2 <groupId>com.ecwid.consul</groupId> 3 <artifactId>consul-api</artifactId> 4 <version>xxx</version> 5 </dependency>
項目地址:https://github.com/windwant/windwant-service/tree/master/consul-service
