利用python模拟dubbo+zk框架的rpc接口测试

基于阿里开源的dubbo框架,利用zookeeper作为注册中心,利用python模拟rpc协议接口进行测试。
1、首先从pypi上安装kazoo和dubbo_telnet两个包
2、利用kazoo连接zk注册中心拉取服务列表
3、利用dubbo_telnet,通过telnet的方式,模拟rpc的服务调用。

#coding=utf8
import sys
from kazoo.client import KazooClient
import urllib
import urllib3
import json
import telnetlib
import socket
import dubbo_telnet

Host = '192.168.1.1:2181'  # zookeeper的地址

def coondoubble_data(servhost, servport,interface,method,param):
    try:
        # 初始化dubbo对象
        conn = dubbo_telnet.connect(servhost, servport)
        # 设置telnet连接超时时间
        conn.set_connect_timeout(10)
        # 设置dubbo服务返回响应的编码
        conn.set_encoding('utf8')
        data=conn.invoke(interface, method, param)
        return data
    except Exception as e:
        return  e

def get_dubbo_full():
    global Host
    zk = KazooClient(hosts="{}".format(Host))
    zk.start()
    urls = []
    list = zk.get_children("dubbo")
    for i in list:
        if 'serv' in i: #关键字过滤
            # print i
            try:
                gg = zk.get_children("/dubbo/{}/providers".format(i))
                if gg:
                    for j in gg:
                        url = urllib.parse.unquote(j)
                        if url.startswith('dubbo:'):
                            urls.append(url.split('?')[0].split('dubbo://')[1])
            except Exception as e:
                print(e)
                print(i)
    services = {}
    for i in urls:
        try:
            path, service = i.split('/')
            if not services.get(path):
                services.update({path: []})
            services[path].append(service)
        except Exception as e:
            pass
    print(json.dumps(services, indent=4))

def get_dubbo(servername):
    global Host
    zk = KazooClient(hosts="{}".format(Host))
    zk.start()
    urls = []
    list = zk.get_children("dubbo")
    for i in list:
        if servername in i:
            try:
                # 获取服务发布方
                gg = zk.get_children("/dubbo/{}/providers".format(i))
                if gg:
                    for j in gg:
                        url = urllib.parse.unquote(j)
                        if url.startswith('dubbo:'):
                            urls.append(url.split('dubbo://')[1])
            except Exception as e:
                print(e)
    paths=[]
    for i in urls:
        try:
            path, temp = i.split('/')
            service=temp.split('?')[0]
            method=temp.split('methods=')[1].split('&')[0].split(',')
            paths.append(path)
        except Exception as e:
            print(e)
    services = {"service": service, "paths": paths, "method": method}
    print(json.dumps(services, indent=4))

if __name__ == '__main__':
    #使用方法get_dubbo('<服务名>')
    get_dubbo('com.serv.finance.service.CommReimQueryService__1_0')
    get_dubbo('com.serv.finance.ats.CommReimQueryATS__1_0')
    param='''{"batSubmitId":null, "logonId":"8ybbmd01.c.0200", "len":"20", "groupId":"020090006110982", "start":"0", "logonLevel":"3", "limitSum1":"40000", "limitSum":"90000000000000000", "endTime":"20200101", "beginTime":"20190101", "batFileName":null, "channelFlag":"1"}'''
    #使用方法coondoubble_data('服务提供者ip','服务提供者port','服务名','调用方法','发送数据')
    #'服务提供者ip','服务提供者port','服务名','调用方法'从get_dubbo()返回结果中获取
    resp=coondoubble_data("192.168.1.100", "35009", "com.serv.finance.service.CommReimQueryService__1_0", "commReimQueryBatch", param)
    print(resp)

代码并非原创,部分参考了一位前辈的文章,但是忘了出处,特此说明

你可能感兴趣的:(自动化测试,接口测试)