IT小白

python注册eureka+spring-cloud 服务互调

2020-07-23  本文已影响0人  边学边记

使用背景

作为一个java开发,大部分应该都使用过 dubbo 和 springcloud 框架;java的服务模块可以做到方便的注册和发现,现今有基于python的功能模块想要直接像java服务一样发现和管理~

之前考虑过注册到dubbo,可惜还没成功;

注册到springcloude 目前测试是可以的,可以在和java模块一起管理,公用java的权限拦截器,geteway路由等功能,还是比较方便的。

本地环境

python3.7

依赖包

pip install py_eureka_client

api地址

https://pypi.org/project/py-eureka-client/

eureka服务 是java的spring-cloud启动的

java依赖包,启动的eureka服务中心

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Finchley.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

python 服务注册

服务注册样例

'''https://pypi.org/project/py-eureka-client/   服务注册样例'''
# coding:utf-8
import tornado.httpserver
import tornado.ioloop
import tornado.options
from tornado.web import RequestHandler
import py_eureka_client.eureka_client as eureka_client
from tornado.options import define, options

define("port", default=8011, help="run on the given port", type=int)


# restfulAPI功能实现在这里
class Info_handler(RequestHandler):
    def get(self):
        self.write("my is info url ")


# restfulAPI功能实现在这里
class Test_handler(RequestHandler):
    def get(self):
        self.write("my is test url ")


# 注册服务
def eurekaclient():
    tornado.options.parse_command_line()
    # 注册eureka服务
    eureka_client.init_registry_client(eureka_server="http://localhost:5000/eureka/",
                                       app_name="python-service",
                                       instance_port=8011)

    # 提供外部调用的接口
    app = tornado.web.Application(handlers=[(r"/info", Info_handler),
                                            (r"/python/test", Test_handler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
    print("eureka exec")


if __name__ == "__main__":
    eurekaclient()

服务调用样例:

import py_eureka_client.eureka_client as eureka_client


# 注册并发现服务
eureka_client.init(eureka_server="http://localhost:5000/eureka/", app_name="do_service", instance_port=9090)

res = eureka_client.do_service("python-service", "/python/test")
print("result of other service" + res)

客户端初始化注册类型

注册并发现服务

这是使用此组件的最简单方法。注册自己,并可以获取eureka中心的服务列表信息(即可以通过服务名调用其他服务接口)

eureka_client.init(eureka_server="http://localhost:5000/eureka/", app_name="do_service", instance_port=9090)

仅使用注册表客户端

如果您的服务器仅提供服务,而无需其他组件的服务,则只能将客户端注册到Eureka服务器,而忽略发现客户端。

eureka_client.init_registry_client(eureka_server="http://localhost:5000/eureka/", app_name="do_service", instance_port=9090)

使用发现服务

如果您的服务不提供服务,但想使用其他组件的服务,则只能使用此发现客户端。

首先,在服务器启动后初始化发现客户端。

eureka_client.init_discovery_client("http://localhost:5000/eureka/",ha_strategy=eureka_client.HA_STRATEGY_STICK)

高可用策略

使用发现客户端时,有几种高可用性策略。 他们是:

  1. HA_STRATEGY_RANDOM,默认策略,随机找到一个节点。
  2. HA_STRATEGY_STICK,使用一个节点,直到它崩溃为止。
  3. HA_STRATEGY_OTHER,请始终使用与上次不同的节点。
# General init method
eureka_client.init(eureka_server="http://your-eureka-server-peer1,http://your-eureka-server-peer2",
                   app_name="your_app_name",
                   instance_port=9090,
                   ha_strategy=eureka_client.HA_STRATEGY_STICK)

# If you only use the discovery client
eureka_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)

调用方式

需要客户端注册 方式是可以发现服务列表的

常用的同步调用

eureka_client.init(eureka_server="http://localhost:5000/eureka/", app_name="do_service", instance_port=9090)

res = eureka_client.do_service("python-service", "/python/test")
print("result of other service" + res)

异步调用

def success_callabck(data):
    # type: (Union[str, dict]) -> object
    # do what you will use of the result.
    print("success_callabck")
    print(data)


def error_callback(error):
    # type: (urllib.request.HTTPError) -> object
    # do what you need to do when error occures
    print("error")
    print(error)


# 异步调用
eureka_client.do_service_async("python-service", "/test", on_success=success_callabck,on_error=error_callback)

其它使用方式 请参考 py-eureka-client api

https://pypi.org/project/py-eureka-client/

上一篇 下一篇

猜你喜欢

热点阅读