源码: 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 进入管理后台

http://192.168.1.103:8848/nacos/index.html#/login

账号和密码都是 nacos
image.png

3 配置管理

(1) 新建配置文件

image.png

(2) 命名空间

命名空间可以隔离配置文件
image.png
image.png

4 py操作nacos配置

下载SDK: https://github.com/nacos-group/nacos-sdk-python
image.png

  1. from playhouse.pool import PooledMySQLDatabase
  2. from playhouse.shortcuts import ReconnectMixin
  3. from loguru import logger
  4. import nacos
  5. class ReconnectMysqlDatabase(ReconnectMixin, PooledMySQLDatabase):
  6. pass
  7. NACOS = {
  8. "Host": "192.168.1.103",
  9. "Port": 8848,
  10. "NameSpace": "cc002bf5-cf66-4e9f-bda9-d56d74cce2f3",
  11. "User": "nacos",
  12. "Password": "nacos",
  13. "DataId": "user_srv.json",
  14. "Group": "dev",
  15. }
  16. client = nacos.NacosClient("{}:{}".format(NACOS["Host"], NACOS["Port"]), namespace=NACOS["NameSpace"],
  17. username=NACOS["User"], password=NACOS["Password"])
  18. import json
  19. json_data = json.loads(client.get_config(NACOS["DataId"], NACOS["Group"]))
  20. logger.info(json_data)
  21. # 监控配置文件的变化
  22. def test_cb(args):
  23. logger.info("配置文件产生变化")
  24. json_data = json.loads(args.get('raw_content'))
  25. logger.info(json_data)
  26. # 重新赋值全局变量
  27. global CONSUL_HOST, CONSUL_PORT, SERVICE_NAME, SERVICE_TAGS, DB
  28. CONSUL_HOST = json_data["consul"]["host"]
  29. CONSUL_PORT = json_data["consul"]["port"]
  30. SERVICE_NAME = json_data["name"]
  31. SERVICE_TAGS = json_data["tags"]
  32. DB = ReconnectMysqlDatabase(json_data["mysql"]["db"], host=json_data["mysql"]["host"],
  33. port=json_data["mysql"]["port"],
  34. user=json_data["mysql"]["user"], password=json_data["mysql"]["password"])
  35. client.add_config_watcher(NACOS["DataId"], NACOS["Group"], test_cb)
  36. import time
  37. time.sleep(3000)
  38. CONSUL_HOST = json_data["consul"]["host"]
  39. CONSUL_PORT = json_data["consul"]["port"]
  40. SERVICE_NAME = json_data["name"]
  41. SERVICE_TAGS = json_data["tags"]
  42. DB = ReconnectMysqlDatabase(json_data["mysql"]["db"], host=json_data["mysql"]["host"], port=json_data["mysql"]["port"],
  43. user=json_data["mysql"]["user"], password=json_data["mysql"]["password"])

5 go操作nacos配置

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/nacos-group/nacos-sdk-go/clients"
  5. "github.com/nacos-group/nacos-sdk-go/common/constant"
  6. "github.com/nacos-group/nacos-sdk-go/vo"
  7. "time"
  8. )
  9. func main() {
  10. // 服务端配置
  11. sc := []constant.ServerConfig{
  12. {
  13. IpAddr: "192.168.1.103",
  14. Port: 8848,
  15. },
  16. }
  17. // 客户端配置
  18. cc := constant.ClientConfig{
  19. 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.
  20. TimeoutMs: 5000,
  21. NotLoadCacheAtStart: true,
  22. LogDir: "tmp/nacos/log",
  23. CacheDir: "tmp/nacos/cache",
  24. RotateTime: "1h",
  25. MaxAge: 3,
  26. LogLevel: "debug",
  27. }
  28. // 创建动态配置客户端
  29. configClient, err := clients.CreateConfigClient(map[string]interface{}{
  30. "serverConfigs": sc,
  31. "clientConfig": cc,
  32. })
  33. if err != nil {
  34. panic(err)
  35. }
  36. // 读取配置
  37. content, err := configClient.GetConfig(vo.ConfigParam{
  38. DataId: "user_api.yaml",
  39. Group: "dev",
  40. })
  41. if err != nil {
  42. panic(err)
  43. }
  44. fmt.Println(content)
  45. // 监听配置变化
  46. err = configClient.ListenConfig(vo.ConfigParam{
  47. DataId: "user_api.yaml",
  48. Group: "dev",
  49. OnChange: func(namespace, group, dataId, data string) {
  50. fmt.Println("配置文件产生变化")
  51. fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
  52. },
  53. })
  54. time.Sleep(3000 * time.Second)
  55. }