zoukankan      html  css  js  c++  java
  • 利用python脚本和telnet调试dubbo接口

    原文地址:https://hhyo.github.io/2017/10/20/python-dubbo-telnet/

    最近在测试项目中接触到dubbo框架,由于业务逻辑复杂,前台一个业务流程在后端会依赖多个服务提供数据,而各方开发进度也不完全一致。在业务测试脚本编写完成后,希望能够在项目整体提测前,先验证部分已提供的dubbo接口的可用性。另外一方面,也能够在测试过程中更快的定位到具体的服务提供者,指派任务,减少反复沟通定位问题所耗费的时间。

    先说环境:
    OS: macOS High Sierra
    python: 2.7

    步骤:

    1. 准备python环境,pip安装dubbo_telnet:pip install dubbo_telnet
    2. 编写如下调试脚本
    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
    #-*- coding: utf-8 -*-

    import dubbo_telnet
    import json
    Host = '192.168.0.1' # Doubble服务器IP
    Port = 9036 # Doubble服务端口

    # 初始化dubbo对象
    conn = dubbo_telnet.connect(Host, Port)

    # 设置telnet连接超时时间
    conn.set_connect_timeout(10)

    # 设置dubbo服务返回响应的编码
    conn.set_encoding('gbk')

    # 显示服务列表
    print conn.do("ls")

    # 显示指定服务的方法列表
    print conn.do("ls XXXService")

    # 方法调用
    interface = 'XXXService'
    method = 'userinfo'
    param = user_id
    result = conn.invoke(interface, method, param)

    print json.dumps(result, sort_keys=True, indent=4, separators=(',', ': '), skipkeys=True, ensure_ascii=False)

    这时候运行脚本,抛错

    1
    2
    3
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

    检查site-packages/dubbo_telnet/init.py文件

    1
    2
    3
    4
    5
    6
    7
    data = ''
    while data.find(self.__finish) == -1:
    data = tn.read_very_eager()
    data = data.split(" ")
    data = json.loads(data[0], encoding=self.__encoding)
    tn.close() # tn.write('exit ')
    return data

    可以知道是在对返回结果处理时出了异常,无法转换为python对象,在这里加上一个错误处理

    1
    2
    3
    4
    5
    try:
    data = json.loads(data[0], encoding=self.__encoding)
    except Exception as ValueError:
    data = data
    #data = json.loads(data[0], encoding=self.__encoding)

    就可以正常返回结果,可以进行简单的dubbo请求和查看响应了。其实整个过程就是利用telnet命令来连接和操作dubbo服务,也可以直接在命令行中进行。

    附上dubbo的telnet命令:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Please input "help [command]" show detail.
    status [-l] - Show status.
    pwd - Print working default service.
    trace [service] [method] [times] - Trace the service.
    exit - Exit the telnet.
    help [command] - Show help.
    invoke [service.]method(args) - Invoke the service method.
    count [service] [method] [times] - Count the service.
    clear [lines] - Clear screen.
    ls [-l] [service] - List services and methods.
    log level - Change log level or show log
    ps [-l] [port] - Print server ports and connections.
    cd [service] - Change default service.
  • 相关阅读:
    virtualbox中ubuntu和windows共享文件夹设置
    嵌入式Linux USB WIFI驱动的移植
    U-boot mkimage指定Linux内核地址时的两种方式
    ramdisk作为根文件系统的配置
    ARM Linux系统的时钟机制
    ARM Linux 如何--注册和触发--软中断
    FAT32文件系统--For TF卡
    如何编写Linux设备驱动程序
    WiFi基本知识 .
    idea插件篇之java内存分析工具(JProfiler)
  • 原文地址:https://www.cnblogs.com/slqt/p/11346923.html
Copyright © 2011-2022 走看看