当前位置:   article > 正文

【DUBBO】python通过nacos调用dubbo接口_python dubbo nacos

python dubbo nacos

一、DUBBO接口请求方法:

目前dubbo接口有很多请求方式,例如:Jmeter利用第三方dubbo-sample插件请求接口,还有封装talent方法,本篇主要借用目前常用方法-python+nacos+dubbo方式

1.1、首先需要安装dubbo-python插件:

我这里下载安装版本为:0.0.4版本

pip install dubbo-python
  • 1

在这里插入图片描述

1.2、建立NacosMethods.py文件:

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'])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

1.3、建立DubboClientNew.py文件:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

1.4、body参数类型为json

    dubboClient = DubboClientNew('nacos地址:端口号', username='nacos账号', password='nacos密码')

     param = {
         "pageNo": 1,
         "pageSize": 10
    }
    result = dubboClient.callDubbo('dubbo接口interface', 'dubbo接口method',
                                    **param
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.5、body参数为int类型

    dubboClient = DubboClientNew('nacos地址:端口号', username='nacos账号', password='nacos密码')

     param = 1
    result = dubboClient.callDubbo('dubbo接口interface', 'dubbo接口method',
                                    param
  • 1
  • 2
  • 3
  • 4
  • 5

1.6、目前支持的数据类型:

:param args: 方法参数
                    1. 对于没有参数的方法,此参数不填;
                    2. 对于只有一个参数的方法,直接填入该参数;
                    3. 对于有多个参数的方法,传入一个包含了所有参数的列表;
                    4. 当前方法参数支持以下类型:
                        * bool
                        * int
                        * long
                        * float
                        * double
                        * java.lang.String
                        * java.lang.Object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

看插件源码即可获得有标注

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/190481
推荐阅读
相关标签
  

闽ICP备14008679号