省略部署方式,百度或者谷歌
Nacos文檔
服務(Service)是 Nacos 世界的一等公民。Nacos 支持幾乎所有主流類型的“服務”的發現、配置和管理:
import nacos
# Both HTTP/HTTPS protocols are supported, if not set protocol prefix default is HTTP, and HTTPS with no ssl check(verify=False)
# "192.168.3.4:8848" or "https://192.168.3.4:443" or "http://192.168.3.4:8848,192.168.3.5:8848" or "https://192.168.3.4:443,https://192.168.3.5:443"
SERVER_ADDRESSES = "http://換成自己的ip:端口"
NAMESPACE = ""
# no auth mode
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE)
# auth mode
# client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, ak="{ak}", sk="{sk}")
# get config
data_id = "python_project.json"
group = "dev"
print(client.get_config(data_id, group))
nacos_data = eval(client.get_config(data_id, group))
password = nacos_data["password"]
print("password is", password)
def test_cb(args):
print("nacos配置發生變化")
global password
nacos_data = eval(client.get_config(data_id, group))
password = nacos_data["password"]
# 注冊服務
a = client.add_naming_instance("changyu_test1", "www.baidu.com", 80)
print(a)
# 發現服務
b = client.list_naming_instance("changyu_test1")
print(b)
print("ip", b["hosts"][0]["ip"])
print("port", b["hosts"][0]["port"])
if __name__ == '__main__':
# 配置變化的監聽
client.add_config_watcher(data_id, group, test_cb)
import time
time.sleep(10000)