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

一: 安装步骤

  • github下载连接

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

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

二: 配置中心的使用

具体操作如下

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

  • 登录后的页面

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

 

  • 创建用户:

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

 

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

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

 

  • 创建角色 & 绑定用户

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

 

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

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

 

使用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",

相关文章: