kratos icon indicating copy to clipboard operation
kratos copied to clipboard

[Question] Have considered changing the consul client registration method

Open moecasts opened this issue 3 years ago • 9 comments

现有注册方式

代码

https://github.com/go-kratos/kratos/blob/014778b72a2086ace5eb7bdf3c7bb10205b68110/contrib/registry/consul/client.go#L70-L125

缺点

只注册一个 service 实例,导致 dns 服务发现只能获取到一个服务节点, 比如同时有 http/8000 和 grpc/9000 两个服务,使用 dns 来做服务发现的话,只能查询到 grpc/9000 一个服务:

# dig @172.233.0.2 -p 8600 novel-service.service.consul SRV
;; QUESTION SECTION:
;novel-service.service.consul.	IN	SRV

;; ANSWER SECTION:
novel-service.service.consul. 0	IN	SRV	1 1 9000 ace90004.addr.dc1.consul.

查询 consul 的服务后发现,http/8000 和 grpc/9000 都是被注册到同一个服务里的 ServiceTaggedAddresses 里:

# curl http://172.233.0.2:8500/v1/catalog/service/novel-service
[
    {
        "ID": "32099b46-bdcb-a831-4c7a-64be788f3e6a",
        "Node": "a5158a57e04b",
        "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"
        },
        "NodeMeta": {
            "consul-network-segment": ""
        },
        "ServiceKind": "",
        "ServiceID": "16b4cc7e8a6f",
        "ServiceName": "novel-service",
        "ServiceTags": [
            "version=v1.0.0"
        ],
        "ServiceAddress": "172.233.0.4",
        "ServiceTaggedAddresses": {
            "grpc": {
                "Address": "grpc://172.233.0.4:9000",
                "Port": 9000
            },
            "http": {
                "Address": "http://172.233.0.4:8000",
                "Port": 8000
            },
            "lan_ipv4": {
                "Address": "172.233.0.4",
                "Port": 9000
            },
            "wan_ipv4": {
                "Address": "172.233.0.4",
                "Port": 9000
            }
        },
        "ServiceMeta": {},
        "ServicePort": 9000,
]

期望

将 http/8000 和 grpc/9000 分别注册各自的服务,不再放在同一个服务里,之后就可以正常使用 dns 来做服务发现了。

moecasts avatar Oct 17 '21 07:10 moecasts

novel-service.service.consul 这个是consul的server?这个dns是consul 内置集成的咯?

longXboy avatar Oct 18 '21 07:10 longXboy

@longXboy 是的。novel-service 是服务名,service.consul 是 consul 内置的 dns 域名后缀。

moecasts avatar Oct 18 '21 07:10 moecasts

@longXboy 这个需求主要是为了能配合 apisix 使用 dns 服务发现时指定端口。现在的话默认都是走 9000 端口,代理不了 http 的 8000 端口。 https://apisix.apache.org/zh/docs/apisix/discovery/dns#%E5%9F%BA%E4%BA%8E-dns-%E7%9A%84%E6%9C%8D%E5%8A%A1%E5%8F%91%E7%8E%B0

moecasts avatar Oct 18 '21 07:10 moecasts

同感,ServiceTaggedAddresses 真的不好用,容易带来坑。所以我现在要么复用端口,要么注册成两个服务,服务名里带上协议类型

letian0805 avatar Oct 29 '21 08:10 letian0805

最直观的感受:明明在代码里注册了多个服务,但由于放到了ServiceTaggedAddresses里,consul UI上只能看到一个服务

letian0805 avatar Oct 29 '21 08:10 letian0805

更优雅的方法是:注册为多个节点,每个节点打上scheme=http 这种 tag

letian0805 avatar Oct 29 '21 08:10 letian0805

注册成两个instance通过tag来区分这样不会breakchange,这样如何?

longXboy avatar Dec 17 '21 03:12 longXboy

注册成两个instance通过tag来区分这样不会breakchange,这样如何?

PR #1730 加了个 Filter 解决这个问题

letian0805 avatar Dec 29 '21 16:12 letian0805

@moecasts PR #1730 配合 PR #1729 再加上用 cmux 做端口复用,可以解决你的问题

letian0805 avatar Dec 31 '21 09:12 letian0805

这个问题解决了吗?现在版本为 consul/v2,我遇到了同样的问题,我这使用的 spring boot gateway,然而在注册服务时由于 asr 的 port 优先取了 grpc 的 port,导致 gateway 中取的也是 grpc 的 port,然而我对 spring boot 并没有太多使用经验,我该采取什么措施来避免这个问题?

EyTest-bang avatar May 30 '23 18:05 EyTest-bang

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Is this problem solved? The current version is consul/v2, and I have encountered the same problem. I am using the spring boot gateway. However, when registering the service, the port of asr takes the port of grpc first, so the port of grpc is also taken in the gateway. However, I I don't have much experience with spring boot, what should I do to avoid this problem?

kratos-ci-bot avatar May 30 '23 18:05 kratos-ci-bot

Hi, @moecasts! I'm Dosu, and I'm here to help the kratos team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you are questioning the current consul client registration method in the code. You suggest changing the registration method to register each service separately to enable DNS service discovery for multiple service nodes. There have been discussions and suggestions from other users, such as longXboy and letian0805, on how to address this issue. PR #1730 has been mentioned as a potential solution. Additionally, EyTest-bang has encountered a similar problem and is seeking advice on how to avoid it.

Before we proceed, we would like to confirm if this issue is still relevant to the latest version of the kratos repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution to the kratos project!

dosubot[bot] avatar Nov 03 '23 16:11 dosubot[bot]