consul服務發現與註冊於配置 (mac版為例)
consul 介紹
Consul是HashiCorp公司推出的開源工具,用於實現分散式系統的服務發現與配置。與其他分散式服務註冊與發現的方案,比如 Airbnb的SmartStack等相比,Consul的方案更“一站式”,內建了服務註冊與發現框 架、分佈一致性協議實現、健康檢查、Key/Value儲存、多資料中心方案,不再需要依賴其他工具(比如ZooKeeper等)。使用起來也較 為簡單。Consul用Golang實現,因此具有天然可移植性(支援Linux、windows和Mac OS X);安裝包僅包含一個可執行檔案,方便部署,與Docker等輕量級容器可無縫配合。
gitHub: https://github.com/hashicorp/consul
官網:https://www.consul.io/
官方說明:https://www.consul.io/docs/index.html
go api: https://godoc.org/github.com/hashicorp/consul/api
golang API 程式碼位置:https://github.com/hashicorp/consul/tree/master/api
consul 目錄或檔案說明
/tmp/consul: 資料儲存
/Users/fox/bin/:程式安裝目錄
fox : mac系統使用者的使用者名稱
server 和 client 說明
CLIENT
CLIENT表示consul的client模式,就是客戶端模式。是consul節點的一種模式,這種模式下,所有註冊到當前節點的服務會被轉發到SERVER,本身是不持久化這些資訊。
簡單的說,client 處理健康檢查,註冊服務等,但是這個註冊只是轉發到server中,如果有成千上萬的服務,分別啟動多個client,可以減少server 壓力
SERVER
SERVER表示consul的server模式,表明這個consul是個server,這種模式下,功能和CLIENT都一樣,唯一不同的是,它會把所有的資訊持久化的本地,這樣遇到故障,資訊是可以被保留的。
SERVER-LEADER
中間那個SERVER下面有LEADER的字眼,表明這個SERVER是它們的老大,它和其它SERVER不一樣的一點是,它需要負責同步註冊的資訊給其它的SERVER,同時也要負責各個節點的健康監測。
consul 安裝
mkdir -p /Users/fox/bin/
開啟這個目錄 /Users/fox/bin/
,然後下載consul
解壓縮後 只有一個 單檔案 consul
執行指令碼
./consul
輸出
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
agent Runs a Consul agent
catalog Interact with the catalog
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
snapshot Saves, restores and inspects snapshots of Consul server state
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
設定環境配置
sudo vim /etc/paths.d/user_fox
內容輸入如下
/Users/fox/bin
應用生效
source /etc/profile
測試,在任何目錄中,輸入
consul
輸出
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
agent Runs a Consul agent
catalog Interact with the catalog
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
snapshot Saves, restores and inspects snapshots of Consul server state
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
consul 引數說明
-dev 開發者模式 該節點的啟動不能用於生產環境,因為該模式下不會持久化任何狀態
該啟動模式僅僅是為了快速便捷的啟動單節點consul
該節點處於server模式
該節點是leader
該節點是一個健康節點
-ui 啟動自有主機的介面
-bootstrap-expect 1 叢集節點,表示等待多少個節點再啟動,這裡是1個,一個就啟動
-bind=127.0.0.1 繫結IP ,本機IP地址,內網IP
-advertise-wan=10.23.123.12 繫結外網ip
-node=node1 節點名稱,如果沒有,預設是主機名
-server 設定為服務端
-data-dir /tmp/consul 資料儲存目錄為 /tmp/consul
-datacenter=dc1 資料中心
更多請看
https://www.consul.io/docs/agent/options.html
consul agent server client
agent可以執行在server或者client模式
consul agent server 服務端
consul agent server 服務端啟動
consul agent -dev -server -bootstrap-expect 1 -data-dir /tmp/consul -node=node1
停止
Ctrl+C #按鍵
如果你按錯了那麼使用強制停止
ps -ef |grep consul
輸出
501 831 813 0 1:12下午 ttys001 0:02.37 consul agent -dev -config-dir /Users/fox/bin/consul.d/
501 890 813 0 1:16下午 ttys001 0:00.00 grep consul
第一行就是要停止的程序
kill -9 831
檢視成員
consul members
服務操作
建立一個 服務
新建目錄
mkdir -p /Users/fox/bin/consul.d
建立web
服務和service2
服務
echo '{"service": {"name": "web", "tags": ["rails"], "port": 801}}' >/Users/fox/bin/consul.d/web.json
echo '{"service": {"name": "service2", "tags": ["rails"], "port": 802}}' >/Users/fox/bin/consul.d/service2.json
啟動代理(你要先關閉之前開啟的那個代理哦)
consul agent -dev -config-dir /Users/fox/bin/consul.d/
查詢服務
DNS 查詢
服務的DNS名稱是 NAME.service.consul
service2:服務名稱
web:服務名稱
dig @127.0.0.1 -p 8600 service2.service.consul
輸出
; <<>> DiG 9.9.7-P3 <<>> @127.0.0.1 -p 8600 service2.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56742
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;service2.service.consul. IN A
;; ANSWER SECTION:
service2.service.consul. 0 IN A 127.0.0.1
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Thu Jan 18 13:29:46 CST 2018
;; MSG SIZE rcvd: 68
HTTP API 查詢
curl http://localhost:8500/v1/catalog/service/service2
輸出
[
{
"ID": "e3e23dc4-f3c7-24bb-e35a-53ae6bcae58a",
"Node": "fox",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "service2",
"ServiceName": "service2",
"ServiceTags": [
"rails"
],
"ServiceAddress": "",
"ServicePort": 802,
"ServiceEnableTagOverride": false,
"CreateIndex": 6,
"ModifyIndex": 6
}
]
查詢健康
curl 'http://localhost:8500/v1/health/service/service2?passing'
輸出
[
{
"Node": {
"ID": "e3e23dc4-f3c7-24bb-e35a-53ae6bcae58a",
"Node": "fox",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"Meta": {
"consul-network-segment": ""
},
"CreateIndex": 5,
"ModifyIndex": 6
},
"Service": {
"ID": "service2",
"Service": "service2",
"Tags": [
"rails"
],
"Address": "",
"Port": 802,
"EnableTagOverride": false,
"CreateIndex": 6,
"ModifyIndex": 6
},
"Checks": [
{
"Node": "fox",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"ServiceTags": [],
"Definition": {},
"CreateIndex": 5,
"ModifyIndex": 5
}
]
}
]
Consul Web介面
加入 -ui 引數, 啟動自有主機的介面
啟動代理(你要先關閉之前開啟的那個代理哦)
consul agent -dev -ui -config-dir /Users/fox/bin/consul.d/
等待一會,瀏覽器中輸入如下,就可以看到UI介面了
http://localhost:8500/ui
Consul 叢集 docker版
Consul 叢集
稍後新增
A&Q
Error starting agent: Failed to start Consul server: Failed to start RPC layer: listen tcp 127.0.0.1:8300: bind: address already in use
原因:你已經啟動一個了,要麼關閉它,要麼繼續使用已經啟動過得
用這個命令檢視
ps -ef |grep consul