本来想研究一下nacos的服务发现,原来sample这么详细啊,直接运行代码吧:package mainimport (fmttimegithub.com/nacos-group/nacos-sdk-go/clientsgithub.com/nacos-group/nacos-sdk-go/clients/naming_cli...
本来想研究一下nacos的服务发现,原来sample这么详细啊,直接运行代码吧:
package main
import (
"fmt"
"time"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/util"
"github.com/nacos-group/nacos-sdk-go/vo"
)
func main() {
sc := []constant.ServerConfig{
{
IpAddr: "192.168.100.30",
Port: 8848,
},
}
//or a more graceful way to create ServerConfig
_ = []constant.ServerConfig{
*constant.NewServerConfig("192.168.100.30", 8848),
}
cc := constant.ClientConfig{
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468", //namespace id
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
//LogDir: "/tmp/nacos/log",
// CacheDir: "/tmp/nacos/cache",
RotateTime: "1h",
MaxAge: 3,
LogLevel: "debug",
}
//or a more graceful way to create ClientConfig
_ = *constant.NewClientConfig(
constant.WithNamespaceId("e525eafa-f7d7-4029-83d9-008937f9d468"),
constant.WithTimeoutMs(5000),
constant.WithNotLoadCacheAtStart(true),
//constant.WithLogDir("/tmp/nacos/log"),
//constant.WithCacheDir("/tmp/nacos/cache"),
constant.WithRotateTime("1h"),
constant.WithMaxAge(3),
constant.WithLogLevel("debug"),
)
// a more graceful way to create naming client
client, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
},
)
if err != nil {
panic(err)
}
//Register with default cluster and group
//ClusterName=DEFAULT,GroupName=DEFAULT_GROUP
ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip: "10.0.0.10",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
Enable: true,
Healthy: true,
Ephemeral: true,
Metadata: map[string]string{"idc": "shanghai"},
})
//Register with cluster name
//GroupName=DEFAULT_GROUP
ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip: "10.0.0.11",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "cluster-a",
Enable: true,
Healthy: true,
Ephemeral: true,
})
//Register different cluster
//GroupName=DEFAULT_GROUP
ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip: "10.0.0.12",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "cluster-b",
Enable: true,
Healthy: true,
Ephemeral: true,
})
//Register different group
ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip: "10.0.0.13",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "cluster-b",
GroupName: "group-a",
Enable: true,
Healthy: true,
Ephemeral: true,
})
ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip: "10.0.0.14",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "cluster-b",
GroupName: "group-b",
Enable: true,
Healthy: true,
Ephemeral: true,
})
//DeRegister with ip,port,serviceName
//ClusterName=DEFAULT, GroupName=DEFAULT_GROUP
//Note:ip=10.0.0.10,port=8848 should belong to the cluster of DEFAULT and the group of DEFAULT_GROUP.
ExampleServiceClient_DeRegisterServiceInstance(client, vo.DeregisterInstanceParam{
Ip: "10.0.0.10",
Port: 8848,
ServiceName: "demo.go",
Ephemeral: true, //it must be true
})
//DeRegister with ip,port,serviceName,cluster
//GroupName=DEFAULT_GROUP
//Note:ip=10.0.0.10,port=8848,cluster=cluster-a should belong to the group of DEFAULT_GROUP.
ExampleServiceClient_DeRegisterServiceInstance(client, vo.DeregisterInstanceParam{
Ip: "10.0.0.11",
Port: 8848,
ServiceName: "demo.go",
Cluster: "cluster-a",
Ephemeral: true, //it must be true
})
//DeRegister with ip,port,serviceName,cluster,group
ExampleServiceClient_DeRegisterServiceInstance(client, vo.DeregisterInstanceParam{
Ip: "10.0.0.14",
Port: 8848,
ServiceName: "demo.go",
Cluster: "cluster-b",
GroupName: "group-b",
Ephemeral: true, //it must be true
})
//Get service with serviceName
//ClusterName=DEFAULT, GroupName=DEFAULT_GROUP
ExampleServiceClient_GetService(client, vo.GetServiceParam{
ServiceName: "demo.go",
})
//Get service with serviceName and cluster
//GroupName=DEFAULT_GROUP
ExampleServiceClient_GetService(client, vo.GetServiceParam{
ServiceName: "demo.go",
Clusters: []string{"cluster-a", "cluster-b"},
})
//Get service with serviceName ,group
//ClusterName=DEFAULT
ExampleServiceClient_GetService(client, vo.GetServiceParam{
ServiceName: "demo.go",
GroupName: "group-a",
})
//SelectAllInstance return all instances,include healthy=false,enable=false,weight<=0
//ClusterName=DEFAULT, GroupName=DEFAULT_GROUP
ExampleServiceClient_SelectAllInstances(client, vo.SelectAllInstancesParam{
ServiceName: "demo.go",
})
//SelectAllInstance
//GroupName=DEFAULT_GROUP
ExampleServiceClient_SelectAllInstances(client, vo.SelectAllInstancesParam{
ServiceName: "demo.go",
Clusters: []string{"cluster-a", "cluster-b"},
})
//SelectAllInstance
//ClusterName=DEFAULT
ExampleServiceClient_SelectAllInstances(client, vo.SelectAllInstancesParam{
ServiceName: "demo.go",
GroupName: "group-a",
})
//SelectInstances only return the instances of healthy=${HealthyOnly},enable=true and weight>0
//ClusterName=DEFAULT,GroupName=DEFAULT_GROUP
ExampleServiceClient_SelectInstances(client, vo.SelectInstancesParam{
ServiceName: "demo.go",
})
//SelectOneHealthyInstance return one instance by WRR strategy for load balance
//And the instance should be health=true,enable=true and weight>0
//ClusterName=DEFAULT,GroupName=DEFAULT_GROUP
ExampleServiceClient_SelectOneHealthyInstance(client, vo.SelectOneHealthInstanceParam{
ServiceName: "demo.go",
})
//Subscribe key=serviceName+groupName+cluster
//Note:We call add multiple SubscribeCallback with the same key.
param := &vo.SubscribeParam{
ServiceName: "demo.go",
Clusters: []string{"cluster-b"},
SubscribeCallback: func(services []model.SubscribeService, err error) {
fmt.Printf("callback111 return services:%s \n\n", util.ToJsonString(services))
},
}
ExampleServiceClient_Subscribe(client, param)
param2 := &vo.SubscribeParam{
ServiceName: "demo.go",
Clusters: []string{"cluster-b"},
SubscribeCallback: func(services []model.SubscribeService, err error) {
fmt.Printf("callback222 return services:%s \n\n", util.ToJsonString(services))
},
}
ExampleServiceClient_Subscribe(client, param2)
ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip: "10.0.0.112",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "cluster-b",
Enable: true,
Healthy: true,
Ephemeral: true,
})
//wait for client pull change from server
time.Sleep(10 * time.Second)
//Now we just unsubscribe callback1, and callback2 will still receive change event
ExampleServiceClient_UnSubscribe(client, param)
ExampleServiceClient_DeRegisterServiceInstance(client, vo.DeregisterInstanceParam{
Ip: "10.0.0.112",
Ephemeral: true,
Port: 8848,
ServiceName: "demo.go",
Cluster: "cluster-b",
})
//wait for client pull change from server
time.Sleep(10 * time.Second)
//GeAllService will get the list of service name
//NameSpace default value is public.If the client set the namespaceId, NameSpace will use it.
//GroupName default value is DEFAULT_GROUP
ExampleServiceClient_GetAllService(client, vo.GetAllServiceInfoParam{
PageNo: 1,
PageSize: 10,
})
ExampleServiceClient_GetAllService(client, vo.GetAllServiceInfoParam{
NameSpace: "0e83cc81-9d8c-4bb8-a28a-ff703187543f",
PageNo: 1,
PageSize: 10,
})
}
/*--------------------------------------------------------------------------*/
func ExampleServiceClient_RegisterServiceInstance(client naming_client.INamingClient, param vo.RegisterInstanceParam) {
success, _ := client.RegisterInstance(param)
fmt.Printf("RegisterServiceInstance,param:%+v,result:%+v \n\n", param, success)
}
func ExampleServiceClient_DeRegisterServiceInstance(client naming_client.INamingClient, param vo.DeregisterInstanceParam) {
success, _ := client.DeregisterInstance(param)
fmt.Printf("DeRegisterServiceInstance,param:%+v,result:%+v \n\n", param, success)
}
func ExampleServiceClient_GetService(client naming_client.INamingClient, param vo.GetServiceParam) {
service, _ := client.GetService(param)
fmt.Printf("GetService,param:%+v, result:%+v \n\n", param, service)
}
func ExampleServiceClient_SelectAllInstances(client naming_client.INamingClient, param vo.SelectAllInstancesParam) {
instances, _ := client.SelectAllInstances(param)
fmt.Printf("SelectAllInstance,param:%+v, result:%+v \n\n", param, instances)
}
func ExampleServiceClient_SelectInstances(client naming_client.INamingClient, param vo.SelectInstancesParam) {
instances, _ := client.SelectInstances(param)
fmt.Printf("SelectInstances,param:%+v, result:%+v \n\n", param, instances)
}
func ExampleServiceClient_SelectOneHealthyInstance(client naming_client.INamingClient, param vo.SelectOneHealthInstanceParam) {
instances, _ := client.SelectOneHealthyInstance(param)
fmt.Printf("SelectInstances,param:%+v, result:%+v \n\n", param, instances)
}
func ExampleServiceClient_Subscribe(client naming_client.INamingClient, param *vo.SubscribeParam) {
client.Subscribe(param)
}
func ExampleServiceClient_UnSubscribe(client naming_client.INamingClient, param *vo.SubscribeParam) {
client.Unsubscribe(param)
}
func ExampleServiceClient_GetAllService(client naming_client.INamingClient, param vo.GetAllServiceInfoParam) {
service, _ := client.GetAllServicesInfo(param)
fmt.Printf("GetAllService,param:%+v, result:%+v \n\n", param, service)
}
运行结果:
沃梦达教程
本文标题为:go nacos服务发现
猜你喜欢
- Ruby on Rails在Ping ++ 平台实现支付 2023-07-22
- Golang http.Client设置超时 2023-09-05
- R语言关于二项分布知识点总结 2022-11-30
- Go Web开发进阶实战(gin框架) 2023-09-06
- R语言绘图数据可视化pie chart饼图 2022-12-10
- 汇编语言程序设计之根据输入改变屏幕颜色的代码 2023-07-06
- R语言-如何切换科学计数法和更换小数点位数 2022-11-23
- Ruby的字符串与数组求最大值的相关问题讨论 2023-07-22
- Swift超详细讲解指针 2023-07-08
- Ruby 迭代器知识汇总 2023-07-23
