登录
首页 >  Golang >  Go问答

如何在单个主机上注册多个 Consul 服务实例

来源:stackoverflow

时间:2024-02-22 09:27:23 341浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《如何在单个主机上注册多个 Consul 服务实例》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

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


正确答案


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

需要注意的关键一点是,服务实例还注册了唯一的 service 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
      }
    ]
  }
]

本篇关于《如何在单个主机上注册多个 Consul 服务实例》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>