javascript
Spring Cloud(二) Consul 服务治理实现
Spring Cloud Consul 項目是針對Consul的服務治理實現。Consul是一個分布式高可用的系統,具有分布式、高可用、高擴展性。
Consul 簡介
Consul 是 HashiCorp 公司推出的開源工具,用于實現分布式系統的服務發現與配置。與其他分布式服務注冊與發現的方案,Consul的方案更“一站式” ,內置了服務注冊與發現框 架、具有以下性質:
- 分布一致性協議實現、
- 健康檢查、
- Key/Value存儲、
- 多數據中心方案,
不再需要依賴其他工具(比如ZooKeeper等)。
使用起來也較 為簡單。Consul使用Go語言編寫,因此具有天然可移植性(支持Linux、windows和Mac OS X);安裝包僅包含一個可執行文件,方便部署,與Docker等輕量級容器可無縫配合 。 基于 Mozilla Public License 2.0 的協議進行開源. Consul 支持健康檢查,并允許 HTTP 和 DNS 協議調用 API 存儲鍵值對. 一致性協議采用 Raft 算法,用來保證服務的高可用. 使用 GOSSIP 協議管理成員和廣播消息, 并且支持 ACL 訪問控制.
Consul 的使用場景
- docker 實例的注冊與配置共享
- coreos 實例的注冊與配置共享
- vitess 集群
- SaaS 應用的配置共享
- 與 confd 服務集成,動態生成 nginx 和 haproxy 配置文件
Consul 的優勢
使用 Raft 算法來保證一致性, 比復雜的 Paxos 算法更直接. 相比較而言, zookeeper 采用的是 Paxos, 而 etcd 使用的則是 Raft. 支持多數據中心,內外網的服務采用不同的端口進行監聽。 多數據中心集群可以避免單數據中心的單點故障,而其部署則需要考慮網絡延遲, 分片等情況等. zookeeper 和 etcd 均不提供多數據中心功能的支持. 支持健康檢查. etcd 不提供此功能. 支持 http 和 dns 協議接口. zookeeper 的集成較為復雜, etcd 只支持 http 協議. 官方提供web管理界面, etcd 無此功能.
Consul 的角色
client: 客戶端, 無狀態, 將 HTTP 和 DNS 接口請求轉發給局域網內的服務端集群.server: 服務端, 保存配置信息, 高可用集群, 在局域網內與本地客戶端通訊, 通過廣域網與其他數據中心通訊. 每個數據中心的 server 數量推薦為 3 個或是 5 個.
由于Spring Cloud Consul項目的實現,我們可以輕松的將基于Spring Boot的微服務應用注冊到Consul上,并通過此實現微服務架構中的服務治理。
搭建環境
參考
- Spring Cloud 官方文檔
- Consul 官方文檔
要想利用Consul提供的服務實現服務的注冊與發現,我們需要搭建Consul Cluster 環境。
在Consul方案中,每個提供服務的節點上都要部署和運行Consul的agent,所有運行Consul agent節點的集合構成Consul Cluster。
Consul agent有兩種運行模式:Server和Client。這里的Server和Client只是Consul集群層面的區分,與搭建在Cluster之上 的應用服務無關。
以Server模式運行的Consul agent節點用于維護Consul集群的狀態,官方建議每個Consul Cluster至少有3個或以上的運行在Server mode的Agent,Client節點不限。
環境配置如下:
Centos 7.3
| node1 | 192.168.252.121 | consul server | 是 |
| node2 | 192.168.252.122 | consul client | 是 |
| node3 | 192.168.252.123 | consul client | 是 |
關閉防火墻
systemctl stop firewalld.service 復制代碼Consul 最新版的下載地址:
releases.hashicorp.com/consul/1.0.…
下載,然后unzip 解壓,得到唯一,一個可執行文件
cd /opt/ wget https://releases.hashicorp.com/consul/1.0.1/consul_1.0.1_linux_amd64.zip unzip consul_1.0.1_linux_amd64.zip cp consul /usr/local/bin/ 復制代碼查看是否安裝成功
[root@node1 opt]# consul 復制代碼出現如下結果,表示安裝成功
Usage: consul [--version] [--help] <command> [<args>]Available commands are:agent Runs a Consul agentcatalog Interact with the catalogevent 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.join 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 復制代碼檢查版本
[root@node1 opt]# consul version 復制代碼Consul v1.0.1 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents) 復制代碼Consul常用命令
| agent | 運行一個consul agent | consul agent -dev |
| join | 將agent加入到consul集群 | consul join IP |
| members | 列出consul cluster集群中的members | consul members |
| leave | 將節點移除所在集群 | consul leave |
consul agent 命令的常用選項
-data-dir
- 作用:指定agent儲存狀態的數據目錄
- 這是所有agent都必須的
- 對于server尤其重要,因為他們必須持久化集群的狀態
-config-dir
- 作用:指定service的配置文件和檢查定義所在的位置
- 通常會指定為”某一個路徑/consul.d”(通常情況下,.d表示一系列配置文件存放的目錄)
-config-file
- 作用:指定一個要裝載的配置文件
- 該選項可以配置多次,進而配置多個配置文件(后邊的會合并前邊的,相同的值覆蓋)
-dev
- 作用:創建一個開發環境下的server節點
- 該參數配置下,不會有任何持久化操作,即不會有任何數據寫入到磁盤
- 這種模式不能用于生產環境(因為第二條)
-bootstrap-expect
- 作用:該命令通知consul server我們現在準備加入的server節點個數,該參數是為了延遲日志復制的啟動直到我們指定數量的server節點成功的加入后啟動。
-node
- 作用:指定節點在集群中的名稱
- 該名稱在集群中必須是唯一的(默認采用機器的host)
- 推薦:直接采用機器的IP
-bind
- 作用:指明節點的IP地址
- 有時候不指定綁定IP,會報Failed to get advertise address: Multiple private IPs found. Please configure one. 的異常
-server
- 作用:指定節點為server
- 每個數據中心(DC)的server數推薦至少為1,至多為5
- 所有的server都采用raft一致性算法來確保事務的一致性和線性化,事務修改了集群的狀態,且集群的狀態保存在每一臺server上保證可用性
- server也是與其他DC交互的門面(gateway)
-client
- 作用:指定節點為client,指定客戶端接口的綁定地址,包括:HTTP、DNS、RPC
- 默認是127.0.0.1,只允許回環接口訪問
- 若不指定為-server,其實就是-client
-join
- 作用:將節點加入到集群
-datacenter(老版本叫-dc,-dc已經失效)
- 作用:指定機器加入到哪一個數據中心中
啟動服務
我們嘗試一下:
-dev表示開發模式運行,使用-client 參數可指定允許客戶端使用什么ip去訪問,例如-client 192.168.252.121 表示可以使用
http://192.168.252.121:8500/ui/ 去訪問。
consul agent -dev -client 192.168.252.121 復制代碼Consul 的高可用
Consul Cluster集群架構圖如下:
這邊準備了三臺Centos 7.3的虛擬機,主機規劃如下,供參考:
| node1 | 192.168.252.121 | consul server | 是 |
| node2 | 192.168.252.122 | consul client | 是 |
| node3 | 192.168.252.123 | consul client | 是 |
搭建步驟
命令參數,參看上面詳細介紹
在 node1 機器上啟動 Consul
cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.121 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.121 -server -bootstrap-expect 1 > /dev/null 2>&1 & 復制代碼在 node2 機器上啟動 Consul,并且將node2節點加入到node1節點上
cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.122 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.122 -join=192.168.252.121 > /dev/null 2>&1 & 復制代碼在 node3 機器上啟動 Consul,并且將node3節點加入到node1節點上
cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.123 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.123 -join=192.168.252.121 > /dev/null 2>&1 & 復制代碼在node1上查看當前集群節點:
consul members -rpc-addr=192.168.252.123:8400 consul leave -rpc-addr=192.168.252.123:8400 復制代碼http://192.168.252.121:8500/ui/ 去訪問。
項目示例
新建項目:spring-cloud-consul-client
添加依賴
在項目 spring-cloud-consul-client pom.xml中引入需要的依賴內容:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> 復制代碼開啟服務注冊
客戶端注冊Consul時,它提供有關自身的元數據,如主機和端口,ID,名稱和標簽。默認情況下,將創建一個HTTP 檢查,每隔10秒Consul命中/health端點。如果健康檢查失敗,則服務實例被標記為關鍵。
package io.ymq.example.consul;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; public class ConsulApplication {("/")public String home() {return "Hello world";}public static void main(String[] args) {SpringApplication.run(ConsulApplication.class, args);} } 復制代碼配置文件
在application.yml配置文件中增加如下信息:如果Consul客戶端位于localhost:8500以外,則需要配置來定位客戶端
spring:application:name: consul-clientcloud:consul:host: 192.168.252.121port: 8500discovery:healthCheckPath: /healthCheckInterval: 5s 復制代碼如果Consul客戶端位于localhost:8500以外的位置,則需要配置來定位客戶端。例:
host: 192.168.252.121 port: 8500 復制代碼HTTP健康檢查路徑 INSTALL
“10s”和“1m”分別表示10秒和1分
discovery:healthCheckPath: ${management.context-path}/healthhealthCheckInterval: 15s 復制代碼啟動服務
到spring-cloud-consul-client 項目根目錄下,執行mvn clean package,把target 目錄下 生成的 jar spring-cloud-consul-client-0.0.1-SNAPSHOT.jar 上傳服務器,發布項目
打包命令
mvn clean package 復制代碼發布命令
nohup java -jar spring-cloud-consul-client-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 & 復制代碼訪問服務
http://192.168.252.121:8500/ui/#/dc1/nodes/192.168.252.121
通過上圖HTTP健康檢查,可以看到服務檢測正常
源碼下載
GitHub:github.com/souyunku/sp…
碼云:gitee.com/souyunku/sp…
Contact
- 作者:鵬磊
- 出處:www.ymq.io
- Email:admin@souyunku.com
- 版權歸作者所有,轉載請注明出處
- Wechat:關注公眾號,搜云庫,專注于開發技術的研究與知識分享
總結
以上是生活随笔為你收集整理的Spring Cloud(二) Consul 服务治理实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: node nest 框架学习(一)
- 下一篇: [Erlang 0097] TCP半开的