使用docker compose搭建consul集群環(huán)境的例子
server模式和client模式server模式和client模式是consul節(jié)點(diǎn)的類型;client不是指的用戶客戶端。
server模式提供數(shù)據(jù)持久化功能。 client模式不提供持久化功能,并且實(shí)際上他也不工作,只是把用戶客戶端的請求轉(zhuǎn)發(fā)到server模式的節(jié)點(diǎn)。所以可以把client模式的節(jié)點(diǎn)想象成LB(load balance),只負(fù)責(zé)請求轉(zhuǎn)發(fā)。 通常server模式的節(jié)點(diǎn)需要配置成多個(gè)例如3個(gè),5個(gè)。而client模式節(jié)點(diǎn)個(gè)數(shù)沒有限制。server模式啟動(dòng)的命令行參數(shù)
-server:表示當(dāng)前使用的server模式;如果沒有指定,則表示是client模式。 -node:指定當(dāng)前節(jié)點(diǎn)在集群中的名稱。 -config-dir:指定配置文件路徑,定義服務(wù)的;路徑下面的所有.json結(jié)尾的文件都被訪問;缺省值為:/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),類似開啟心跳。 -bind: 綁定服務(wù)器的ip地址。 -client: 客戶端可訪問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)上必須保持一致。這里說明一下,另外一個(gè)參數(shù)-bootstrap,用來控制一個(gè)server是否運(yùn)行在bootstrap模式:當(dāng)一個(gè)server處于bootstrap模式時(shí),它可以選舉自己為leader;注意在一個(gè)datacenter中只能有一個(gè)server處于bootstrap模式。所以這個(gè)參數(shù)一般只能用在只有一個(gè)server的開發(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來搭建如下的consul集群環(huán)境
集群包含三個(gè)server:node1, node2, node3 集群包含一個(gè)client:node4;并且在client上提供web UI訪問服務(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 membersNode Address Status Type Build Protocol DC Segmentnode1 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>
訪問http://127.0.0.1:8500
注冊配置中心例子
spring: application: name: cloud-payment-service ####consul注冊中心地址 cloud: consul: enabled: true host: 127.0.0.1 port: 8500 discovery:hostname: 127.0.0.1prefer-ip-address: trueservice-name: ${spring.application.name}#healthCheckInterval: 15sinstance-id: ${spring.application.name}-8002enabled: true
KV訪問的例子
$ 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 fooHello foo$ docker exec -t node4 consul kv get -detailed foo/foo1CreateIndex 124Flags 0Key foo/foo1LockIndex0ModifyIndex 124Session -Value Hello foo1$ docker exec -t node4 consul kv get -keys -separator='' foofoofoo/foo1foo/foo2foo/foo2/foo21$ docker exec -t node4 consul kv get not-a-real-keyError! No key exists at: not-a-real-key
以上就是使用docker compose搭建consul集群環(huán)境的詳細(xì)內(nèi)容,更多關(guān)于docker compose集群環(huán)境的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python 如何在 Matplotlib 中繪制垂直線2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. ASP常用日期格式化函數(shù) FormatDate()4. python中@contextmanager實(shí)例用法5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 如何通過python實(shí)現(xiàn)IOU計(jì)算代碼實(shí)例9. 開發(fā)效率翻倍的Web API使用技巧10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
