微服务之consul(一)
一、概述
consul是google開源的一個使用go語言開發(fā)的服務(wù)發(fā)現(xiàn)、配置管理中心服務(wù)。內(nèi)置了服務(wù)注冊與發(fā)現(xiàn)框 架、分布一致性協(xié)議實現(xiàn)、健康檢查、Key/Value存儲、多數(shù)據(jù)中心方案,不再需要依賴其他工具(比如ZooKeeper等)。服務(wù)部署簡單,只有一個可運行的二進制的包。每個節(jié)點都需要運行agent,他有兩種運行模式server和client。每個數(shù)據(jù)中心官方建議需要3或5個server節(jié)點以保證數(shù)據(jù)安全,同時保證server-leader的選舉能夠正確的進行。
@client
CLIENT表示consul的client模式,就是客戶端模式。是consul節(jié)點的一種模式,這種模式下,所有注冊到當(dāng)前節(jié)點的服務(wù)會被轉(zhuǎn)發(fā)到SERVER,本身是不持久化這些信息。
@server
SERVER表示consul的server模式,表明這個consul是個server,這種模式下,功能和CLIENT都一樣,唯一不同的是,它會把所有的信息持久化的本地,這樣遇到故障,信息是可以被保留的。
@server-leader
中間那個SERVER下面有LEADER的字眼,表明這個SERVER是它們的老大,它和其它SERVER不一樣的一點是,它需要負(fù)責(zé)同步注冊的信息給其它的SERVER,同時也要負(fù)責(zé)各個節(jié)點的健康監(jiān)測。
@raft
server節(jié)點之間的數(shù)據(jù)一致性保證,一致性協(xié)議使用的是raft,而zookeeper用的paxos,etcd采用的也是taft。
@服務(wù)發(fā)現(xiàn)協(xié)議
consul采用http和dns協(xié)議,etcd只支持http
@服務(wù)注冊
consul支持兩種方式實現(xiàn)服務(wù)注冊,一種是通過consul的服務(wù)注冊http?API,由服務(wù)自己調(diào)用API實現(xiàn)注冊,另一種方式是通過json個是的配置文件實現(xiàn)注冊,將需要注冊的服務(wù)以json格式的配置文件給出。consul官方建議使用第二種方式。
@服務(wù)發(fā)現(xiàn)
consul支持兩種方式實現(xiàn)服務(wù)發(fā)現(xiàn),一種是通過http?API來查詢有哪些服務(wù),另外一種是通過consul?agent?自帶的DNS(8600端口),域名是以NAME.service.consul的形式給出,NAME即在定義的服務(wù)配置文件中,服務(wù)的名稱。DNS方式可以通過check的方式檢查服務(wù)。
@服務(wù)間的通信協(xié)議
Consul使用gossip協(xié)議管理成員關(guān)系、廣播消息到整個集群,他有兩個gossip? pool(LAN?pool和WAN?pool),LAN?pool是同一個數(shù)據(jù)中心內(nèi)部通信的,WAN pool是多個數(shù)據(jù)中心通信的,LAN?pool有多個,WAN?pool只有一個。
?
二、consul集群搭建
1)安裝
首先去官網(wǎng)現(xiàn)在合適的consul包:https://www.consul.io/downloads.html
安裝直接下載zip包,解壓后只有一個可執(zhí)行的文件consul,將consul添加到系統(tǒng)的環(huán)境變量里面。
#unzip?consul_1.2.3_linux_amd64.zip
#cp -a consul? /usr/bin
#consul
Usage: consul [--version] [--help] <command> [<args>]Available commands are:agent Runs a Consul agentcatalog Interact with the catalogconnect Interact with Consul Connectevent Fire a new eventexec Executes a command on Consul nodesforce-leave Forces a member of the cluster to enter the "left" stateinfo Provides debugging information for operators.intention Interact with Connect service intentionsjoin Tell Consul agent to join clusterkeygen Generates a new encryption keykeyring Manages gossip layer encryption keyskv Interact with the key-value storeleave Gracefully leaves the Consul cluster and shuts downlock Execute a command holding a lockmaint Controls node or service maintenance modemembers Lists the members of a Consul clustermonitor Stream logs from a Consul agentoperator Provides cluster-level tools for Consul operatorsreload Triggers the agent to reload configuration filesrtt Estimates network round trip time between nodessnapshot Saves, restores and inspects snapshots of Consul server statevalidate Validate config files/directoriesversion Prints the Consul versionwatch Watch for changes in Consul?
?輸入consul,出現(xiàn)上面的內(nèi)容證明安裝成功。
?
2)啟動
consul必須啟動agent才能使用,有兩種啟動模式server和client,還有一個官方自帶的ui。server用與持久化服務(wù)信息,集群官方建議3或5個節(jié)點。client只用與于server交互。ui可以查看集群情況的。
server:cn1:#consul agent? -bootstrap-expect 2? -server? ?-data-dir /data/consul0 -node=cn1 -bind=192.168.1.202 -config-dir /etc/consul.d -enable-script-checks=true? -datacenter=dc1?cn2:#consul agent? ? -server? -data-dir /data/consul0 -node=cn2 -bind=192.168.1.201 -config-dir /etc/consul.d -enable-script-checks=true? -datacenter=dc1? -join 192.168.1.202cn3:#consul agent? -server? -data-dir /data/consul0 -node=cn3 -bind=192.168.1.200 -config-dir /etc/consul.d -enable-script-checks=true? -datacenter=dc1? -join 192.168.1.202參數(shù)解釋:
-bootstrap-expect:集群期望的節(jié)點數(shù),只有節(jié)點數(shù)量達到這個值才會選舉leader。
-server:?運行在server模式
-data-dir:指定數(shù)據(jù)目錄,其他的節(jié)點對于這個目錄必須有讀的權(quán)限
-node:指定節(jié)點的名稱
-bind:為該節(jié)點綁定一個地址
-config-dir:指定配置文件,定義服務(wù)的,默認(rèn)所有一.json結(jié)尾的文件都會讀
-enable-script-checks=true:設(shè)置檢查服務(wù)為可用
-datacenter:?數(shù)據(jù)中心沒名稱,
-join:加入到已有的集群中
client:#consul agent? ?-data-dir /data/consul0 -node=cn4 -bind=192.168.1.199 -config-dir /etc/consul.d -enable-script-checks=true? -datacenter=dc1? -join 192.168.1.202client節(jié)點可以有多個,自己根據(jù)服務(wù)指定即可。
ui:#consul agent? -ui? -data-dir /data/consul0 -node=cn4 -bind=192.168.1.198? -client 192.168.1.198? ?-config-dir /etc/consul.d -enable-script-checks=true? -datacenter=dc1? -join 192.168.1.202?-ui:使用自帶的ui,
-ui-dir:指定ui的目錄,使用自己定義的ui
-client:指定web? ui、的監(jiān)聽地址,默認(rèn)127.0.0.1只能本機訪問。
集群創(chuàng)建完成后:
使用一些常用的命令檢查集群的狀態(tài):
#consul? info
可以在raft:stat看到此節(jié)點的狀態(tài)是Fllower或者leader
#consul membersNode Address Status Type Build Protocol DC Segment cn1 192.168.1.202:8301 alive server 1.0.2 2 dc1 <all> cn2 192.168.1.201:8301 alive server 1.0.2 2 dc1 <all> cn3 192.168.1.200:8301 alive client 1.0.2 2 dc1 <default>新加入一個節(jié)點有幾種方式;
1、這種方式,重啟后不會自動加入集群#consul? join? 192.168.1.2022、#在啟動的時候使用-join指定一個集群#consul agent? -ui? -data-dir /data/consul0 -node=cn4 -bind=192.168.1.198 -config-dir /etc/consul.d -enable-script-checks=true? -datacenter=dc1? -join 192.168.1.2023、使用-startjoin或-rejoin#consul agent? -ui? -data-dir /data/consul0 -node=cn4 -bind=192.168.1.198 -config-dir /etc/consul.d -enable-script-checks=true? -datacenter=dc1? -rejoin?
訪問ui:
http://192.168.1.198:8500/ui
端口:
8300:consul?agent服務(wù)relplaction、rpc(client-server)
8301:lan?gossip
8302:wan?gossip
8500:http?api端口
8600:DNS服務(wù)端口
?
3)服務(wù)注冊
采用的是配置文件的方式,(官方推薦)首先創(chuàng)建一個目錄用于存放定義服務(wù)的配置文件
#mkdir /etc/consul.d/
啟動服務(wù)的時候要使用-config-dir?參數(shù)指定。
下面給出一個服務(wù)定義:
#cat?web.json
?
{"service":{"name":"web","tags":["rails"],"port":80,"check":{"name":"ping","script":"curl -s localhost:80","interval":"3s"}} }?
如果這樣啟動consul后,會發(fā)現(xiàn)consul的日志里面一直報錯,因為我們沒有啟動80端口的服務(wù),下面給出我寫的一個go程序:
#cat? web.go
package main import ("io""log""net/http""strconv""fmt" ) var iCnt int = 0;func helloHandler(w http.ResponseWriter, r*http.request) {iCnt++;str :="Hell eorld ! friend("+ strconv.Itoa(iCnt)+")"io.WriteString(w,str)fmt.Println(str) }func main(){ht :=http.HanderFunc(helloHandler)if ht != nil {http.Handle("/hello",ht)}err := http.ListenAndServe(":80",nil)if err != nil{log.Fatal("ListenAndserve:",err.Error())} }?
#需要一個goalong的環(huán)境:#go? build -o? web? web.go#./web此時就可以在沒有運行web服務(wù)的機器上面執(zhí)行DNS查詢:#?dig @127.0.0.1 -p 8600 web.service.consul SRV;; ANSWER SECTION: web.service.consul. 0 IN SRV 1 1 80 cn2.node.dc1.consul. web.service.consul. 0 IN SRV 1 1 80 cn3.node.dc1.consul.;; ADDITIONAL SECTION: cn2.node.dc1.consul. 0 IN A 192.168.1.201 cn2.node.dc1.consul. 0 IN TXT "consul-network-segment=" cn3.node.dc1.consul. 0 IN A 192.168.1.200 cn3.node.dc1.consul. 0 IN TXT "consul-network-segment=";; Query time: 17 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: 四 1月 04 14:39:32 CST 2018 ;; MSG SIZE rcvd: 229可以看到服務(wù)已經(jīng)注冊到集群里面了。?
使用dns查詢,默認(rèn)域名格式NAME.service.consul,NAME就是web.json里面定義的service的name??梢宰约褐付ㄓ蚝投丝?#xff1a;-domain、-dns-port 53?
為了方便使用consul集群的注冊使用,所以寫了一個三節(jié)點client的注冊腳本,方便統(tǒng)一注冊服務(wù)和管理。還利用到了nfs,將服務(wù)文件共享到集群。
#!/usr/bin/env python #encoding: utf8 #decription: registered a service to consul from subprocess import call import syshosts = {"b1":"10.10.1.01:8500","b2":"10.10.7.1:8500","b3":"10.10.8.21:8500"}def consul_relaod():if (len(sys.argv) != 2) or sys.argv[1] == '-h':print(("Usage: {0} [option] :{1} ,if you wang update all of them,you must use 'all'").format(sys.argv[0],hosts.keys()))sys.exit()elif(sys.argv[1] == 'all'):for i in hosts.keys():call(("consul reload -http-addr {}").format(hosts[i]),shell=True)else:call(("consul reload -http-addr {}").format(hosts[sys.argv[1]]),shell=True)if __name__ == '__main__':consul_relaod()hosts是client節(jié)點列表??梢灾蛔云渲械囊粋€節(jié)點,輸入hosts中對應(yīng)的key,也可以輸入all,注冊到所有節(jié)點;nfs共享的是/etc/consul.d目錄。
4)健康檢查
check使用來做服務(wù)的健康檢查的,可以擁有多個,也可以不使用支持多種方式檢查。check必須是script或者TTL類型的,如果是script類型則script和interval變量必須被提供,如果是TTL類型則ttl變量必須被提供。script是consul主動去檢查服務(wù)的健康狀況,ttl是服務(wù)主動向consul報告自己的狀況。
script?check:{"check":{"id": mutil-memory,"name": "memory utilization","tags": ["system"],"script": "/etc/init.d/check_memory.py","interval": "10s","timeout": "1s"}}http check:{"check": {"id": "api","name": "HTTP API? 500","http": "http://loclhost:500/health","interval": "10s","timeout": "1s"}}tcp? check:{"check": {"id": "ssh","name": "ssh TCP 26622","tcp": "localhost:26622","interval": "10s","timeout": "1s"}}ttl? check:{"check": {"id": "web-app","name": "Web APP status","notes": "Web APP does a curl? internally every 10 seconds","ttl": "30s"}}三、更新consul版本為最新版本1.2.3.
版本更新特性:
https://github.com/hashicorp/consul/blob/v1.2.3/CHANGELOG.md
FEATURES:agent: New Cloud Auto-join provider: Kubernetes (K8S) [GH-4635] http: Added support for "Authorization: Bearer" head in addition to the X-Consul-Token header. [GH-4483] dns: Added a way to specify SRV weights for each service instance to allow weighted DNS load-balancing. [GH-4198] dns: Include EDNS-ECS options in EDNS responses where appropriate: see RFC 7871 [GH-4647] IMPROVEMENTS:ui: Switch to fullscreen layout for lists and detail, left aligned forms [GH-4435] connect: TLS certificate readiness now performs x509 certificate verification to determine whether the cert is usable. [GH-4540] ui: The syntax highlighting/code editor is now on by default [GH-4651] ui: Fallback to showing Node.Address if Service.Address is not set [GH-4579]ui較之前有很大改變:
?
?
?
總結(jié)
以上是生活随笔為你收集整理的微服务之consul(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx动态配置及服务发现那些事
- 下一篇: openresty+consul动态配置