Nacos相关技术文档
~~~
(四)Nacos开发教程之SpringCloud集成服务注册(参考,后续用go实现)
(五)Nacos开发教程之SpringCloud集成配置管理(参考,后续用go实现)
~~~
Mac-M1电脑本地使用docker搭建Nacos-Server
参考:https://zhuanlan.zhihu.com/p/531360994
安装完访问地址:访问地址:http://localhost:8848/nacos/ 账号/密码:nacos/nacos
# 先search下 docker search nacos NAME DESCRIPTION STARS OFFICIAL AUTOMATED nacos/nacos-server This project contains a Docker image meant t… 435 [OK] zhusaidong/nacos-server-m1 Nacos Server for Apple MacOS M1 22 nacos/nacos-mysql 7 # 安装这个版本的 zhusaidong/nacos-server-m1 docker pull zhusaidong/nacos-server-m1:2.0.3 # docker images 验证下 ~ > docker images REPOSITORY TAG IMAGE ID CREATED SIZE zhusaidong/nacos-server-m1 2.0.3 f1d91cde7e60 2 years ago 1.12GB mysql latest 6ef9eb9d764e 9 months ago 530MB redis latest 667aff0d6b0a 9 months ago 111MB postgres latest 2ce4f03f420b 9 months ago 359MB # run docker run -d -p 8848:8848 --env MODE=standalone --name nacos zhusaidong/nacos-server-m1:2.0.3 # docker ps 看看后台有没有运行 docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cde07371d1e3 zhusaidong/nacos-server-m1:2.0.3 "bin/docker-startup.…" 3 seconds ago Up 3 seconds 0.0.0.0:8848->8848/tcp nacos
Golang操作Nacos实现一个简单的配置监听
里面的配置如下:
{ "name":"naruto", "age":23, "gender":"female" }
Golang的代码如下:
package main import ( "encoding/json" "fmt" "github.com/nacos-group/nacos-sdk-go/clients" "github.com/nacos-group/nacos-sdk-go/clients/config_client" "github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/nacos-group/nacos-sdk-go/vo" "log" ) const ( dataId = "config.json" group = "group_test1" ) type ConfigData struct { Name string `json:"name"` Age int `json:"age"` Gender string `json:"gender"` } func newConfigClient() config_client.IConfigClient { sc := []constant.ServerConfig{ { IpAddr: "127.0.0.1", Port: 8848, // 默认Nacos端口 }, } cc := constant.ClientConfig{ // Notice 有命名空间的话 需要加上 NamespaceId: "fb94fc3b-514b-44d0-9d20-eefeb997c9ad", // Notice 其他配置 TimeoutMs: 5000, NotLoadCacheAtStart: true, CacheDir: "./nacos/cache", LogLevel: "error", } client, err := clients.NewConfigClient( vo.NacosClientParam{ ClientConfig: &cc, ServerConfigs: sc, }) if err != nil { log.Fatalf("无法创建Nacos客户端:%v", err) } return client } func main() { // 初始化 nacos 客户端 client := newConfigClient() // 获取初始配置 content, err := client.GetConfig(vo.ConfigParam{ DataId: dataId, Group: group, }) if err != nil { log.Fatalf("无法获取初始配置:%v", err) } var jsonData ConfigData if err := json.Unmarshal([]byte(content), &jsonData); err != nil { log.Fatalf("无法解析初始配置:%v", err) } fmt.Printf("初始配置:%+v\n", jsonData) // 启动配置监听 err = client.ListenConfig(vo.ConfigParam{ DataId: dataId, Group: group, OnChange: func(namespace, group, dataId, data string) { var updatedData ConfigData if err := json.Unmarshal([]byte(data), &updatedData); err != nil { log.Printf("配置更改,但无法解析新配置:%v", err) } else { jsonData = updatedData fmt.Printf("配置已更新:%+v\n", jsonData) } }, }) if err != nil { log.Fatalf("无法启动配置监听:%v", err) } select {} }
效果:程序运行起来会打印初始配置,如果更新nacos中对应的配置的话,会打印配置更新后的内容~
标签:group,err,配置,nacos,Nacos,Golang,docker,监听 From: https://www.cnblogs.com/paulwhw/p/17787897.html