go nacos服务发现

go nacos服务发现,第1张

概述本来想研究一下nacos的服务发现,原来sample这么详细啊,直接运行代码吧:packagemainimport("fmt""time""github.comacos-groupacos-sdk-go/clients""github.comacos-groupacos-sdk-go/clientsaming_client""gi

本来想研究一下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服务发现全部内容,希望文章能够帮你解决go nacos服务发现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1241758.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存