consul 重啓後 id 重新生成,導致該consul不能接收服務註冊問題解決。

一、問題

最近測試環境的 consul 發生了重啓

但是重啓之後,發現服務註冊不上去了,查看consul 的日誌,發現了下面的信息。

2021/04/28 06:40:55 [WARN] agent: Syncing service "uat_10.60.6.30:davietest:8092" failed. rpc error making call: failed inserting node: Error while renaming Node ID: "c82f9d74-a4bc-c65e-a2fb-b9bfcec17ad3": Node name uat_37 is reserved by node 3ccb61e1-e0dd-9548-fc41-59de4b9f3991 with name uat_37
2021/04/28 06:40:55 [ERR] agent: failed to sync remote state: rpc error making call: failed inserting node: Error while renaming Node ID: "c82f9d74-a4bc-c65e-a2fb-b9bfcec17ad3": Node name uat_37 is reserved by node 3ccb61e1-e0dd-9548-fc41-59de4b9f3991 with name uat_37

第一眼我也沒看出來什麼異常, 認真一看,就是consul 的 node id 不一致導致的問題。 由於 consul server 異常重啓後,導致 該節點的 node ID 重新生成了。,但是原來的 nodeid 已經在集羣內其他節點未刪除。

二、解決辦法

修改當前異常 consul 的node id 爲 之前的id。

我們看下我們consul 指定的 -data-dir= 路徑是在哪,我們這邊指定的是 /consul/data/ , 在該目錄下可以找到爲我們的文件 node-id

  1. 備份下 node-id 文件
  2. 3ccb61e1-e0dd-9548-fc41-59de4b9f3991 寫入到 node-id 文件,替換老的內容。
  3. 重啓 consul.

三、擴展

註冊服務

接口 http://ip:8500/v1/agent/service/register

請求方式 PUT

請求參數

{
  "ID": "djx-test1-037",
  "Name": "djx-test1",
  "Tags": [],
  "Address": "10.60.7.8",
  "Port": 8000,
  "Meta": {},
  "Weights": {
    "Passing": 10,
    "Warning": 1
  }
}

註銷服務

  1. 去註冊的節點 執行命令 consul services deregister -id djx-test1-037

  2. PUT 請求 http://ip:8500/v1/agent/service/deregister/{ID}

    ip 需要爲註冊的ip.

  3. PUT 請求 /v1/catalog/deregister

    https://www.consul.io/api-docs/catalog

    可以不用在註冊的機器發起註銷。

    {
      "Datacenter": "yx-sz",
      "ServiceID": "djx-test1-037",
      "Node": "uat_hq_37"
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章