赞
踩
目前dubbo接口有很多请求方式,例如:Jmeter利用第三方dubbo-sample插件请求接口,还有封装talent方法,本篇主要借用目前常用方法-python+nacos+dubbo方式
我这里下载安装版本为:0.0.4版本
pip install dubbo-python
import json
from dubbo.client import NacosRegister
from dubbo.common.constants import DUBBO_NC_PROVIDERS
from dubbo.common.exceptions import RegisterException
class NacosMethods(NacosRegister):
def get_methods_from_nacos(self, interface, version):
""" 从nacos中根据interface获取到providers信息,methods信息 :param interface: :param version: :return: """
service = DUBBO_NC_PROVIDERS.format(interface, version)
providers = self.nc.get_service_list(timeout=self.timeout, group_name=self.group_name,
namespace_id=self.namespace_id)
if not providers or service not in providers:
raise RegisterException('no providers for service {}'.format(service))
self.nc.subscribe([], service_name=service)
services = self.nc.subscribed_local_manager.get_local_instances(service) or {}
self.close()
instance_r = None
for k, v in services.items():
service = v.__dict__
instance = service.get('instance')
instance_r = instance
if not isinstance(instance, list): instance = [instance]
hosts = []
for ins in instance:
host = '{}:{}'.format(ins.get('ip'), ins.get('port'))
hosts.append(host)
self.hosts[interface] = hosts
return json.loads(instance_r['metadata']['methodParamType'])
from dubbo.client import NacosRegister, DubboClient
from dubbo.codec.encoder import Object
from Dubbo.NacosMethods import NacosMethods
from pprint import pprint
class DubboClientNew:
def __init__(self, host, username, password):
self.host = host
self.username = username
self.password = password
def callDubbo(self, className, methodName, *args, **param):
methods = NacosMethods(self.host, self.username, self.password) \
.get_methods_from_nacos(className, ':')
nc = NacosRegister(self.host, self.username, self.password)
methodNames = methods.keys()
obj = ''
for method in methodNames:
if method == methodName:
obj = methods[method][0]
queryRequest = Object(obj)
if args != ():
queryRequest = args
elif param != {}:
for key in param.keys():
queryRequest[key] = param[key]
dubbo_cli = DubboClient(className, nacos_register=nc, version=':')
result = dubbo_cli.call(methodName, queryRequest)
return result
if __name__ == '__main__':
dubboClient = DubboClientNew('nacos地址:端口号', username='nacos账号', password='nacos密码')
param = {
"pageNo": 1,
"pageSize": 10
}
result = dubboClient.callDubbo('dubbo接口interface', 'dubbo接口method',
**param)
pprint(result)
dubboClient = DubboClientNew('nacos地址:端口号', username='nacos账号', password='nacos密码')
param = {
"pageNo": 1,
"pageSize": 10
}
result = dubboClient.callDubbo('dubbo接口interface', 'dubbo接口method',
**param
dubboClient = DubboClientNew('nacos地址:端口号', username='nacos账号', password='nacos密码')
param = 1
result = dubboClient.callDubbo('dubbo接口interface', 'dubbo接口method',
param
:param args: 方法参数
1. 对于没有参数的方法,此参数不填;
2. 对于只有一个参数的方法,直接填入该参数;
3. 对于有多个参数的方法,传入一个包含了所有参数的列表;
4. 当前方法参数支持以下类型:
* bool
* int
* long
* float
* double
* java.lang.String
* java.lang.Object
看插件源码即可获得有标注
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。