nacos-group / nacos-sdk-python Goto Github PK
View Code? Open in Web Editor NEWnacos python sdk
License: Apache License 2.0
nacos python sdk
License: Apache License 2.0
编译安装的 python3.6.8,pip3 21.0.1
Traceback (most recent call last):
File "run.py", line 47, in
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE,username=USER,password=PASS)
TypeError: init() got an unexpected keyword argument 'username'
#部分代码如下:
import requests
import nacos
SERVER_ADDRESSES = "nacos-hello.nacos-demo.com:80
NAMESPACE="test"
data_id = "conf-tab.md"
group = "test"
USER=nacos
PASS=nacos
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE,username=USER,password=PASS)
#client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="nacos", password="nacos")
python端服务注册成功后,大概每隔几周服务就会掉线。掉线后重新发送心跳包,java端就找不到实例了,此时nacos实例列表中可以看到实例,但是java端请求的时候就会报空指针。
naming-event.log
2021-08-13 12:15:58,605 INFO {POS} {IP-DISABLED} valid: 192.168.0.228:9000@DEFAULT@DEFAULT_GROUP@@semantic, region: unknown, msg: client timeout after 15000, last beat: 1628828139154
2021-08-13 12:16:13,664 INFO DEFAULT_GROUP@@semantic {SYNC} {IP-DEAD} cluster: DEFAULT, dead ips size: 1, content: [192.168.0.228:9000:unknown:DEFAULT_1.0_false_false_DEFAULT]
2021-08-13 12:16:13,664 INFO [IP-UPDATED] namespace: prod, service: DEFAULT_GROUP@@semantic, ips:
2021-08-13 12:18:19,154 INFO DEFAULT_GROUP@@semantic {SYNC} {IP-NEW} cluster: DEFAULT, new ips size: 1, content: [192.168.0.228:9000:unknown:DEFAULT_1.0_true_false_DEFAULT]
2021-08-13 12:18:19,155 INFO [IP-UPDATED] namespace: prod, service: DEFAULT_GROUP@@semantic, ips: 192.168.0.228:9000_true,
naming-server.log
2021-08-13 12:15:59,638 WARN protect threshold reached, return all ips, service: DEFAULT_GROUP@@semantic
2021-08-13 12:16:03,313 WARN protect threshold reached, return all ips, service: DEFAULT_GROUP@@semantic
2021-08-13 12:16:13,315 WARN protect threshold reached, return all ips, service: DEFAULT_GROUP@@semantic
2021-08-13 12:16:13,638 INFO [AUTO-DELETE-IP] service: DEFAULT_GROUP@@semantic, ip: {"instanceId":"192.168.0.228#9000#DEFAULT#DEFAULT_GROUP@@semantic","ip":"192.168.0.228","port":9000,"weight":1.0,"healthy":false,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@semantic","metadata":{},"lastBeat":1628828139154,"marked":false,"app":"DEFAULT","instanceHeartBeatInterval":5000,"instanceIdGenerator":"simple","instanceHeartBeatTimeOut":15000,"ipDeleteTimeout":30000}
2021-08-13 12:16:13,664 INFO [NACOS-RAFT] datum is changed, key: com.alibaba.nacos.naming.iplist.ephemeral.prod##DEFAULT_GROUP@@semantic, value: {"instanceList":[]}
2021-08-13 12:18:19,154 WARN [CLIENT-BEAT] The instance has been removed for health mechanism, perform data compensation operations, beat: {"load":0.0,"cpu":0.0,"rt":0.0,"qps":0.0,"mem":0.0,"port":9000,"ip":"192.168.0.228","serviceName":"semantic","cluster":"DEFAULT","weight":1.0,"ephemeral":true}, serviceName: DEFAULT_GROUP@@semantic
2021-08-13 12:18:19,154 INFO [NACOS-RAFT] datum is changed, key: com.alibaba.nacos.naming.iplist.ephemeral.prod##DEFAULT_GROUP@@semantic, value: {"instanceList":[{"instanceId":"192.168.0.228#9000#DEFAULT#DEFAULT_GROUP@@semantic","ip":"192.168.0.228","port":9000,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@semantic","lastBeat":1628828299154,"marked":false,"instanceHeartBeatInterval":5000,"instanceIdGenerator":"simple","instanceHeartBeatTimeOut":15000,"ipDeleteTimeout":30000}]}
java端的log
2021-08-13 12:16:23.317 INFO 16115 --- [com.alibaba.nacos.client.naming.updater] com.alibaba.nacos.client.naming : removed ips(1) service: DEFAULT_GROUP@@semantic -> [{"clusterName":"DEFAULT","enabled":true,"ephemeral":true,"healthy":true,"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000,"instanceId":"192.168.0.228#9000#DEFAULT#DEFAULT_GROUP@@semantic","instanceIdGenerator":"simple","ip":"192.168.0.228","ipDeleteTimeout":30000,"metadata":{},"port":9000,"serviceName":"DEFAULT_GROUP@@semantic","weight":1.0}]
2021-08-13 12:16:23.318 INFO 16115 --- [com.alibaba.nacos.client.naming.updater] com.alibaba.nacos.client.naming : current ips:(0) service: DEFAULT_GROUP@@semantic -> []
2021-08-13 12:18:23.338 INFO 16115 --- [com.alibaba.nacos.client.naming.updater] com.alibaba.nacos.client.naming : new ips(1) service: DEFAULT_GROUP@@semantic -> [{"clusterName":"DEFAULT","enabled":true,"ephemeral":true,"healthy":true,"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000,"instanceId":"192.168.0.228#9000#DEFAULT#DEFAULT_GROUP@@semantic","instanceIdGenerator":"simple","ip":"192.168.0.228","ipDeleteTimeout":30000,"port":9000,"serviceName":"DEFAULT_GROUP@@semantic","weight":1.0}]
2021-08-13 12:18:23.339 INFO 16115 --- [com.alibaba.nacos.client.naming.updater] com.alibaba.nacos.client.naming : current ips:(1) service: DEFAULT_GROUP@@semantic -> [{"clusterName":"DEFAULT","enabled":true,"ephemeral":true,"healthy":true,"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000,"instanceId":"192.168.0.228#9000#DEFAULT#DEFAULT_GROUP@@semantic","instanceIdGenerator":"simple","ip":"192.168.0.228","ipDeleteTimeout":30000,"port":9000,"serviceName":"DEFAULT_GROUP@@semantic","weight":1.0}]
2021-08-13 12:19:25.319 ERROR 16115 --- [reactor-http-epoll-8] a.w.r.e.AbstractErrorWebExceptionHandler : [4107f492] 500 Server Error for HTTP POST "/semantic/bot/1a47a3f9a9af49f3"java.lang.NullPointerException: null
2021-08-13 12:20:01.375 ERROR 16115 --- [reactor-http-epoll-3] a.w.r.e.AbstractErrorWebExceptionHandler : [76993041] 500 Server Error for HTTP POST "/semantic/bot/assistant"
java.lang.NullPointerException: null
2021-08-13 12:20:04.657 ERROR 16115 --- [reactor-http-epoll-6] a.w.r.e.AbstractErrorWebExceptionHandler : [99790b9a] 500 Server Error for HTTP POST "/semantic/bot/1a47a3f9a9af49f3"
java.lang.NullPointerException: null
2021-08-13 12:20:08.177 ERROR 16115 --- [reactor-http-epoll-7] a.w.r.e.AbstractErrorWebExceptionHandler : [e2caca67] 500 Server Error for HTTP POST "/semantic/bot/1a47a3f9a9af49f3"
希望能提供获取其他服务feign接口的api
Description
This is a sub-issue of [ISSUE #54]
Add naming.event for module naming
Description
This is the sub-issue of [ISSUE #51]
Add module exception for the new version of Nacos Python SDK.
Description
This is a sub-issue of [ISSUE #54]
Add naming.dtos for module naming
why is the "public" namespace be ""
client.add_naming_instance("", "1.0.0.7", 8080)
client.add_naming_instance("test.service1", "1.0.0.7", 8080)
the cluster name will be None
client.send_heartbeat("test.service1", "1.0.0.7", 8080)
the cluster name will be DEFAULT
Description
This is a sub-issue of [ISSUE #54]
Add naming.core for module naming
Description
This is the sub-issue of [ISSUE #51]
Add module common for the new version of Nacos Python SDK.
This is the sub-issue of [ISSUE #51]
Add module naming for the new version of Nacos Python SDK.
to-do list
The following parts need to be added:
问题如下:
通过域名的形式在浏览器中访问正常,切可以通过client.py中的_do_sync_req函数拿到正确的url拼接参数,但是在_do_sync_req函数中有一个奇怪的操作server_info = self.get_server() if not server_info: logger.error("[do-sync-req] can not get one server.") raise NacosRequestException("Server is not available.") address, port = server_info server = ":".join([address, str(port)]) server_url = "%s://%s" % ("http", server) if python_version_bellow("3"): req = Request(url=server_url + url, data=urlencode(data).encode() if data else None, headers=all_headers) req.get_method = lambda: method else: req = Request(url=server_url + url, data=urlencode(data).encode() if data else None, headers=all_headers, method=method)
直接吧port加到了路由中然后去访问,这样对于通过域名方式访问就是把url拼接为 xxx.com:port ,这样会造成requests请求失败超时。希望可以兼容域名方式访问nacos。谢谢
Description
This is a sub-issue of [ISSUE #54]
Add naming.beat for module naming
code
import datetime
import logging
import random
from flask import Flask
from pyctuator.pyctuator import Pyctuator
import nacos
# Keep the console clear - configure werkzeug (flask's WSGI web app) not to log the detail of every incoming request
logging.getLogger("werkzeug").setLevel(logging.WARNING)
my_logger = logging.getLogger("example")
app = Flask("Flask Example Server")
@app.route("/")
def hello():
my_logger.debug(f"{datetime.datetime.now()} - {str(random.randint(0, 100))}")
print("Printing to STDOUT")
return "Hello World!"
example_app_address = "192.168.1.2"
example_sba_address = "192.168.1.6"
Pyctuator(
app,
"huiche-ai",
app_url=f"http://{example_app_address}:5000",
pyctuator_endpoint_url=f"http://{example_app_address}:5000/pyctuator",
registration_url=f"http://{example_sba_address}:8764/instances",
app_description="Demonstrate Spring Boot Admin Integration with Flask",
)
def regis_server_to_nacos():
SERVER_ADDRESSES = "192.168.1.6"
NAMESPACE = "public"
# no auth mode
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE)
# auth mode
# client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="nacos", password="nacos")
# get config
data_id = "nacos.cfg.dataId"
group = "test"
print(client.get_config(data_id, group))
client.add_naming_instance("heiche-ai", "192.168.1.2", "5000")
if __name__ == '__main__':
regis_server_to_nacos()
app.run(port=5000, host="0.0.0.0",debug=True)
nacos-sdk-python/nacos/client.py
Line 708 in cc9adae
这里长轮询params丢了,部分参数比如tenant不传会导致权限问题
python的注册代码
SERVER_ADDRESSES = "127.0.0.1:8848"
NAMESPACE = "public"
def func():
# no auth mode
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE)
client.send_heartbeat("python", "127.0.0.1", "5000")
print('send heartbeat')
java的获取代码
@RestController
public class TestController {
@Autowired
DiscoveryClient discoveryClient;
@Autowired
RestTemplate restTemplate;
@GetMapping("/hello")
public String sayHello() throws NacosException {
List<ServiceInstance> list = discoveryClient.getInstances("python");
ServiceInstance instance = list.get(0);
}
}
直接就报空指针了
java.lang.NullPointerException: null
at java.util.HashMap.putMapEntries(HashMap.java:500) ~[na:1.8.0_111]
at java.util.HashMap.putAll(HashMap.java:784) ~[na:1.8.0_111]
at com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery.hostToServiceInstance(NacosServiceDiscovery.java:95) ~[spring-cloud-alibaba-nacos-discovery-2.2.0.RELEASE.jar:2.2.0.RELEASE]
这是什么原因啊?
Description
This is the sub-issue of [ISSUE #51]
Add module utils for the new version of Nacos Python SDK.
print(client.get_config(data_id, group))
print("+++++++++++++++++++++++++")
print(type(client.get_config(data_id, group)))
get到的配置是str类型 在flask项目中该如何使用
Description
This is a sub-issue of [ISSUE #54]
Add naming.remote for module naming
nacos日志等级会影响scrapy框架的日志等级设置,请问如何禁用掉nacos的日志输出,或者如何解决这个问题?
Description
This is a sub-issue of [ISSUE #54]
Add naming.utils for module naming
Description
This is the sub-issue of [ISSUE #51]
Add other parts for the new version of Nacos Python SDK.
如题,应用flask注册到nacos,是否还需要uwsgi或者gunicorn呢?
The client is missing parameter to set the proxy, because some environment clients must access the server through the proxy
Description
This is the sub-issue of [ISSUE #51]
Add module remote for the new version of Nacos Python SDK.
I want to ask what is the process of registering an instance?
When I request this path /nacos/v1/ns/instance by post method, it will report 500 error.
Shall I need to create a service firstly on the nacos server?
是否有获取全部data_id的方法呢?如果要批量修改所有app的配置,没有data_id的话 不是很方便
import引用正常,调用里面的函数报错python: symbol lookup error: python: undefined symbol: SSL_CTX_get0_param
调用add_naming_instance
返回500,跟踪了一下发现如下情况。
在add_naming_instance
方法中,有一处对_do_sync_req
的调用:
resp = self._do_sync_req("/nacos/v1/ns/instance", None, None, params, self.default_timeout, "POST")
其中第三个参数None传给了param,第四个参数params传给了data
在_do_sync_req
中会将params放在url中,将data放在body中。
而根据nacos的api规范,参数应该放在url中。所以上面的调用会返回错误。
我的环境:
python:3.6
nacos-sdk-python: 0.1.5
Description
This is a sub-issue of [ISSUE #54]
Add naming.nacos_naming_service for module naming
2.0.0版本nacos,连续发送心跳,但应用健康不连续,会出现一段时间掉线。然后后面又恢复
Add a new function to the subscribe
service when it changed.
For example, this function can subscribe to the service of nacos.test
, and the listener can launch an event to sense the change when service in nacos server has changed.
Recommend method sign as nacos.client.NacosClient.subscribe(service_name:str,listeners:List[EventListener], group_name:str=None)
or nacos.client.NacosClient.subscribe(service_name:str,listener:EventListener, group_name:str=None)
.
Add a new function to the unsubscribe
service.
For example, the client can unsubscribe
service positively which has subscribed.
Recommend method sign as nacos.client.NacosClient.unsubscribe(service_name:str,listener:EventListener group_name:str=None,clusters:List[str]=None)
.
Add a new function to trigger heartbeat schedular and keep instance always be healthy in nacos server.
没有登录模块,不是使用默认账号的话,接口调用有问题
现在的sdk,支持ip+端口,是否可以支持域名连接
Description
This is the sub-issue of [ISSUE #51]
Add module security for the new version of Nacos Python SDK.
Description
https://github.com/alibaba/nacos/issues/5694
to-do list
For a new version of Nacos Python SDK, the following things need to be done.
公司的环境必须用https.而这个客户端我看代码里应该是写死http了.
目前我只能用requests模块获取配置.其他的特性都无法使用.
后续会考虑支持吗?
SERVER_ADDRESSES = "127.0.0.1:8848"
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="csx", password="csx")
print(client.publish_config('test', 'test', json.dumps({'a': 1, 'b': 2}), config_type='json'))
print(client.get_config('test', 'test'))
print(client.remove_config("test", "test"))
print(client.get_config('test', 'test'))
i use 1.0.0.GA nacos and this plugin.
the first problem is that add_naming_instance
will throw exception(HTTP 400),because the default value of paramter metadata
is None,it make the request be 400.
now,the second problem is service will be down after some seconds
if name == "main":
client.add_config_watcher(data_id, group, test_cb)
import time
time.sleep(3000)
Traceback (most recent call last):
File "/Users/lzh/micro_service/PythonStart/nacos_test/main.py", line 29, in
client.add_config_watcher(data_id, group, test_cb)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nacos/commons.py", line 10, in synced_func
return func(*args, **kws)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nacos/client.py", line 519, in add_config_watcher
self.add_config_watchers(data_id, group, [cb], content)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nacos/commons.py", line 10, in synced_func
return func(*args, **kws)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nacos/client.py", line 565, in add_config_watchers
puller.start()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 121, in start
self._popen = self._Popen(self)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 284, in _Popen
return Popen(process_obj)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 32, in init
super().init(process_obj)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_fork.py", line 19, in init
self._launch(process_obj)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 47, in _launch
reduction.dump(process_obj, fp)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_thread.RLock' object
Process finished with exit code 1
def regis_server_to_nacos():
'''
注册到nacos
'''
SERVER_ADDRESSES = "xxxx"
NAMESPACE = "xxxx"
# 获取到本机IP
current_host_ip = get_current_host_ip()
# no auth mode
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE)
group = "XXXXX"
client.add_naming_instance("ner-service", current_host_ip, "80", group_name=group)
def nacos_client_send_heartbeat():
client.send_heartbeat("ner-service", current_host_ip, "80", group_name=group)
return nacos_client_send_heartbeat
看文档只支持到1.3.2
Type: bug report
Description: The NacosRequestException and 500 error are raised while the parameter filled in the function like "client.add_naming_instance(service_name='rpi-test',ip='11.11.11.11',port=8088)"
WARNING:root:[do-sync-req] server:121.36.31.116:8848 is not available for reason:
ERROR:root:[do-sync-req] 121.36.31.116:8848 maybe down, no server is currently available
ERROR:root:[add-naming-instance] exception All server are not available occur
Traceback (most recent call last):
File "/home/pi/.virtualenvs/nacos/lib/python3.7/site-packages/nacos/client.py", line 581, in add_naming_instance
resp = self._do_sync_req("/nacos/v1/ns/instance", None, None, params, self.default_timeout, "POST")
File "/home/pi/.virtualenvs/nacos/lib/python3.7/site-packages/nacos/client.py", line 452, in _do_sync_req
raise NacosRequestException("All server are not available")
nacos.exception.NacosRequestException: All server are not available
Traceback (most recent call last):
File "/home/pi/Downloads/nacos/nacos-client.py", line 27, in
client.add_naming_instance(service_name='rpi-test',ip='11.11.11.11',port=8088)
File "/home/pi/.virtualenvs/nacos/lib/python3.7/site-packages/nacos/client.py", line 581, in add_naming_instance
resp = self._do_sync_req("/nacos/v1/ns/instance", None, None, params, self.default_timeout, "POST")
File "/home/pi/.virtualenvs/nacos/lib/python3.7/site-packages/nacos/client.py", line 452, in _do_sync_req
raise NacosRequestException("All server are not available")
nacos.exception.NacosRequestException: All server are not available
Environment:
It's maybe a solution:
I found it caused by "metadata" and "cluster_name" when they are None instead of ''.
it equal the comand below.
And, client.add_naming_instance(service_name='rpi-test',ip='11.11.11.11',port=8088,cluster_name='',metadata='') is available
这个是抛异常了吧,但是没打出来,日志文件放哪里的呢
如题 一些基础服务的connect需要显示关闭,nacos py需要么?
Description
This is a sub-issue of [ISSUE #54 ]
Add naming.cache for module naming
Description
This is a sub-issue of [ISSUE #54]
Add naming.backups for module naming
/nacos/v1/cs/configs?dataId=&group=&search=accurate&pageNo=1&pageSize=1000&tenant=public&username=nacos&password=nacos
这个接口在命名空间为public时,拿不到数据
初步判断,在官方提供的管理界面上,如果为public时tenant参数应该为空,所以如果命名空间为public时,需要做特殊处理
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.