源码: https://github.com/alibaba/nacos
文档: https://nacos.io/zh-cn/docs/what-is-nacos.html
开源时间: 2018
1 安装nacos
docker run —name nacos-standalone -e MODE=standalone -e JVM_XMS=512m -e JVM_XMX=512m -e JVM_XMN=256m -p 8848:8848 -d nacos/nacos-server:latest
2 进入管理后台
3 配置管理
(1) 新建配置文件
(2) 命名空间
4 py操作nacos配置
下载SDK: https://github.com/nacos-group/nacos-sdk-python
from playhouse.pool import PooledMySQLDatabase
from playhouse.shortcuts import ReconnectMixin
from loguru import logger
import nacos
class ReconnectMysqlDatabase(ReconnectMixin, PooledMySQLDatabase):
pass
NACOS = {
"Host": "192.168.1.103",
"Port": 8848,
"NameSpace": "cc002bf5-cf66-4e9f-bda9-d56d74cce2f3",
"User": "nacos",
"Password": "nacos",
"DataId": "user_srv.json",
"Group": "dev",
}
client = nacos.NacosClient("{}:{}".format(NACOS["Host"], NACOS["Port"]), namespace=NACOS["NameSpace"],
username=NACOS["User"], password=NACOS["Password"])
import json
json_data = json.loads(client.get_config(NACOS["DataId"], NACOS["Group"]))
logger.info(json_data)
# 监控配置文件的变化
def test_cb(args):
logger.info("配置文件产生变化")
json_data = json.loads(args.get('raw_content'))
logger.info(json_data)
# 重新赋值全局变量
global CONSUL_HOST, CONSUL_PORT, SERVICE_NAME, SERVICE_TAGS, DB
CONSUL_HOST = json_data["consul"]["host"]
CONSUL_PORT = json_data["consul"]["port"]
SERVICE_NAME = json_data["name"]
SERVICE_TAGS = json_data["tags"]
DB = ReconnectMysqlDatabase(json_data["mysql"]["db"], host=json_data["mysql"]["host"],
port=json_data["mysql"]["port"],
user=json_data["mysql"]["user"], password=json_data["mysql"]["password"])
client.add_config_watcher(NACOS["DataId"], NACOS["Group"], test_cb)
import time
time.sleep(3000)
CONSUL_HOST = json_data["consul"]["host"]
CONSUL_PORT = json_data["consul"]["port"]
SERVICE_NAME = json_data["name"]
SERVICE_TAGS = json_data["tags"]
DB = ReconnectMysqlDatabase(json_data["mysql"]["db"], host=json_data["mysql"]["host"], port=json_data["mysql"]["port"],
user=json_data["mysql"]["user"], password=json_data["mysql"]["password"])
5 go操作nacos配置
package main
import (
"fmt"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"time"
)
func main() {
// 服务端配置
sc := []constant.ServerConfig{
{
IpAddr: "192.168.1.103",
Port: 8848,
},
}
// 客户端配置
cc := constant.ClientConfig{
NamespaceId: "cc002bf5-cf66-4e9f-bda9-d56d74cce2f3", //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "tmp/nacos/log",
CacheDir: "tmp/nacos/cache",
RotateTime: "1h",
MaxAge: 3,
LogLevel: "debug",
}
// 创建动态配置客户端
configClient, err := clients.CreateConfigClient(map[string]interface{}{
"serverConfigs": sc,
"clientConfig": cc,
})
if err != nil {
panic(err)
}
// 读取配置
content, err := configClient.GetConfig(vo.ConfigParam{
DataId: "user_api.yaml",
Group: "dev",
})
if err != nil {
panic(err)
}
fmt.Println(content)
// 监听配置变化
err = configClient.ListenConfig(vo.ConfigParam{
DataId: "user_api.yaml",
Group: "dev",
OnChange: func(namespace, group, dataId, data string) {
fmt.Println("配置文件产生变化")
fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
},
})
time.Sleep(3000 * time.Second)
}