Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #101]Feature support gRPC: add demos #103

Open
wants to merge 1 commit into
base: feature_support_grpc_copy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions v2/demo/demo_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import logging
import time

from v2.nacos.config.ilistener import Listener
from v2.nacos.config.nacos_config_service import NacosConfigService

SERVER_ADDRESSES = "http://127.0.0.1:8848"
NAMESPACE = ""
DATA_ID1 = "nacos_config_test1"
DATA_ID2 = "nacos_config_test2"
GROUP = "default"
CONFIG_CONTENT = "content"
CONFIG_TYPE = "text"
TIMEOUT = 3000

properties = {
"namespace": NAMESPACE,
"serverAddr": SERVER_ADDRESSES,
"username": "nacos",
"password": "nacos",
"endpoint": "",
"contextPath": "/nacos",
"ak": "nnn",
"sk": "nnn"
}

logger = logging.getLogger("nacos")
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s:%(message)s"))
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

logger.removeHandler(handler)


class DemoListener(Listener):
def get_executor(self):
return

def receive_config_info(self, config_info: str):
print("receive_config_info:", config_info)


if __name__ == '__main__':
config = NacosConfigService(logger, properties)
time.sleep(1)

# get server status
server_status = config.get_server_status()
print("server_status:", server_status)

# publish config
publish_config = config.publish_config(DATA_ID1, GROUP, CONFIG_CONTENT, CONFIG_TYPE)
print("publish_config:", str(publish_config))
time.sleep(1)

# publish config cas
publish_config_cas = config.publish_config_cas(DATA_ID2, GROUP, CONFIG_CONTENT, "", CONFIG_TYPE)
print("publish_config_cas:", str(publish_config_cas))
time.sleep(1)

# get config
config_content = config.get_config(DATA_ID1, GROUP, TIMEOUT)
print("get_config:", config_content)

# add listener
demo_listener = DemoListener()
config.add_listener(DATA_ID1, GROUP, demo_listener)
print("add_listener complete")
time.sleep(1)

# remove listener
config.remove_listener(DATA_ID1, GROUP, demo_listener)
print("remove_listener complete")
time.sleep(1)

# get config and sign listener
config_content_2 = config.get_config_and_sign_listener(DATA_ID1, GROUP, TIMEOUT, demo_listener)
print("get_config_and_sign_listener:", config_content_2)
time.sleep(1)

# remove config
remove_config = config.remove_config(DATA_ID1, GROUP)
print("remove_config:", str(remove_config))
time.sleep(1)

# shutdown
time.sleep(5)
config.shutdown()
print("shutdown")
27 changes: 25 additions & 2 deletions v2/demo/demo_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from v2.nacos.naming.nacos_naming_service import NacosNamingService

SERVER_ADDRESSES = "http://127.0.0.1:8848"
# SERVER_ADDRESSES = "http://10.62.188.68:8848"
NAMESPACE = "public"

properties = {
Expand Down Expand Up @@ -59,4 +58,28 @@ def on_event(self, event) -> None:
print("list_view:", str(list_view))

# subscribe
naming.subscribe("nacos.test.2", "default", [], DemoListener())
demo_listener = DemoListener()
naming.subscribe("nacos.test.2", "default", [], demo_listener)

# select one healthy instance
one_healthy_instance = naming.select_one_healthy_instance("nacos.test.2", "default", [], True)
print("select one healthy instance:", str(one_healthy_instance))

# get server status
server_status = naming.get_server_status()
time.sleep(1)
print("server_status:", server_status)

# unsubscribe
naming.unsubscribe("nacos.test.2", "default", [], demo_listener)
time.sleep(1)
print("unsubscribed!")

# deregister instance
naming.deregister_instance("nacos.test.2", "default", "11.11.11.11", 8888, "DEFAULT")
time.sleep(1)
print("deregister instance!")

# shutdown
naming.shutdown()
print("shutdown")