1. 程式人生 > >Centos7 安裝 consul 1.4.0 替換 spring cloud eureka

Centos7 安裝 consul 1.4.0 替換 spring cloud eureka

eureka官方已經正式宣佈:自2.0起不再維護該專案,並在github 專案wiki上放出了一段嚇唬人的話:

點選檢視原圖

大意就是:從2.x起,官方不會繼續開發了,如果需要使用2.x,風險自負。但其實我覺得問題並不大,eureka目前的功能已經非常穩定,就算不升級,服務註冊/發現這些功能已經夠用。

如果想尋找替代方案的話,建議採用功能更為豐富的consul,除了服務註冊、發現,consul還提供了k-v儲存等其它功能,consul的官網針對其它同類軟體也做了詳細比較,詳見 consul vs other software,有興趣的可以看看,特別是有一句話,翻譯成大白話就是:我不是針對在座的各位,我想說除我之外其它的都是渣渣(足見其相當的自信!)

點選檢視原圖

進入正題,先來看consul的部署安裝:

一、叢集規劃

consul藉助agent來執行,類似elk的logstash agent 或 zabbix監控系統的agent ,每個需要被發現的服務上,通過consul agent client 來收集服務本身的資訊,然後向consul agent server彙報, consul server 可以叢集部署。

規劃一下:

序號   節點ip 節點名稱   角色
1  10.0.xx.55  server1  server
2  10.0.xx.203  server2  server
3  10.0.xx.204  server3  server
4  10.0.xx.205  client1  client & web ui

這是按正式生產環境來規劃的,如果本機開發,有一個方便的dev模式(後面再講)。 上述表格中,我們打算組建3個server節點的consul server cluster,另外有1個client,模擬客戶端以及充當consul的web admin ui(管理介面)。

 

二、下載安裝

目前consul的最高版本為1.4.0(本文用1.2.0部署),只需要把相應的release壓縮包 下載到機器上解壓即可。

# wget https://releases.hashicorp.com/consul/1.2.0/consul_1.2.0_linux_amd64.zip

# unzip consul_1.2.0_linux_amd64.zip

假設都解壓到~/consul/bin目錄下,解壓後會得到1個名為consul的可執行檔案

為了方便,可以將其複製到/usr/local/bin下(本步驟可選,需要root許可權)

# cp ./consul /usr/local/bin

然後檢查下是否安裝成功:

# consul version

Consul v1.2.0

Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

如果出現版本輸出,就表示可以了(4個節點上,每臺機器上都重複上述操作,全安裝好)

三、啟動

3.1 啟動server端

基本的命令為:

# consul agent -server -bind=10.0.xx.55 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/data/application/consul_data/ -node=server1

看著一大堆引數,其實不復雜

-server 表示是以服務端身份啟動

-bind 表示繫結到哪個ip(有些伺服器會繫結多塊網絡卡,可以通過bind引數強制指定繫結的ip)

-client 指定客戶端訪問的ip(consul有豐富的api介面,這裡的客戶端指瀏覽器或呼叫方),0.0.0.0表示不限客戶端ip

-bootstrap-expect=3 表示server叢集最低節點數為3,低於這個值將工作不正常(注:類似zookeeper一樣,通常叢集數為奇數,方便選舉,consul採用的是raft演算法)

-data-dir 表示指定資料的存放目錄(該目錄必須存在)

-node 表示節點在web ui中顯示的名稱

啟動成功後,終端視窗不要關閉,可以在瀏覽器裡,訪問下,類似 http://10.0.xx.55:8500/,正常的話,應該會看到一行文字:Consul Agent。

為了防止終端關閉後,consul退出,可以在剛才命令上,加點東西,類似:
nohup xxx  > /dev/null 2>&1 & 

即:

# nohup consul agent -server -bind=10.0.xx.55 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/data/application/consul_data/ -node=server1 > /dev/null 2>&1 &

將其轉入後臺執行。

另外2臺節點上,也做類似操作:

# nohup consul agent -server -bind=10.0.xx.203 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/data/application/consul_data/ -node=server2 > /dev/null 2>&1 &

注意更改bind引數的ip,以及node引數裡的節點名稱。

# nohup consul agent -server -bind=10.0.xx.204 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/data/application/consul_data/ -node=server3 > /dev/null 2>&1 &

3.2 啟動client端

幾乎完全一樣,只是把-server 去掉,在10.0.xx.205上執行:

# nohup consul agent -client=0.0.0.0 -data-dir=/data/application/consul_data/ -node=client1  -ui  > /dev/null 2>&1 &

四、組建cluster

現在我們有了3個server node + 1個client node,但是這4個節點都是相互獨立的,可以在任一節點上執行:

# consul members

可以看到,只有自身節點的資訊。

要加自己加入叢集,可以執行以下命令(假設:其它3個節點,都加入10.0.xx.55)

# consul join 10.0.xx.55

成功後,會輸出:

Successfully joined cluster by contacting 1 nodes.

其它2個節點(指:10.0.xx.55之外的節點)上類似上述操作,都加入叢集中,完成後,可以再次驗證

可以看到4臺節點的資訊都有了。

tips: 如果反過來,要將1個節點從叢集中撤掉,可以在該節點上執行

# consul leave

即可。

五、web ui

10.0.xx.205上,可能有同學注意到了,啟動consul時,我們加了一個  -ui 的引數,這代表將啟動consul自帶的web管理介面,訪問 http://10.0.xx.205:8500/ui

可以看到各節點的資訊。

六、服務註冊/發現/登出

現在services裡,除了consul外,並沒有其它服務註冊進來。可以通過api 手動註冊一個服務:

用postman(或其它rest api工具、curl都行),向 http://10.0.xx.55:8500/v1/agent/service/register ,傳送以下json,http method指定為PUT, Content-Type指定為application/json

{

    "ID": "my-service-id",

    "Name": "my-service-name",

    "Tags": [

        "release=1",

        "mytag=xyz"

    ],

    "Address": "192.168.1.1",

    "Port": 8000,

    "Meta": {

        "my_version": "4.0"

    },

    "EnableTagOverride": false,

    "Check": {

        "DeregisterCriticalServiceAfter": "90m",

        "HTTP": "http://yjmyzz.cnblogs.com/",

        "Interval": "10s"

    }

} 

其中的引數名一看就能大概猜出含義,就不詳細解釋了,想深入研究的,建議參看consul api文件。傳送完成後,再看web ui

就能看到新註冊的服務my-service-name,特別要提一下的是tags,eureka中有metadata-map可以提供元資料,consul中對應的特性為tags。

除了web ui,還可以通過rest介面,發現服務的詳細資訊:

http://10.0.21.55:8500/v1/agent/services 這個rest api可以列出所有服務:

 

{

    "my-service-id": {

        "Kind": "",

        "ID": "my-service-id",

        "Service": "my-service-name",

        "Tags": [

            "release=1",

            "mytag=xyz"

        ],

        "Meta": {

            "my_version": "4.0"

        },

        "Port": 8000,

        "Address": "192.168.1.1",

        "EnableTagOverride": false,

        "CreateIndex": 0,

        "ModifyIndex": 0,

        "ProxyDestination": "",

        "Connect": null

    }

}

服務發現,其實就是通過這個api實現的,有服務註冊就會有反向操作:服務登出。同樣也是通過api完成

# curl -X PUT http://10.0.21.55:8500/v1/agent/service/deregister/my-service-id

這樣就把my-service-id這個服務給登出了

七、dev開發者模式

前面這一陣折騰需要好幾臺機器,本機除錯開發時不太方便,為此consul體貼的提供了dev模式,使用方式極為簡單

# consul agent -dev

開發模式,自帶了web ui,直接http://localhost:8500/ 即可,非常方便。  

八、與spring-cloud的整合

前面這些鋪墊,其實就是為了與spring-cloud的整合,非常簡單:

compile('org.springframework.cloud:spring-cloud-starter-consul-discovery')

新增對org.springframework.cloud:spring-cloud-starter-consul-discovery的依賴

然後application.yml中配置如下節點:

spring:
  ...
  cloud:
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:
        tags: version=1.0,author=yjmyzz
        healthCheckPath: /info.json
        healthCheckInterval: 5s
        instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

注:

一般在需要部署的微服務機器上,都約定安裝consul agent,所以這裡host通常指定為127.0.0.1 (本機測試consul可用dev模式啟動);

tags 相當於eureka裡的metadata-map,大家根據實際需要設定

healthCheckPath 為健康檢查的url,可以配置成/health或其它能檢測出微服務執行狀態的url

healthCheckInterval 為healthcheck的時間間隔

instanceId這一長串,spring cloud官網文件的推薦,為了保證生成一個唯一的id ,也可以換成

${spring.application.name}:${spring.cloud.client.ipAddress}

(即:以ip結尾),這個instanceId即為consul service列表中的service id

最後提醒一下:如果使用consul來替換eureka,而你的專案中又依賴了eureka的jar包,最好將eureka的自動配置從啟動類裡排除掉,參考下面:

注:如果專案的pom/gradle依賴項裡,已經移除了org.springframework.cloud:spring-cloud-starter-eureka-server 就不用exclude排除EurekaClientAutoConfiguration,另外上圖中@EnableEurekaClient 也可以換成@EnableDiscoveryClient

 

參考文件:
1、https://www.consul.io/api/index.html

2、https://www.consul.io/intro/getting-started/install.html

3、https://cloud.spring.io/spring-cloud-static/spring-cloud-consul/1.3.3.RELEASE/multi/multi_spring-cloud-consul-discovery.html