Skip to content

Commit

Permalink
add python sdk basic demo #48
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Aug 12, 2024
1 parent 8a0e52b commit d5c1a72
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
42 changes: 42 additions & 0 deletions sdk-examples/python/basic_config/config_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#-*- config: utf8 -*-

import nacos
import json

SERVER_ADDRESSES = "http://127.0.0.1:8848"
NAMESPACE = "dev"

client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE)

# auth mode
#client = nacos.NacosClient(Server_ADDRESSES, namespace=NAMESPACE,ak="{ak}",sk="{sk}")

data_id = "config.001"
group = "default"

def change_config_info(times):
d = {"name":"foo","value":-1}
for i in range(times):
d["value"]=i
config_str= json.dumps(d)
client.publish_config(data_id,group,config_str)
print("publish value:",config_str)
time.sleep(1)
get_value = client.get_config(data_id,group)
print("get value:",get_value)

def config_watcher(params):
print("config_watcher change:",params)

def main():
# publish config
print("publish config:",client.publish_config(data_id,group,"hello info"))
# get config
print("get config:",client.get_config(data_id,group))
# use config watcher
client.add_config_watcher(data_id,group,config_watcher)
# change config value
change_config_info(10)

if __name__ == "__main__":
main()
34 changes: 34 additions & 0 deletions sdk-examples/python/basic_naming/naming_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#-*- config: utf8 -*-

import nacos
from nacos.listener import SubscribeListener
import time

SERVER_ADDRESSES = "http://127.0.0.1:8848"
NAMESPACE = "dev"

client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE)

# auth mode
#client = nacos.NacosClient(Server_ADDRESSES, namespace=NAMESPACE,ak="{ak}",sk="{sk}")

data_id = "config.001"
group = "default"
service_name = "foo_service"

def instance_subscribe(*args,**kv):
print("instance_subscribe:",args[0],args[1].__dict__)

def add_naming_instance(num):
for i in range(num):
client.add_naming_instance(service_name,"192.168.10.{0}".format(100+i),8080,group_name = group,heartbeat_interval=5)
time.sleep(0.5)

def main():
subscriber = SubscribeListener(instance_subscribe,group)
client.subscribe(subscriber,service_name = service_name,group_name = group)
add_naming_instance(10)
print("holding")

if __name__ == "__main__":
main()

0 comments on commit d5c1a72

Please sign in to comment.