nacos配置中心Python Client


代碼倉庫地址

nacos-client-py

監聽nacos server配置中心,並拉取應用配置文件

Usage

1.創建.env文件

nacos服務器信息

$ cat .env
nacos_server="192.168.1.1:8848"
nacos_namespace_id="fbea1ef7-2428-4516-9a38-487c99c7df7d"
nacos_suth_user="nacos"
nacos_auth_passwd="nacos"

2.創建配置文件信息

需要監聽的配置文件

configs:
  - name: official-nginx
    id: official          # nacos data id
    group: haproxy          # nacos group
    path: "/etc/haproxy/haproxy.cfg"       # 服務器上配置文件路徑
    command: "systemctl restart haproxy"      # 配置文件更新后,需要執行的命令

3.定制化腳本

    # 回調函數,當監聽到配置文件發生變化后,執行的操作
    def print_cm(self, status):
        snapshot_file = "{0}+{1}+{2}".format(status['data_id'], status['group'], NAMESPACE)
        for p in self.cf['configs']:
            if status['data_id'] == p['id'] and status['group'] == p['group']:
                shutil.copy("nacos-data/snapshot/{}".format(snapshot_file), p['path'])      # 將snapsho file復制到本地的文件路徑
            s, r = subprocess.getstatusoutput(p['command'])
            if int(s) != 0:
                print("命令執行失敗:{}".format(p['command']))
                return False
        return True

4.使用supervisor啟動服務

啟動命令

python3 nacos-get-config.py


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM