-
Notifications
You must be signed in to change notification settings - Fork 40
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
Comments
cc @Skyenought |
server 在注册时有将多个 nacos 地址配置进入 ServerConfigs 吗? |
@Skyenought |
我的代码无论是在 server 向一个 nacos 注册, 还是两个 nacos 注册,并没有出现复现相关的问题 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, ®istry.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, ®istry.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))
}
} |
我理解你的意思, 我在使用你预期方式还是无法复现😭
|
你好,我用你的代码,依然能复现。我一个server只注册到一个nacos。
第一个nacos 192.168.56.108:8848 client代码也是复制你的,改了一下nacos配置
先清理了cache目录,这里因为server配置不会变,所以每次执行都要先清理这个目录。
注释掉ttest2(),运行结果
注释掉ttest1(),运行结果
单独运行都是正常的。
此时cache目录会自动生成
cache/DEFAULT_GROUP@@hertz.test.demo2@@default
按你的代码,也是能复现。 我按DEFAULT_GROUP@@hertz.test.demo@@default的内容修改了DEFAULT_GROUP@@hertz.test.demo2@@default
然后注释掉这个配置,就可以正常访问
是否你测试的时候,刚好成功了,然后cache了配置,而且你server的配置也是固定了,所以就测试不出来。你每次启动client的时候尝试一下,把cache文件都清一下,再测client。 |
好的, 我复现出来了, 现在我来看看怎么回事 |
使用 waitgroup 就可以了 var wg sync.WaitGroup
func main() {
wg.Add(2)
test1()
test2()
wg.Wait()
} |
测了,还是不行。 |
引入多个客户端的目的是什么?真实场景需要这么是用嘛 |
目前看是 resolve 的 cache-key 定义不合理导致覆盖,但是需要明确你的场景然后我们重新定义 cache-key。 |
1,现在在测试这种场景是否能用。 |
想在一个go程序里面,同时访问不同的nacos/consul里面的不同服务。后初始化调用的,不能正常使用。
代码简单表示了一下问题
The text was updated successfully, but these errors were encountered: