微服務Consul系列之服務部署、搭建、使用

技術博客: Node.js技術棧

使用Consul解決了哪些問題:

  • 是否在爲不一樣環境來維護不一樣項目配置而發愁
  • 是否有由於配置的更改,致使代碼還要進行修改、發佈,由於客流量大了還要規避開高峯期等到半夜來發布
  • 微服務架構下,應用的分解,業務系統與服務系統之間的調用管理

以上只是列舉的筆者曾經遇到的幾點問題,固然問題還不止於這些,下面介紹的Consul能夠有效解決這些問題,固然還有一些其它的優勢,讓咱們一塊兒期待下文的Consul的講解。html

Consul的四大核心特性:

  • 服務發現:能夠方便的實現服務註冊,經過DNS或者HTTP應用程序能夠很容易的找到他所依賴的服務.
  • Key/Value存儲:使用Key/Value進行數據存儲。
  • 多數據中心:Consul支持開箱即用的多數據中心。這意味着用戶不須要擔憂創建額外的抽象層讓業務擴展到多個區域
  • 健康檢查:能夠對指定服務進行健康檢查例如,Response Status是否爲200,避免將流量轉發到不健康的服務上。

Consul架構

圖片來自官網 Consul Architecturenode

上圖很好的展現了Consul對於多數據中心的支持,另外在兩個數據中心之間只有Service層能夠相互通訊。linux

Consul是一個分佈式高可用的系統,一個發現和配置服務的工具。客戶端能夠利用它提供的API註冊和發現服務,及監控檢測功能實現服務的高可用,深刻的架構描述具體細節,能夠參考官網Consul Architecture,下面開始進入實戰操做部分。git

安裝部署

本處主要以linux來說解,其餘操做平臺見官網Download Consulgithub

下載 wget https://releases.hashicorp.com/consul/1.4.0/consul_1.4.0_linux_amd64.zip json

解壓 unzip consul_1.4.0_linux_amd64.zip獲得目錄consul
複製 consul到你的系統的任何一個地方,若是你想使用命令行直接訪問,確保複製的目錄在你的PATH裏 cp consul /usr/local/bin/bootstrap

驗證consul是否安裝成功,出現如下窗口則安裝成功bash

[root@iZbp1isjfk2rw8fpnxx8wgZ ~]# consul
Usage: consul [--version] [--help] <command> [<args>]

Available commands are:
    acl            Interact with Consul's ACLs
    agent          Runs a Consul agent
    catalog        Interact with the catalog
    connect        Interact with Consul Connect
    debug          Records a debugging archive for operators
    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.
    intention      Interact with Connect service intentions
    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
    services       Interact with services
    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

Consul Agent

執行consul agent -dev ,啓動開發模式,這個模式會快速啓動一個單節點的Consul。注意,這個模式不能數據持久化,所以,不能用於生產環境服務器

啓動命令簡介:

  • -server:定義agent運行在server模式,每一個數據中心的Server建議在3~5個避免失敗狀況下數據的丟失
  • -client:定義agent運行在client模式
  • -bootstrap-expect:在一個datacenter中指望提供的server節點數目,當該值提供的時候,consul一直等到達到指定sever數目的時候纔會引導整個集羣
  • -bind:節點的ip地址通常是0.0.0.0或雲服務內網地址,用於被集羣中的其餘節點所訪問
  • -node:指定節點在集羣中的惟一名稱,默認爲機器的hostname
  • -config-dir:配置文件目錄,裏面全部以.json結尾的文件都會被加載
  • 更多可選參數參考Consul Command-line Options

Start Agent

[root@iZbp1isjfk2rw8fpnxx8wgZ ~]# consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
           Version: 'v1.4.0'
           Node ID: '9e05f4d6-56c1-e57c-c726-15d9ab1c0dd5'
         Node name: 'iZbp1isjfk2rw8fpnxx8wgZ'
        Datacenter: 'dc1' (Segment: '<all>')
            Server: true (Bootstrap: false)
       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

看下 consul agent 輸出的幾個重要信息:架構

  • Node name:代理的惟一名稱,默認是機器的hostname,能夠經過-node標誌自定義,例如:consul agent -dev -node myNode
  • Datacenter:數據中心,Consul支持多個數據中心,爲了有效的工做,每一個節點必須被配置且上報到數據中心,-datacenter標誌用來設置數據中心,對於單一的DC配置,這個代理默認爲dc1
  • Server:表示代理是以服務器仍是客戶端的模式來運行。
  • Client Addr:用於代理的客戶端接口地址。
  • Cluster Addr:用於集羣中的Consul代理之間通訊的地址和端口集,改地址必須可供其它節點訪問。

查看集羣成員

打開一個新終端執行consul members,能夠看到集羣的成員。

圖片描述

  • Node:節點名稱
  • Address:節點地址
  • Status:alive表示節點爲健康狀態
  • Type:節點的運行模式(Server)

Stop Agent

Agent兩種中止方式:gracefullyforcefully
gracefully方式中止,則是發送中斷信號到Agent進程兩種方法:Ctrl+Ckill -INT consul_pid

服務註冊

Consul服務搭建好以後,經過提供服務定義HTTP API註冊一個服務,通用的模式是經過提供服務定義的方式,下文還會介紹怎麼應用Consul進行健康檢查

提供服務定義方式服務註冊

建立目錄/etc/consul.d(.d 後綴意思是這個路徑包含了一組配置文件),Consul會載入該目錄下的全部文件。

例如我如今有個測試服務test01端口爲3010

sudo mkdir /etc/consul.d/test01.json

{
    "service":{
        "name":"test01",
        "tags":[
            "",
            ""
        ],
        "address":"127.0.0.1",
        "port":3010,
        "enable_tag_override": false,
        "check":{
            "deregisterCriticalServiceAfter":"90m",
            "http":"http://127.0.0.1:3010/health",
            "interval":"10s"
        }
    }
}

服務定義配置文件含義:

  • name:服務名
  • tags:服務的tag,自定義,能夠根據這個tag來區分同一個服務名的服務
  • address:服務註冊到consul的IP,服務發現,發現的就是這個IP
  • port:服務註冊consul的PORT,發現的就是這個PORT
  • enable_tag_override:標籤是否容許覆蓋
  • check:健康檢查部分

    • deregisterCriticalServiceAfter
    • http:指定健康檢查的URL,調用後只要返回20X,consul都認爲是健康的
    • interval:健康檢查間隔時間,每隔10s,調用一次上面的URL

重啓Agent設置配置目錄

consul agent -dev -config-dir /etc/consul.d

看如下運行結果:

啓動以後控制檯輸出了Synced service "test01",意思是Agent從配置文件中載入了服務定義,且成功註冊到服務目錄,另外右邊的服務test01也收到了健康檢查接口調用

圖片描述

採用HTTP API服務註冊

調用/v1/agent/service/register接口進行註冊,請求Method爲PUT方式

請求Body值爲服務定義中的service值,看一下示例:

curl -X PUT \
  http://127.0.0.1:8301/v1/agent/service/register \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 6b672c02-350f-3d1c-7793-1a0d9e800fc9' \
  -d '{
    "id": "test01",
    "name":"test01",
    "tags":[
        "",
        ""
    ],
    "address":"127.0.0.1",
    "port":3010,
    "check":{
        "deregisterCriticalServiceAfter":"90m",
        "http":"http://127.0.0.1:3010/health",
        "interval":"10s"
    }
}'

本篇爲微服務架構中基於Consul實現的服務註冊系列文章,目錄以下:

做者:五月君
連接:https://www.imooc.com/article...
來源:慕課網
Github: Node.js技術棧

相關文章
相關標籤/搜索