主頁(yè) > 知識(shí)庫(kù) > 使用docker compose搭建consul集群環(huán)境的例子

使用docker compose搭建consul集群環(huán)境的例子

熱門標(biāo)簽:宿城區(qū)電話機(jī)器人找哪家 河南防封號(hào)電銷機(jī)器人是什么 麗江真人語(yǔ)音電話外呼系統(tǒng) 福州企業(yè)電銷機(jī)器人排名 400電話辦理介紹信 10086外包用的什么外呼系統(tǒng) 怎么找到?jīng)]有地圖標(biāo)注的店 打400電話怎么辦理收費(fèi) 上海申請(qǐng)高400開頭的電話

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ù)沒有限制。

server模式啟動(dòng)的命令行參數(shù)

  • -server:表示當(dāng)前使用的server模式;如果沒有指定,則表示是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),類似開啟心跳。
  • -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的開發(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)文章!

標(biāo)簽:遵義 運(yùn)城 連云港 朝陽(yáng) 荊門 面試通知 雞西 隴南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用docker compose搭建consul集群環(huán)境的例子》,本文關(guān)鍵詞  使用,docker,compose,搭建,consul,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《使用docker compose搭建consul集群環(huán)境的例子》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于使用docker compose搭建consul集群環(huán)境的例子的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章