注册中心—组件—Consul
原文作者:DreamsonMa
原文地址:Consul 快速入門(mén) - Kong最佳實(shí)踐
目錄
Consul是什么
安裝Consul
運(yùn)行Agent
注冊(cè)服務(wù)
Consul集群
KV數(shù)據(jù)
WEB UI
Consul是什么
Consul是一個(gè)服務(wù)網(wǎng)格解決方案(微服務(wù)間的 TCP/IP,負(fù)責(zé)服務(wù)之間的網(wǎng)絡(luò)調(diào)用、限流、熔斷和監(jiān)控),它是一個(gè)分布式的、高度可用的系統(tǒng),而且開(kāi)發(fā)使用都很簡(jiǎn)便。它提供了一個(gè)功能齊全的控制平面,主要特點(diǎn)是:服務(wù)發(fā)現(xiàn)、健康檢查、鍵值存儲(chǔ)、安全服務(wù)通信、多數(shù)據(jù)中心。
安裝Consul
這里提供兩臺(tái)Centos:local12,local13安裝方式:
[root@local13 ~] wget https://releases.hashicorp.com/consul/1.2.2/consul_1.2.2_linux_amd64.zip [root@local13 ~] unzip consul_1.2.2_linux_amd64.zip [root@local13 ~] ./consul Usage: consul [--version] [--help] <command> [<args>] # local12同上運(yùn)行Agent
安裝Consul后必須運(yùn)行Agent,可以選擇服務(wù)器或客戶(hù)端模式。每個(gè)數(shù)據(jù)中心至少有一個(gè)服務(wù)器(推薦3~5個(gè)服務(wù)器集群)。簡(jiǎn)單起見(jiàn),先啟動(dòng)一個(gè)Agent的開(kāi)發(fā)模式:
# Agent的開(kāi)發(fā)模式 [root@local13 ~] ./consul agent -dev ==> Starting Consul agent... ==> Consul agent running!# 查看集群成員 [root@local13 ~] ./consul members Node Address Status Type Build Protocol DC Segment local13 127.0.0.1:8301 alive server 1.2.2 2 dc1 <all># 使用 HTTP API 查看 [root@local13 ~] curl localhost:8500/v1/catalog/nodes [{"ID": "796b14fe-1332-4aa0-d96f-8f287a4ccc7e","Node": "local13","Address": "127.0.0.1","Datacenter": "dc1","TaggedAddresses": {"lan": "127.0.0.1","wan": "127.0.0.1"},"Meta": {"consul-network-segment": ""},"CreateIndex": 9,"ModifyIndex": 10} ] # 還可以使用 DNS 接口來(lái)查詢(xún)節(jié)點(diǎn)(默認(rèn)端口:8600) [root@local13 ~] yum install bind-utils [root@local13 ~] dig @127.0.0.1 -p 8600 local13.node.consul ... ;; QUESTION SECTION: ;local13.node.consul. IN A ;; ANSWER SECTION: local13.node.consul. 0 IN A 127.0.0.1 ...注冊(cè)服務(wù)
1、定義一個(gè)服務(wù)
[root@local13 ~] mkdir /etc/consul.d [root@local13 ~] echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' | sudo tee /etc/consul.d/web.json [root@local13 ~] ./consul agent -dev -config-dir=/etc/consul.d2、查詢(xún)一個(gè)服務(wù)
# 使用 DNS API [root@local13 ~] dig @127.0.0.1 -p 8600 web.service.consul ... ;; QUESTION SECTION: ;web.service.consul. IN A ;; ANSWER SECTION: web.service.consul. 0 IN A 127.0.0.1# 使用 DNS API 查找 SRV 記錄 [root@local13 ~] dig @127.0.0.1 -p 8600 web.service.consul SRV ... ;; QUESTION SECTION: ;web.service.consul. IN SRV ;; ANSWER SECTION: web.service.consul. 0 IN SRV 1 1 80 local13.node.dc1.consul. ;; ADDITIONAL SECTION: local13.node.dc1.consul. 0 IN A 127.0.0.1 ...# 使用 HTTP API 查詢(xún) [root@local13 ~] curl http://localhost:8500/v1/catalog/service/web # 健康檢查 [root@local13 ~] curl 'http://localhost:8500/v1/health/service/web?passing'Consul集群
1、創(chuàng)建node1,consul server
[root@local12 ~] ./consul agent -server -bootstrap-expect=1 \ -data-dir=/tmp/consul \ -node=agent-one -bind=192.168.56.112 \ -enable-script-checks=true -config-dir=/etc/consul.d \ -client 0.0.0.0 -ui # -node:節(jié)點(diǎn)的名稱(chēng) # -bind:綁定的一個(gè)地址,用于節(jié)點(diǎn)之間通信的地址,可以是內(nèi)外網(wǎng),必須是可以訪問(wèn)到的地址 # -server:這個(gè)就是表示這個(gè)節(jié)點(diǎn)是個(gè)SERVER # -bootstrap-expect:這個(gè)就是表示期望提供的SERVER節(jié)點(diǎn)數(shù)目,數(shù)目一達(dá)到,它就會(huì)被激活,然后就是LEADER了 # -dc:指明數(shù)據(jù)中心的名字 # -client 0.0.0.0 -ui:啟動(dòng)UI(為了方便后續(xù)的UI訪問(wèn))2、創(chuàng)建node2,consul client
[root@local13 ~] ./consul agent -data-dir=/tmp/consul \ -node=agent-two \ -bind=192.168.56.113 -enable-script-checks=true \ -config-dir=/etc/consul.d \ -ui3、加入集群
[root@local13 ~] ./consul join 192.168.56.112 Successfully joined cluster by contacting 1 nodes. [root@local13 ~] ./consul members Node Address Status Type Build Protocol DC Segment agent-one 192.168.1.13:8301 alive server 1.2.2 2 dc1 <all> agent-two 192.168.1.12:8301 alive client 1.2.2 2 dc1 <default>4、查詢(xún)節(jié)點(diǎn)
[root@local13 ~] dig @127.0.0.1 -p 8600 agent-two.node.consul ... ;; QUESTION SECTION: ;agent-two.node.consul. IN A ;; ANSWER SECTION: agent-two.node.consul. 0 IN A 192.168.1.12KV數(shù)據(jù)
類(lèi)似Redis,一般也就用來(lái)做服務(wù)配置。簡(jiǎn)單了解下命令就好:
consul kv put redis/config/minconns 1 consul kv put redis/config/minconns 2 # 更新 consul kv get redis/config/minconns consul kv delete redis/config/minconns consul kv delete -recurse redis # 批量刪除WEB UI
訪問(wèn)下:http://192.168.56.112:8500/ui
?
總結(jié)
以上是生活随笔為你收集整理的注册中心—组件—Consul的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 注册中心—组件—Eureka
- 下一篇: 滑动窗口—最值问题