How to register multiple service instances in consul on one machine

huangapple go评论72阅读模式
英文:

How to register multiple service instances in consul on one machine

问题

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

英文:

I have a consul running locally on a dev machine. I also have one golang service running on two different ports on the same machine. Is there a way to register them as one service but two instances in consul using golang API (for example, is it possible to specify the node name when registering)?

答案1

得分: 1

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

需要注意的关键点是,为了区分在同一代理上运行的my-service的实例A和实例B,这些服务实例还使用唯一的服务ID进行注册。

package main

import (
	"fmt"

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

func main() {
	// 获取一个新的客户端
	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
      }
    ]
  }
]
英文:

Here's a very basic example which registers two instances of a service named my-service. Each instance is configured to listen on a different port, 8080 and 8081 respectively.

The key thing to note is that the service instances are also registered with a unique service ID in order to disambiguate between instance A and instance B of my-service which are running on the same agent.

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)
	}
}

After running go mod init consul-register (or any module name), and executing the code with go run main.go, you can see the service has been registered in the catalog.

$ consul catalog services
consul
my-service

Both service instances are correctly being returned for service discovery queries over DNS or 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
      }
    ]
  }
]

huangapple
  • 本文由 发表于 2021年6月10日 11:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/67914117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定