**在eureka服务器中发现**其他服务,并注册python组件以供其他服务使用。
py-eureka-client的Python项目详细描述
python eureka客户端
说明
这是一个用python编写的eureka客户端,您可以很容易地将python组件与spring cloud集成。
支持Python版本
python 2.7/3.6+(它也应该在3.5下工作,而不是测试)
为什么选择
- 将python组件注册到eureka服务器。
- 支持多个Eureka服务器注册。
- 将heartbeat发送到eureka服务器。
- 服务器关闭时自动从Eureka服务器注销。
- 来自Eureka服务器的发现应用程序。
- 易于使用的接口使用其他rest服务。
- 当一个节点关闭时自动尝试其他节点。
如何使用
安装
pip install py_eureka_client
开始
这是使用此组件的最简单方法。
importpy_eureka_client.eureka_clientaseureka_clientyour_rest_server_port=9090# The flowing code will register your server to eureka server and also start to send heartbeat every 30 secondseureka_client.init(eureka_server="http://your-eureka-server-peer1,http://your-eureka-server-peer2",app_name="your_app_name",instance_port=your_rest_server_port)
然后,在您的业务代码中,使用
importpy_eureka_client.eureka_clientaseureka_clientres=eureka_client.do_service("OTHER-SERVICE-NAME","/service/context/path")print("result of other service"+res)
有关发现客户端的详细信息,请阅读Use Discovery Client
章节
仅使用注册表客户端
如果您的服务器只提供服务,而不需要其他组件的服务,则只能将您的客户端注册到eureka服务器,而忽略发现客户端。代码如下:
importpy_eureka_client.eureka_clientaseureka_clientyour_rest_server_port=9090# The flowing code will register your server to eureka server and also start to send heartbeat every 30 secondseureka_client.init_registry_client(eureka_server="http://your-eureka-server-peer1,http://your-eureka-server-peer2",app_name="your_app_name",instance_port=your_rest_server_port)
如果您不像上面的示例那样指定主机和IP,客户机将选择一个可以连接到Eureka服务器的主机和IP。
使用发现服务
如果您的服务不提供服务,但希望使用其他组件的服务,则只能使用此发现客户端。
首先,在服务器启动后初始化发现客户端。
importpy_eureka_client.eureka_clientaseureka_clienteureka_client.init_discovery_client("http://192.168.3.116:8761/eureka/, http://192.168.3.116:8762/eureka/")
如果不使用init
或init_discovery_client
,则现在可以使用以下方法来使用其他组件的服务:
这是最简单的服务方式:
importpy_eureka_client.eureka_clientaseureka_clienttry:res=eureka_client.do_service("OTHER-SERVICE-NAME","/service/context/path")print("result of other service"+res)excepturllib.request.HTTPErrorase:# If all nodes are down, a `HTTPError` will raise.print(e)
do_service
函数还接收一个return_type
关键字参数,当传递“json”时,结果将是一个dict
类型的对象。其他参数遵循python2中的urllib.request.urlopen
(urllib2.urlopen
)方法,包括data
等。有关详细信息,请阅读相关文档。
您也可以使用它的async
版本:
importpy_eureka_client.eureka_clientaseureka_clientdefsuccess_callabck(data):# type: (Union[str, dict]) -> object# do what you will use of the result.print(data)deferror_callback(error):# type: (urllib.request.HTTPError) -> object# do what you need to do when error occuresprint(error)eureka_client.do_service_async("OTHER-SERVICE-NAME","/service/context/path",on_success=success_callabck,on_error=error_callback)
do_服务方法将在一个节点返回http错误时自动尝试其他节点,直到一个成功或所有节点被尝试为止。
如果要使用自己的http库来执行请求,请使用walk_nodes
函数:
importpy_eureka_client.eureka_clientaseureka_clientdefwalk_using_your_own_urllib(url):print(url)""" # Connect to url and read result, then return it. # The result you return here will be returned to the `eureka_client.walk_nodes` function # If you want find this node is down, you can raise a `urllib.request.HTTPError`(urllib2.HTTPError in python2) # Then the `eureka_client.walk_nodes` will try to find another node to do the service. """# result is the result that you return in walk_using_your_own_urllib functiontry:res=eureka_client.walk_nodes("OTHER-SERVICE-NAME","/service/context/path",walker=walk_using_your_own_urllib)print(res)excepturllib.request.HTTPErrorase:# If all nodes are down, a `HTTPError` will raise.print(e)
还提供了async
版本:
importpy_eureka_client.eureka_clientaseureka_clientdefwalk_using_your_own_urllib(url):print(url)""" # Connect to url and read result, then return it. # The result you return here will be returned to the `eureka_client.walk_nodes` function # If provided node is down, you can raise a `urllib.request.HTTPError`(urllib2.HTTPError in python2) # Then the `eureka_client.walk_nodes` will try to find another node to do the service. """defsuccess_callabck(data):# type: (Union[str, dict]) -> object# do what you will use of the result.print(data)deferror_callback(error):# type: (urllib.request.HTTPError) -> object# do what you need to do when error occuresprint(error)eureka_client.walk_nodes("OTHER-SERVICE-NAME","/service/context/path",walker=walk_using_your_own_urllib,on_success=success_callabck,on_error=error_callback)
高可用策略
在使用发现客户端时,有几种ha策略。它们是:
- Ha_strategy_random,默认策略,查找节点randally。
- 哈哈策略棒,用一个节点直到它掉下来。
- hau_strategy_other,总是使用与上次不同的节点。
在init
函数或init_discovery_client
中,可以指定上述策略之一:
importpy_eureka_client.eureka_clientaseureka_client# General init methodeureka_client.init(eureka_server="http://your-eureka-server-peer1,http://your-eureka-server-peer2",app_name="your_app_name",instance_port=your_rest_server_port,ha_strategy=eureka_client.HA_STRATEGY_STICK)# If you only use the discovery clienteureka_client.init_discovery_client("http://192.168.3.116:8761/eureka/, http://192.168.3.116:8762/eureka/",ha_strategy=eureka_client.HA_STRATEGY_STICK)
使用其他http客户端
您可以使用其他http客户端连接到eureka服务器和其他服务,而不是使用内置的urlopen方法。如果您通过自签名证书使用https连接,它应该很有用。
为此,您应该:
- 继承
py_eureka_client.http_client
中的HttpClient
类。 - 重写类中的
urlopen
方法。 - 将类设置为
py_eureka_client.http_client
。
importpy_eureka_client.http_clientashttp_client# 1. Inherit the `HttpClient` class in `py_eureka_client.http_client`.classMyHttpClient(http_client.HttpClient):# 2. Rewrite the `urlopen` method in your class.# If you want to raise an exception, please make sure that the exception is an `urllib.error.HTTPError` or `urllib.error.URLError`# (urllib2.HTTPError or urllib2.URLError in python 2), or it may cause some un-handled errors.defurlopen(self):# The flowing code is the default implementation, you can see what fields you can use. you can change your implementation hereres=urllib2.urlopen(self.request,data=self.data,timeout=self.timeout,cafile=self.cafile,capath=self.capath,cadefault=self.cadefault,context=self.context)ifres.info().get("Content-Encoding")=="gzip":try:# python2f=gzip.GzipFile(fileobj=StringIO(res.read()))exceptNameError:f=gzip.GzipFile(fileobj=res)else:f=restxt=f.read().decode(_DEFAULT_ENCODING)f.close()returntxt# 3. Set your class to `py_eureka_client.http_client`. http_client.set_http_client_class(MyHttpClient)
停止客户端
<>此模块将在程序正常退出时自动停止和注销EURKA服务器。(使用@atexit
),但是,如果要自己停止,请使用以下代码:importpy_eureka_client.eureka_clientaseureka_clienteureka_client.stop()