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 \n", 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,
})
}

 

posted @ 2021-01-16 22:58  zbs666  阅读(918)  评论(0编辑  收藏  举报