Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hertz服务发现组件中, withTags()是不是没有实际用处 #596

Closed
dengWuuu opened this issue Feb 6, 2023 · 6 comments
Closed
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@dengWuuu
Copy link

dengWuuu commented Feb 6, 2023

Describe the Question

服务发现Nacos组件中,请求的时候带上withTags()发现没什么用似乎,请求也不会都发送到带上tag的那个hertz服务器。

Reproducible Code

client代码

import (
	"context"
	"fmt"
	"github.com/cloudwego/hertz/pkg/app/client"
	"github.com/cloudwego/hertz/pkg/app/client/discovery"
	"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd"
	"github.com/cloudwego/hertz/pkg/common/config"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/hertz-contrib/registry/nacos"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

func main() {
	sc := []constant.ServerConfig{
		*constant.NewServerConfig("127.0.0.1", 8848),
	}
	cc := constant.ClientConfig{
		NamespaceId:         "public",
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "info",
	}
	nacosCli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		})
	if err != nil {
		panic(err)
	}
	r := nacos.NewNacosResolver(nacosCli)
	discoveryWithTag(r)
}

func discoveryWithSD(r discovery.Resolver) {
	fmt.Println("simply discovery:")
	cli, err := client.NewClient()
	if err != nil {
		panic(err)
	}
	cli.Use(sd.Discovery(r))
	for i := 0; i < 10; i++ {
		status, body, err := cli.Get(context.Background(), nil, "http://hertz.custom-config.demo/ping", config.WithSD(true))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s", status, string(body))
	}
}

func discoveryWithTag(r discovery.Resolver) {
	fmt.Println("discovery with tag:")
	cli, err := client.NewClient()
	if err != nil {
		panic(err)
	}
	cli.Use(sd.Discovery(r))
	for i := 0; i < 10; i++ {
		status, body, err := cli.Get(context.Background(), nil, "http://hertz.custom-config.demo/ping",
			config.WithSD(true),
			config.WithTag("key1", "val1"))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s", status, string(body))
	}
}


server代码

import (
	"context"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/app/server/registry"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/hertz-contrib/registry/nacos"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

import (
	"sync"
)

var (
	wg        sync.WaitGroup
	server1IP = "127.0.0.1:8088"
	server2IP = "127.0.0.1:8089"
)

func main() {
	sc := []constant.ServerConfig{
		*constant.NewServerConfig("127.0.0.1", 8848),
	}

	cc := constant.ClientConfig{
		NamespaceId:         "public",
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "info",
	}

	cli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		},
	)
	if err != nil {
		panic(err)
	}
	wg.Add(2)
	r := nacos.NewNacosRegistry(cli)
	go func() {
		defer wg.Done()
		h := server.Default(
			server.WithHostPorts(server1IP),
			server.WithRegistry(r, &registry.Info{
				ServiceName: "hertz.custom-config.demo",
				Addr:        utils.NewNetAddr("tcp", server1IP),
				Weight:      10,
				Tags: map[string]string{
					"key1": "val1",
				},
			}))
		h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
			ctx.JSON(consts.StatusOK, utils.H{"ping1": "pong1"})
		})
		h.Spin()
	}()

	go func() {
		defer wg.Done()
		h := server.Default(
			server.WithHostPorts(server2IP),
			server.WithRegistry(r, &registry.Info{
				ServiceName: "hertz.custom-config.demo",
				Addr:        utils.NewNetAddr("tcp", server2IP),
				Weight:      10,
				Tags: map[string]string{
					"key2": "val2",
				},
			}))
		h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
			ctx.JSON(consts.StatusOK, utils.H{"ping2": "pong2"})
		})
		h.Spin()

	}()
	wg.Wait()
}

Expected behavior
我以为是都转发到对应tag的服务器
但是结果如下图
image

Hertz version:

Please provide the version of Hertz you are using.

Environment:
mod文件如下图

module github.com/hertz-contrib/registry/nacos

go 1.16

require (
	github.com/cloudwego/hertz v0.2.2-0.20220819075506-6fff4a1e7a9c
	github.com/nacos-group/nacos-sdk-go v1.1.2
	github.com/stretchr/testify v1.7.0
)
@L2ncE
Copy link
Contributor

L2ncE commented Feb 6, 2023

我看到你使用的 Nacos,在 Nacos 的配置中,info.Tags 会配置为 Nacos 的 Metadata,类似的在 Consul 中也是把 Tags 配置成的 Metadata,原因在于拓展许多都并没有自定义 Info 而是复用的 Hertz 中的 Info 导致一些配置项并不匹配。后面有时间我会统一修改一下。

@dengWuuu
Copy link
Author

dengWuuu commented Feb 6, 2023

okkkkkkkk 了解了

@li-jin-gou li-jin-gou added the enhancement New feature or request label Feb 6, 2023
@L2ncE L2ncE self-assigned this Feb 6, 2023
@kolinfluence
Copy link

very useful. the more u use hertz, the more u'll know withtag is very useful.

@L2ncE L2ncE removed their assignment Mar 21, 2023
@L2ncE L2ncE added the good first issue Good for newcomers label Mar 21, 2023
@L2ncE
Copy link
Contributor

L2ncE commented Mar 21, 2023

Hello, you can try to change it yourself first, and I will help code review.

@dengWuuu
Copy link
Author

Hello, you can try to change it yourself first, and I will help code review.

oh! i will try to fix it

@HeyJavaBean
Copy link
Member

cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Development

No branches or pull requests

5 participants