Spring Cloud Consul 項目是針對Consul的服務治理實現。Consul是一個分佈式高可用的系統,具備分佈式、高可用、高擴展性。html
Consul 是 HashiCorp 公司推出的開源工具,用於實現分佈式系統的服務發現與配置。與其餘分佈式服務註冊與發現的方案,Consul的方案更「一站式」 ,內置了服務註冊與發現框 架、具備如下性質:java
再也不須要依賴其餘工具(好比ZooKeeper等)。node
使用起來也較 爲簡單。Consul使用Go語言編寫,所以具備自然可移植性(支持Linux、windows和Mac OS X);安裝包僅包含一個可執行文件,方便部署,與Docker等輕量級容器可無縫配合 。 基於 Mozilla Public License 2.0 的協議進行開源. Consul 支持健康檢查,並容許 HTTP 和 DNS 協議調用 API 存儲鍵值對. 一致性協議採用 Raft 算法,用來保證服務的高可用. 使用 GOSSIP 協議管理成員和廣播消息, 而且支持 ACL 訪問控制.linux
使用 Raft 算法來保證一致性, 比複雜的 Paxos 算法更直接. 相比較而言, zookeeper 採用的是 Paxos, 而 etcd 使用的則是 Raft. 支持多數據中心,內外網的服務採用不一樣的端口進行監聽。 多數據中心集羣能夠避免單數據中心的單點故障,而其部署則須要考慮網絡延遲, 分片等狀況等. zookeeper 和 etcd 均不提供多數據中心功能的支持. 支持健康檢查. etcd 不提供此功能. 支持 http 和 dns 協議接口. zookeeper 的集成較爲複雜, etcd 只支持 http 協議. 官方提供web管理界面, etcd 無此功能.nginx
client: 客戶端, 無狀態, 將 HTTP 和 DNS 接口請求轉發給局域網內的服務端集羣.server: 服務端, 保存配置信息, 高可用集羣, 在局域網內與本地客戶端通信, 經過廣域網與其餘數據中心通信. 每一個數據中心的 server 數量推薦爲 3 個或是 5 個.git
因爲Spring Cloud Consul項目的實現,咱們能夠輕鬆的將基於Spring Boot的微服務應用註冊到Consul上,並經過此實現微服務架構中的服務治理。github
參考web
要想利用Consul提供的服務實現服務的註冊與發現,咱們須要搭建Consul Cluster 環境。算法
在Consul方案中,每一個提供服務的節點上都要部署和運行Consul的agent,全部運行Consul agent節點的集合構成Consul Cluster。spring
Consul agent有兩種運行模式:Server和Client。這裏的Server和Client只是Consul集羣層面的區分,與搭建在Cluster之上 的應用服務無關。
以Server模式運行的Consul agent節點用於維護Consul集羣的狀態,官方建議每一個Consul Cluster至少有3個或以上的運行在Server mode的Agent,Client節點不限。
環境配置以下:
Centos 7.3
主機名稱 | IP | 做用 | 是否容許遠程訪問 |
---|---|---|---|
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 agent
catalog Interact with the catalog
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
snapshot Saves, restores and inspects snapshots of Consul server state
validate Validate config files/directories
version Prints the Consul version
watch 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)
複製代碼
命令 | 解釋 | 示例 |
---|---|---|
agent | 運行一個consul agent | consul agent -dev |
join | 將agent加入到consul集羣 | consul join IP |
members | 列出consul cluster集羣中的members | consul members |
leave | 將節點移除所在集羣 | consul leave |
-data-dir
-config-dir
-config-file
-dev
-bootstrap-expect
-node
-bind
Failed to get advertise address: Multiple private IPs found. Please configure one.
的異常-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 Cluster集羣架構圖以下:
這邊準備了三臺Centos 7.3的虛擬機,主機規劃以下,供參考:
主機名稱 | IP | 做用 | 是否容許遠程訪問 |
---|---|---|---|
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;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulApplication {
@RequestMapping("/")
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-client
cloud:
consul:
host: 192.168.252.121
port: 8500
discovery:
healthCheckPath: /
healthCheckInterval: 5s
複製代碼
若是Consul客戶端位於localhost:8500之外的位置,則須要配置來定位客戶端。例:
host: 192.168.252.121
port: 8500
複製代碼
HTTP健康檢查路徑 INSTALL
「10s」和「1m」分別表示10秒和1分
discovery:
healthCheckPath: ${management.context-path}/health
healthCheckInterval: 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…