• nacos 配置中心 & 服务发现 使用


    nacos 配置中心 & 服务发现 使用

    一: 安装步骤

    • github下载连接

    • 解压, 进入bin目录执行: sh startup.sh -m standalone

    • 看到 nacos is starting with standalone 表面城管

    二: 配置中心的使用

    具体操作如下

    默认用户名,密码: nacos , nacos

    • 登录后的页面

    image-20210116220725920

     

    • 创建用户:

    image-20210116221011641

     

    • 创建namespace (类似php的namespace , golang的package ; 主要用户隔离环境)

    image-20210116221036477

     

    • 创建角色 & 绑定用户

    image-20210116221220754

     

    • 分配权限 (给角色分配权限)

    image-20210116221256896

     

    使用golang执行操作 (使用 nacos-group/nacos-sdk-go 客户端来操作)
    func main() {
    // 服务端配置
    sc := []constant.ServerConfig{
    {
    IpAddr: "127.0.0.1", // nacos服务端的地址, 集群版配置多个
    Port:   8848,        // nacos 的端口
    },
    }
    // 客户端配置
    cc := constant.ClientConfig{
    NamespaceId:         "09f4588a-ca0e-4ab6-9911-549703764e39", //namespace_id 刚才配置添加的id
    TimeoutMs:           5000,                                   // 超时时间
    NotLoadCacheAtStart: true,
    LogDir:              "/tmp/nacos/log",
    CacheDir:            "/tmp/nacos/cache",
    RotateTime:          "1h",
    MaxAge:              3,
    LogLevel:            "debug",
    }

    // a more graceful way to create config client
    // 创建配置中心client
    client, err := clients.NewConfigClient(
    vo.NacosClientParam{
    ClientConfig:  &cc,
    ServerConfigs: sc,
    },
    )

    if err != nil {
    panic(err)
    }

    //publish config
    //config key=dataId+group+namespaceId
    _, err = client.PublishConfig(vo.ConfigParam{
    DataId:  "test-data",
    Group:   "test-group",
    Content: "hello world123!",
    })
    _, err = client.PublishConfig(vo.ConfigParam{
    DataId:  "test-data-2",
    Group:   "test-group",
    Content: "hello world001!",
    })
    if err != nil {
    fmt.Printf("PublishConfig err:%+v ", err)
    }

    //get config
    content, err := client.GetConfig(vo.ConfigParam{
    DataId: "test-data",
    Group:  "test-group",
    })
    fmt.Println("GetConfig,config :" + content)
    }  

    输出: GetConfig,config :hello world123!

     

    还可以监听key的变更

      //Listen config change,key=dataId+group+namespaceId.
    // 监听配置的变更
    err = client.ListenConfig(vo.ConfigParam{
    DataId: "test-data",
    Group:  "test-group",
    OnChange: func(namespace, group, dataId, data string) {
    fmt.Println("config changed group:" + group + ", dataId:" + dataId + ", content:" + data)
    },
    })
    • 登录nacos客户端查看

    image-20210116221921951

     

    注意:

    • nacos 只需要知道namespace , client不需要输入username, password就可以访问

    • 但是如果输入了, username 与 password 不匹配就好报错

    二: 服务发现的使用

     

    image-20210116224914402

     

    具体代码

    func main() {
    // 服务端配置
    sc := []constant.ServerConfig{
    {
    IpAddr: "127.0.0.1",
    Port:   8848,
    },
    }
    // 客户端配置
    cc := constant.ClientConfig{
    NamespaceId:         "09f4588a-ca0e-4ab6-9911-549703764e39", //namespace id
    TimeoutMs:           5000,
    NotLoadCacheAtStart: true,
    LogDir:              "/tmp/nacos/log",
    CacheDir:            "/tmp/nacos/cache",
    RotateTime:          "1h",
    MaxAge:              3,
    LogLevel:            "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:          "192.168.0.11",
    Port:        8848,
    ServiceName: "zbs_test_service",
    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:          "192.168.0.12",
    Port:        8848,
    ServiceName: "zbs_test_service",
    Weight:      10,
    ClusterName: "cluster-a",
    Enable:      true,
    Healthy:     true,
    Ephemeral:   true,
    })
    }
  • 相关阅读:
    [转]HDR渲染器的实现(基于OpenGL)
    32位微机的内存管理模式
    王爽汇编检测点1.1
    标志位的说明
    通用寄存器的作用
    Debug初次使用
    昨天纠结了一天要始学习dos汇编还是win32汇编,终于想通了
    CPU对存储器的读写(二、数据总线、控制总线)
    CPU对存储器的读写(一、地址总线)
    段寄存器的引用
  • 原文地址:https://www.cnblogs.com/lovezbs/p/14287810.html
Copyright © 2020-2023  润新知