首页 > 解决方案 > 如何在一台机器上在consul中注册多个服务实例

问题描述

我有一个在开发机器上本地运行的领事。我还有一个 golang 服务在同一台机器上的两个不同端口上运行。有没有办法使用golang API将它们注册为一个服务但在consul中注册两个实例(例如,注册时是否可以指定节点名称)?

标签: goconsul

解决方案


这是一个非常基本的示例,它注册了名为my-service. 每个实例都配置为侦听不同的端口,分别为 8080 和 8081。

需要注意的关键是服务实例也注册了一个唯一的服务 ID,以便消除在my-service同一个代理上运行的实例 A 和实例 B 之间的歧义。

package main

import (
    "fmt"

    "github.com/hashicorp/consul/api"
)

func main() {
    // Get a new client
    client, err := api.NewClient(api.DefaultConfig())
    if err != nil {
        panic(err)
    }

    service_name := "my-service"
    service_ports := [2]int{8080, 8081}

    for idx, port := range service_ports {
        svc_reg := &api.AgentServiceRegistration{
            ID:   fmt.Sprintf("%s-%d", service_name, idx),
            Name: service_name,
            Port: port,
        }

        client.Agent().ServiceRegister(svc_reg)
    }
}

运行go mod init consul-register(或任何模块名称)后,使用 执行代码go run main.go,您可以看到该服务已在目录中注册。

$ consul catalog services
consul
my-service

通过 DNS 或 HTTP 为服务发现查询正确返回了两个服务实例。

$ dig @127.0.0.1 -p 8600 -t SRV my-service.service.consul +short
1 1 8080 b1000.local.node.dc1.consul.
1 1 8081 b1000.local.node.dc1.consul.

$ curl localhost:8500/v1/health/service/my-service 
[
  {
    "Node": {
      "ID": "11113853-a8e0-5787-7482-538078db855a",
      "Node": "b1000.local",
      "Address": "127.0.0.1",
      "Datacenter": "dc1",
      "TaggedAddresses": {
        "lan": "127.0.0.1",
        "lan_ipv4": "127.0.0.1",
        "wan": "127.0.0.1",
        "wan_ipv4": "127.0.0.1"
      },
      "Meta": {
        "consul-network-segment": ""
      },
      "CreateIndex": 11,
      "ModifyIndex": 13
    },
    "Service": {
      "ID": "my-service-0",
      "Service": "my-service",
      "Tags": [],
      "Address": "",
      "Meta": null,
      "Port": 8080,
      "Weights": {
        "Passing": 1,
        "Warning": 1
      },
      "EnableTagOverride": false,
      "Proxy": {
        "Mode": "",
        "MeshGateway": {},
        "Expose": {},
        "TransparentProxy": {}
      },
      "Connect": {},
      "CreateIndex": 14,
      "ModifyIndex": 14
    },
    "Checks": [
      {
        "Node": "b1000.local",
        "CheckID": "serfHealth",
        "Name": "Serf Health Status",
        "Status": "passing",
        "Notes": "",
        "Output": "Agent alive and reachable",
        "ServiceID": "",
        "ServiceName": "",
        "ServiceTags": [],
        "Type": "",
        "Definition": {},
        "CreateIndex": 11,
        "ModifyIndex": 11
      }
    ]
  },
  {
    "Node": {
      "ID": "11113853-a8e0-5787-7482-538078db855a",
      "Node": "b1000.local",
      "Address": "127.0.0.1",
      "Datacenter": "dc1",
      "TaggedAddresses": {
        "lan": "127.0.0.1",
        "lan_ipv4": "127.0.0.1",
        "wan": "127.0.0.1",
        "wan_ipv4": "127.0.0.1"
      },
      "Meta": {
        "consul-network-segment": ""
      },
      "CreateIndex": 11,
      "ModifyIndex": 13
    },
    "Service": {
      "ID": "my-service-1",
      "Service": "my-service",
      "Tags": [],
      "Address": "",
      "Meta": null,
      "Port": 8081,
      "Weights": {
        "Passing": 1,
        "Warning": 1
      },
      "EnableTagOverride": false,
      "Proxy": {
        "Mode": "",
        "MeshGateway": {},
        "Expose": {},
        "TransparentProxy": {}
      },
      "Connect": {},
      "CreateIndex": 15,
      "ModifyIndex": 15
    },
    "Checks": [
      {
        "Node": "b1000.local",
        "CheckID": "serfHealth",
        "Name": "Serf Health Status",
        "Status": "passing",
        "Notes": "",
        "Output": "Agent alive and reachable",
        "ServiceID": "",
        "ServiceName": "",
        "ServiceTags": [],
        "Type": "",
        "Definition": {},
        "CreateIndex": 11,
        "ModifyIndex": 11
      }
    ]
  }
]

推荐阅读