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

本文介绍如何使用Python服务注册到Spring Cloud中,并实现服务发现。通过Py-Eureka-Client库,Python服务能够像Java服务一样注册到Eureka服务中心,便于统一管理和调用。

使用背景

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

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

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

本地环境

python3.7

依赖包

pip install py_eureka_client

api地址

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

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

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.cloud

spring-cloud-dependencies

Finchley.RELEASE

pom

import

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)

高可用策略

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

HA_STRATEGY_RANDOM,默认策略,随机找到一个节点。

HA_STRATEGY_STICK,使用一个节点,直到它崩溃为止。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值