使用docker compose搭建consul集群環(huán)境的例子
consul基本概念
server模式和client模式
server模式和client模式是consul節(jié)點(diǎn)的類型;client不是指的用戶客戶端。
- server模式提供數(shù)據(jù)持久化功能。
- client模式不提供持久化功能,并且實(shí)際上他也不工作,只是把用戶客戶端的請(qǐng)求轉(zhuǎn)發(fā)到server模式的節(jié)點(diǎn)。所以可以把client模式的節(jié)點(diǎn)想象成LB(load balance),只負(fù)責(zé)請(qǐng)求轉(zhuǎn)發(fā)。
- 通常server模式的節(jié)點(diǎn)需要配置成多個(gè)例如3個(gè),5個(gè)。而client模式節(jié)點(diǎn)個(gè)數(shù)沒(méi)有限制。
server模式啟動(dòng)的命令行參數(shù)
- -server:表示當(dāng)前使用的server模式;如果沒(méi)有指定,則表示是client模式。
- -node:指定當(dāng)前節(jié)點(diǎn)在集群中的名稱。
- -config-dir:指定配置文件路徑,定義服務(wù)的;路徑下面的所有.json結(jié)尾的文件都被訪問(wèn);缺省值為:/consul/config。
- -data-dir: consul存儲(chǔ)數(shù)據(jù)的目錄;缺省值為:/consul/data。
- -datacenter:數(shù)據(jù)中心名稱,缺省值為dc1。
- -ui:使用consul自帶的web UI界面 。
- -join:加入到已有的集群中。
- -enable-script-checks: 檢查服務(wù)是否處于活動(dòng)狀態(tài),類似開(kāi)啟心跳。
- -bind: 綁定服務(wù)器的ip地址。
- -client: 客戶端可訪問(wèn)ip,缺省值為:“127.0.0.1”,即僅允許環(huán)回連接。
- -bootstrap-expect:在一個(gè)datacenter中期望的server節(jié)點(diǎn)數(shù)目,consul啟動(dòng)時(shí)會(huì)一直等待直到達(dá)到這個(gè)數(shù)目的server才會(huì)引導(dǎo)整個(gè)集群。這個(gè)參數(shù)的值在同一個(gè)datacenter的所有server節(jié)點(diǎn)上必須保持一致。
這里說(shuō)明一下,另外一個(gè)參數(shù)-bootstrap,用來(lái)控制一個(gè)server是否運(yùn)行在bootstrap模式:當(dāng)一個(gè)server處于bootstrap模式時(shí),它可以選舉自己為leader;注意在一個(gè)datacenter中只能有一個(gè)server處于bootstrap模式。所以這個(gè)參數(shù)一般只能用在只有一個(gè)server的開(kāi)發(fā)環(huán)境中,在有多個(gè)server的cluster產(chǎn)品環(huán)境中,不能使用這個(gè)參數(shù),否則如果多個(gè)server都標(biāo)記自己為leader那么會(huì)導(dǎo)致數(shù)據(jù)不一致。另外該標(biāo)記不能和-bootstrap-expect同時(shí)指定。
使用docker-compose來(lái)搭建如下的consul集群環(huán)境
- 集群包含三個(gè)server:node1, node2, node3
- 集群包含一個(gè)client:node4;并且在client上提供web UI訪問(wèn)服務(wù)。.
編輯docker-compose.yml文件
version: '2' networks: byfn: services: consul1: image: consul container_name: node1 command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 networks: - byfn consul2: image: consul container_name: node2 command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 depends_on: - consul1 networks: - byfn consul3: image: consul container_name: node3 command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 depends_on: - consul1 networks: - byfn consul4: image: consul container_name: node4 command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui ports: - 8500:8500 depends_on: - consul2 - consul3 networks: - byfn
啟動(dòng)服務(wù)
$ docker-compose up $ docker exec -t node1 consul members Node Address Status Type Build Protocol DC Segment node1 172.21.0.2:8301 alive server 1.4.0 2 dc1 <all> node2 172.21.0.4:8301 alive server 1.4.0 2 dc1 <all> node3 172.21.0.3:8301 alive server 1.4.0 2 dc1 <all> ndoe4 172.21.0.5:8301 alive client 1.4.0 2 dc1 <default>
訪問(wèn)http://127.0.0.1:8500
注冊(cè)配置中心例子
spring: application: name: cloud-payment-service ####consul注冊(cè)中心地址 cloud: consul: enabled: true host: 127.0.0.1 port: 8500 discovery: hostname: 127.0.0.1 prefer-ip-address: true service-name: ${spring.application.name} #healthCheckInterval: 15s instance-id: ${spring.application.name}-8002 enabled: true
KV訪問(wèn)的例子
$ docker exec -t node4 consul kv put foo "Hello foo" $ docker exec -t node4 consul kv put foo/foo1 "Hello foo1" $ docker exec -t node4 consul kv put foo/foo2 "Hello foo2" $ docker exec -t node4 consul kv put foo/foo21 "Hello foo21" $ docker exec -t node4 consul kv get foo Hello foo $ docker exec -t node4 consul kv get -detailed foo/foo1 CreateIndex 124 Flags 0 Key foo/foo1 LockIndex 0 ModifyIndex 124 Session - Value Hello foo1 $ docker exec -t node4 consul kv get -keys -separator="" foo foo foo/foo1 foo/foo2 foo/foo2/foo21 $ docker exec -t node4 consul kv get not-a-real-key Error! No key exists at: not-a-real-key
以上就是使用docker compose搭建consul集群環(huán)境的詳細(xì)內(nèi)容,更多關(guān)于docker compose集群環(huán)境的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。