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

一个程序创建多个客户端不生效 #107

Open
kingkongpoon opened this issue Jan 19, 2024 · 13 comments
Open

一个程序创建多个客户端不生效 #107

kingkongpoon opened this issue Jan 19, 2024 · 13 comments
Assignees
Labels
question Further information is requested

Comments

@kingkongpoon
Copy link

kingkongpoon commented Jan 19, 2024

想在一个go程序里面,同时访问不同的nacos/consul里面的不同服务。后初始化调用的,不能正常使用。
代码简单表示了一下问题

nacos1 192.168.56.1:8848  服务名 server11111
nacos2 192.168.56.2:8848  服务名 server22222
import (
	"context"
	"github.com/cloudwego/hertz/pkg/app/client"
	"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"
	"log"
)

func main() {
	test1()
	test2()
}

func test1() {

	nacosClient, err := client.NewClient()
	if err != nil {
		panic(err)
	}

	cc := constant.ClientConfig{}
	sc := constant.ServerConfig{}

	sc.IpAddr = "192.168.56.1"
	sc.Port = 8848

	var scl []constant.ServerConfig

	scl = append(scl, sc)

	nacosCli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: scl,
		},
	)

	if err != nil {
		log.Fatal(err)
		return
	}

	r := nacos.NewNacosResolver(nacosCli)
	nacosClient.Use(sd.Discovery(r))

	for i := 0; i < 10; i++ {
		status, body, err := nacosClient.Get(context.Background(), nil, "http://server11111/index", config.WithSD(true))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s\n", status, string(body))
	}
}

func test2() {

	nacosClient, err := client.NewClient()
	if err != nil {
		panic(err)
	}
	cc := constant.ClientConfig{}
	sc := constant.ServerConfig{}

	sc.IpAddr = "192.168.56.2"
	sc.Port = 8848

	var scl []constant.ServerConfig

	scl = append(scl, sc)

	nacosCli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: scl,
		},
	)

	if err != nil {
		log.Fatal(err)
		return
	}

	r := nacos.NewNacosResolver(nacosCli)

	nacosClient.Use(sd.Discovery(r))

	for i := 0; i < 10; i++ {
		status, body, err := nacosClient.Get(context.Background(), nil, "http://server22222/index", config.WithSD(true))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s\n", status, string(body))
	}
}
运行结果,第一个test1()运行能正常获取数据,test2()运行报错
 nacos-client.go:57: [Fatal] instance list is empty!
@li-jin-gou
Copy link
Collaborator

cc @Skyenought

@Skyenought
Copy link
Member

server 在注册时有将多个 nacos 地址配置进入 ServerConfigs 吗?

@li-jin-gou li-jin-gou added the question Further information is requested label Jan 21, 2024
@kingkongpoon
Copy link
Author

kingkongpoon commented Jan 22, 2024

@Skyenought
server端是2个服务,注册在不同的nacos上面,现在是想通过1个client端,向2个nacos(上的服务)请求数据。
test1,test2各自运行都是正常的,一起先后执行,后执行的就不能正常使用。
image

@Skyenought
Copy link
Member

我的代码无论是在 server 向一个 nacos 注册, 还是两个 nacos 注册,并没有出现复现相关的问题
请尽量给我一个方便复现的 demo ?

server.go

package main

import (
	"context"
	"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"
	"log"
	"sync"

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

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

func main() {
	wg.Add(2)
	go server1()
	go server2()

	wg.Wait()
}

func server1() {
	defer wg.Done()
	sc := []constant.ServerConfig{
		*constant.NewServerConfig("127.0.0.1", 8848),
		*constant.NewServerConfig("127.0.0.1", 8849),
	}
	cc := constant.ClientConfig{
		NotLoadCacheAtStart: true,
	}
	client, _ := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		},
	)
	r := nacos.NewNacosRegistry(client)
	h := server.Default(
		server.WithHostPorts(server1IP),
		server.WithRegistry(r, &registry.Info{
			ServiceName: "hertz.test.demo",
			Addr:        utils.NewNetAddr("tcp", server1IP),
			Weight:      10,
			Tags:        nil,
		}),
	)
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(consts.StatusOK, utils.H{"ping1": "pong1"})
	})
	h.Spin()
}

func server2() {
	defer wg.Done()
	sc2 := []constant.ServerConfig{
		*constant.NewServerConfig("127.0.0.1", 8849),
		*constant.NewServerConfig("127.0.0.1", 8848),
	}
	cc := constant.ClientConfig{
		NotLoadCacheAtStart: true,
	}
	client2, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc2,
		},
	)
	r2 := nacos.NewNacosRegistry(client2)
	if err != nil {
		log.Fatal(err)
	}
	h := server.Default(
		server.WithHostPorts(server2IP),
		server.WithRegistry(r2, &registry.Info{
			ServiceName: "hertz.test.demo2",
			Addr:        utils.NewNetAddr("tcp", server2IP),
			Weight:      10,
			Tags:        nil,
		}),
	)
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(consts.StatusOK, utils.H{"ping2": "pong2"})
	})
	h.Spin()
}

client.go

package main

import (
	"context"
	"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"
	"log"

	"github.com/cloudwego/hertz/pkg/app/client"
	"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"
)

func main() {
	test1()
	test2()
}

func test1() {

	nacosClient, err := client.NewClient()
	if err != nil {
		panic(err)
	}

	cc := constant.ClientConfig{}
	sc := constant.ServerConfig{}

	sc.IpAddr = "127.0.0.1"
	sc.Port = 8848

	var scl []constant.ServerConfig

	scl = append(scl, sc)

	nacosCli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: scl,
		},
	)

	if err != nil {
		log.Fatal(err)
		return
	}

	r := nacos.NewNacosResolver(nacosCli)
	nacosClient.Use(sd.Discovery(r))

	for i := 0; i < 10; i++ {
		status, body, err := nacosClient.Get(context.Background(), nil, "http://hertz.test.demo/ping", config.WithSD(true))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s\n", status, string(body))
	}
}

func test2() {
	nacosClient, err := client.NewClient()
	if err != nil {
		panic(err)
	}
	cc := constant.ClientConfig{}
	sc := constant.ServerConfig{}

	sc.IpAddr = "127.0.0.1"
	sc.Port = 8849

	var scl []constant.ServerConfig

	scl = append(scl, sc)

	nacosCli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: scl,
		},
	)

	if err != nil {
		log.Fatal(err)
		return
	}

	r := nacos.NewNacosResolver(nacosCli)

	nacosClient.Use(sd.Discovery(r))

	for i := 0; i < 10; i++ {
		status, body, err := nacosClient.Get(context.Background(), nil, "http://hertz.test.demo2/ping", config.WithSD(true))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s\n", status, string(body))
	}
}

@kingkongpoon
Copy link
Author

你好,感谢你的回复。
按你现在的代码,注册到nacos的形式是
image
所以client端访问,是没问题的。不过不是我想用的效果。

你把
server.go的

func server1() {
	defer wg.Done()
	sc := []constant.ServerConfig{
		*constant.NewServerConfig("127.0.0.1", 8848),
		*constant.NewServerConfig("127.0.0.1", 8849),
	}

改为

func server1() {
	defer wg.Done()
	sc := []constant.ServerConfig{
		*constant.NewServerConfig("127.0.0.1", 8848),
	}
func server2() {
	defer wg.Done()
	sc2 := []constant.ServerConfig{
		*constant.NewServerConfig("127.0.0.1", 8849),
		*constant.NewServerConfig("127.0.0.1", 8848),
	}

改为

func server2() {
	defer wg.Done()
	sc2 := []constant.ServerConfig{
		*constant.NewServerConfig("127.0.0.1", 8849),
	}

client.go的不用改,才是我想测的东西。

然后,其实按我上面的代码,是有时能成功的,不过90%概率不成功。本地会有个cache的文件夹,里面有个naming的缓存文件。
应该和constant包下的

	NotLoadCacheAtStart  bool                   // not to load persistent nacos service info in CacheDir at start time
	UpdateCacheWhenEmpty bool                   // update cache when get empty service instance from server

这两个属性有关,就是读不读缓存文件。

我对比了几次
能成功分别访问的时候,naming里面会有每个server服务的信息
server11111

文件名 DEFAULT_GROUP@@server11111@@DEFAULT

内容
{
    "dom":"",
    "cacheMillis":10000,
    "useSpecifiedUrl":false,
    "hosts":[
        {
            "valid":false,
            "marked":false,
            "instanceId":"",
            "port":10265,
            "ip":"172.30.0.1",
            "weight":1,
            "metadata":{
                "preserved.register.source":"SPRING_CLOUD"
            },
            "clusterName":"DEFAULT",
            "serviceName":"DEFAULT_GROUP@@server11111",
            "enabled":true,
            "healthy":true,
            "ephemeral":true
        }
    ],
    "checksum":"",
    "lastRefTime":1705660387987,
    "env":"",
    "clusters":"DEFAULT",
    "metadata":null,
    "name":"DEFAULT_GROUP@@server11111"
}

server22222

另外一个,我把服务名字定义为了server22222
文件名自动生成会是  DEFAULT_GROUP@@server22222@@DEFAULT

不能访问的时候,内容是
{
    "dom":"",
    "cacheMillis":10000,
    "useSpecifiedUrl":false,
    "hosts":[
    ],
    "checksum":"",
    "lastRefTime":1705660387987,
    "env":"",
    "clusters":"DEFAULT",
    "metadata":null,
    "name":"DEFAULT_GROUP@@server22222"
}

能访问的时候,内容是
{
    "dom":"",
    "cacheMillis":10000,
    "useSpecifiedUrl":false,
    "hosts":[
        {
            "valid":false,
            "marked":false,
            "instanceId":"",
            "port":10360,
            "ip":"172.30.0.1",
            "weight":1,
            "metadata":{
                "preserved.register.source":"SPRING_CLOUD"
            },
            "clusterName":"DEFAULT",
            "serviceName":"DEFAULT_GROUP@@server22222",
            "enabled":true,
            "healthy":true,
            "ephemeral":true
        }
    ],
    "checksum":"",
    "lastRefTime":1705660387987,
    "env":"",
    "clusters":"DEFAULT",
    "metadata":null,
    "name":"DEFAULT_GROUP@@server22222"
}

@Skyenought
Copy link
Member

我理解你的意思, 我在使用你预期方式还是无法复现😭
要不你把下面的 client config 设置成 true 看看? 你得保证在请求的时候, nacos 里的 instance 是 health 的

	NotLoadCacheAtStart  bool                 
	UpdateCacheWhenEmpty bool   

@kingkongpoon
Copy link
Author

kingkongpoon commented Jan 24, 2024

你好,我用你的代码,依然能复现。我一个server只注册到一个nacos。
我贴一下按你的代码,改成我的nacos的ip和port。

package main

import (
	"context"
	"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"
	"log"
	"sync"

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

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

func main() {
	wg.Add(2)
	go server1()
	go server2()

	wg.Wait()
}

func server1() {
	defer wg.Done()
	sc := []constant.ServerConfig{
		*constant.NewServerConfig("192.168.56.108", 8848),  //这个只注册到一个nacos
	}
	cc := constant.ClientConfig{
		NotLoadCacheAtStart: true,
	}
	client, _ := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		},
	)
	r := nacos.NewNacosRegistry(client)
	h := server.Default(
		server.WithHostPorts(server1IP),
		server.WithRegistry(r, &registry.Info{
			ServiceName: "hertz.test.demo",
			Addr:        utils.NewNetAddr("tcp", server1IP),
			Weight:      10,
			Tags:        nil,
		}),
	)
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(consts.StatusOK, utils.H{"ping1": "pong1"})
	})
	h.Spin()
}

func server2() {
	defer wg.Done()
	sc2 := []constant.ServerConfig{
		*constant.NewServerConfig("192.168.56.108", 18848),    //这个只注册到一个nacos
	}
	cc := constant.ClientConfig{
		NotLoadCacheAtStart: true,
	}
	client2, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc2,
		},
	)
	r2 := nacos.NewNacosRegistry(client2)
	if err != nil {
		log.Fatal(err)
	}
	h := server.Default(
		server.WithHostPorts(server2IP),
		server.WithRegistry(r2, &registry.Info{
			ServiceName: "hertz.test.demo2",
			Addr:        utils.NewNetAddr("tcp", server2IP),
			Weight:      10,
			Tags:        nil,
		}),
	)
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(consts.StatusOK, utils.H{"ping2": "pong2"})
	})
	h.Spin()
}

第一个nacos 192.168.56.108:8848
image
第二个nacos 192.168.56.108:18848
image

client代码也是复制你的,改了一下nacos配置

package main

import (
	"context"
	"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"
	"log"

	"github.com/cloudwego/hertz/pkg/app/client"
	"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"
)

func main() {
	ttest1()
	ttest2()
}

func ttest1() {

	nacosClient, err := client.NewClient()
	if err != nil {
		panic(err)
	}

	cc := constant.ClientConfig{
		NotLoadCacheAtStart:  true,  //加了没用
		UpdateCacheWhenEmpty: true,  //加了没用
	}
	sc := constant.ServerConfig{}

	sc.IpAddr = "192.168.56.108"  //改了地址
	sc.Port = 8848                //改了端口

	var scl []constant.ServerConfig

	scl = append(scl, sc)

	nacosCli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: scl,
		},
	)

	if err != nil {
		log.Fatal(err)
		return
	}

	r := nacos.NewNacosResolver(nacosCli)
	nacosClient.Use(sd.Discovery(r))

	for i := 0; i < 10; i++ {
		status, body, err := nacosClient.Get(context.Background(), nil, "http://hertz.test.demo/ping", config.WithSD(true))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s\n", status, string(body))
	}
}

func ttest2() {
	nacosClient, err := client.NewClient()
	if err != nil {
		panic(err)
	}
	cc := constant.ClientConfig{
		NotLoadCacheAtStart:  true,  //加了没用
		UpdateCacheWhenEmpty: true,  //加了没用
	}
	sc := constant.ServerConfig{}

	sc.IpAddr = "192.168.56.108"   //改了地址
	sc.Port = 18848                //改了端口

	var scl []constant.ServerConfig

	scl = append(scl, sc)

	nacosCli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: scl,
		},
	)

	if err != nil {
		log.Fatal(err)
		return
	}

	r := nacos.NewNacosResolver(nacosCli)

	nacosClient.Use(sd.Discovery(r))

	for i := 0; i < 10; i++ {
		status, body, err := nacosClient.Get(context.Background(), nil, "http://hertz.test.demo2/ping", config.WithSD(true))
		if err != nil {
			hlog.Fatal(err)
		}
		hlog.Infof("code=%d,body=%s\n", status, string(body))
	}
}

先清理了cache目录,这里因为server配置不会变,所以每次执行都要先清理这个目录。
直接运行,结果

2024/01/24 10:03:10.901454 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.901607 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.902284 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.902284 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.902284 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.902807 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.902807 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.903343 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.903594 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.903594 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:03:10.913110 c1.go:103: [Fatal] instance list is empty!

注释掉ttest2(),运行结果

2024/01/24 10:13:13.946647 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.947177 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.947732 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.947732 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.947732 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.948309 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.949486 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.949486 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.949486 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:13:13.949486 c1.go:62: [Info] code=200,body={"ping1":"pong1"}

注释掉ttest1(),运行结果

2024/01/24 10:13:43.861539 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.862093 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.862093 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.862093 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.862093 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.862640 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.862640 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.862640 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.862640 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:13:43.863174 c1.go:105: [Info] code=200,body={"ping2":"pong2"}

单独运行都是正常的。
2个测试方法一起执行,又变回ttest2执行出问题。

2024/01/24 10:14:19.213867 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.213867 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.213867 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.214482 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.214482 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.214482 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.214482 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.215027 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.215027 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.215744 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:14:19.221665 c1.go:103: [Fatal] instance list is empty!      

此时cache目录会自动生成
image
cache/DEFAULT_GROUP@@hertz.test.demo@@default

{
    "dom":"",
    "cacheMillis":10000,
    "useSpecifiedUrl":false,
    "hosts":[
        {
            "valid":false,
            "marked":false,
            "instanceId":"127.0.0.1#8088#DEFAULT#DEFAULT_GROUP@@hertz.test.demo",
            "port":8088,
            "ip":"127.0.0.1",
            "weight":10,
            "metadata":{

            },
            "clusterName":"DEFAULT",
            "serviceName":"DEFAULT_GROUP@@hertz.test.demo",
            "enabled":true,
            "healthy":true,
            "ephemeral":true
        }
    ],
    "checksum":"",
    "lastRefTime":1706062458175,
    "env":"",
    "clusters":"DEFAULT",
    "metadata":null,
    "name":"DEFAULT_GROUP@@hertz.test.demo"
}

cache/DEFAULT_GROUP@@hertz.test.demo2@@default

{
    "dom":"",
    "cacheMillis":10000,
    "useSpecifiedUrl":false,
    "hosts":[

    ],
    "checksum":"",
    "lastRefTime":1706062458183,
    "env":"",
    "clusters":"DEFAULT",
    "metadata":null,
    "name":"DEFAULT_GROUP@@hertz.test.demo2"
}

按你的代码,也是能复现。

我按DEFAULT_GROUP@@hertz.test.demo@@default的内容修改了DEFAULT_GROUP@@hertz.test.demo2@@default

{
    "dom":"",
    "cacheMillis":10000,
    "useSpecifiedUrl":false,
    "hosts":[
        {
            "valid":false,
            "marked":false,
            "instanceId":"127.0.0.1#8089#DEFAULT#DEFAULT_GROUP@@hertz.test.demo2",
            "port":8089,
            "ip":"127.0.0.1",
            "weight":10,
            "metadata":{

            },
            "clusterName":"DEFAULT",
            "serviceName":"DEFAULT_GROUP@@hertz.test.demo2",
            "enabled":true,
            "healthy":true,
            "ephemeral":true
        }
    ],
    "checksum":"",
    "lastRefTime":1706063442231,
    "env":"",
    "clusters":"DEFAULT",
    "metadata":null,
    "name":"DEFAULT_GROUP@@hertz.test.demo2"
}

然后注释掉这个配置,就可以正常访问

		//NotLoadCacheAtStart:  true, 
		//UpdateCacheWhenEmpty: true, 
2024/01/24 10:32:32.065455 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.066431 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.067409 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.068385 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.068385 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.069364 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.069364 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.070339 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.070339 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.070339 c1.go:62: [Info] code=200,body={"ping1":"pong1"}
2024/01/24 10:32:32.080927 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.081903 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.082875 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.086781 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.090688 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.100451 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.103607 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.111549 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.118164 c1.go:105: [Info] code=200,body={"ping2":"pong2"}
2024/01/24 10:32:32.125719 c1.go:105: [Info] code=200,body={"ping2":"pong2"}

是否你测试的时候,刚好成功了,然后cache了配置,而且你server的配置也是固定了,所以就测试不出来。你每次启动client的时候尝试一下,把cache文件都清一下,再测client。

@Skyenought Skyenought assigned Skyenought and unassigned Skyenought Jan 24, 2024
@Skyenought
Copy link
Member

好的, 我复现出来了, 现在我来看看怎么回事

@Skyenought
Copy link
Member

使用 waitgroup 就可以了

var wg sync.WaitGroup

func main() {
	wg.Add(2)
	test1()
	test2()
	wg.Wait()
}

@kingkongpoon
Copy link
Author

测了,还是不行。
你测试的时候,是否每次都把cache文件夹都清了?

@li-jin-gou
Copy link
Collaborator

测了,还是不行。 你测试的时候,是否每次都把cache文件夹都清了?

引入多个客户端的目的是什么?真实场景需要这么是用嘛

@li-jin-gou
Copy link
Collaborator

li-jin-gou commented Jan 26, 2024

目前看是 resolve 的 cache-key 定义不合理导致覆盖,但是需要明确你的场景然后我们重新定义 cache-key。

@kingkongpoon
Copy link
Author

测了,还是不行。 你测试的时候,是否每次都把cache文件夹都清了?

引入多个客户端的目的是什么?真实场景需要这么是用嘛

1,现在在测试这种场景是否能用。
2,内部团队现在是各自维护了nacos,自己对应的服务,然后通过域名再相互调用。如果能这样用,就不需要走域名,而是自己通过nacos就可以了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants