python通过telnet接口访问dubbo服务

本文介绍了如何使用telnet命令对Dubbo服务进行调试,包括连接服务、调用方法和查看返回结果。同时,提供了一个Python实现的Dubbo客户端,用于更便捷地调用Dubbo服务的方法,通过示例展示了如何通过Python脚本进行服务调用。
摘要由CSDN通过智能技术生成

##简介

dubbo服务发布之后,我们可以利用telnet命令进行调试、管理。更多资料参见Telnet命令参考手册

telnet 调用示例:

$ telnet 172.17.103.110 9097
Trying 172.17.103.110...
Connected to 172.17.103.110.
Escape character is '^]'.

dubbo>ls com.oppo.sso.service.onekey.IOnekeyRegister
register
dubbo>invoke com.oppo.sso.service.onekey.IOnekeyRegister.register({"applicationKey":"mac","imei":"","mobile":"13244448888","createIP":"127.0.0.1","createBy":"172.17.0.1"})
{"configCountry":null,"userIdLong":0,"appPackage":null,"appVersion":null,"accountName":null,"romVersion":null,"resultCode":3001,"thirdStatus":null,"registerType":0,"sendChannel":null,"operator":null,"manufacturer":null,"password":null,"osVersion":null,"lock":false,"model":null,"visitorLocked":false,"OK":false,"brand":null,"email":null,"createIP":null,"deny":false,"encryptEmail":null,"sessionKey":null,"thirdId":null,"passwordOriginal":null,"mobile":null,"applicationKey":null,"thirdpartyOk":false,"userAgent":null,"userName":null,"resultDesc":"应用不存在","userId":0,"encryptMobile":null,"emailStatus":null,"createBy":null,"freePwd":false,"changeTimes":0,"createTime":null,"mobileStatus":null,"oldLock":false,"codeTimeout":null,"lastUpdate":null,"imei":null,"sessionTimeout":0,"sdkVersion":null,"networkID":0,"status":null}
elapsed: 98 ms.
dubbo>

这个参数 我要说一下 如果直接调用服务端java 很多教程都是传递的json参数
而dubbo端接收的可能不是json,所以如果不是 请直接传一个list参数即可 換一種方法说就是不要写参数名 直接写参数 也就是
command = ‘invoke’ + dubbo 服务名 + 方法名 (‘1165’)

Python实现


import json
import telnetlib


class Dubbo(telnetlib.Telnet):

    prompt = 'dubbo>'
    coding = 'utf-8'

    def __init__(self, host=None, port=0,):
        super().__init__(host, port)
        self.write(b'\n')

    def command(self, flag, str_=""):
        data = self.read_until(flag.encode())
        self.write(str_.encode() + b"\n")
        return data

    def invoke(self, service_name, method_name, arg):
        command_str = "invoke {0}.{1}{2}".format(
            service_name, method_name,arg
        self.command(Dubbo.prompt, command_str)
        data = self.command(Dubbo.prompt, "")
        data = json.loads(data.decode(Dubbo.coding,
                                      errors='ignore').split('\n')[0].strip())
        return data



    def do(self, arg):
        command_str = arg
        print(command_str)
        self.command(Dubbo.prompt, command_str)
        data = self.command(Dubbo.prompt, command_str)
        print(data)
        data = data.decode(Dubbo.coding, errors='ignore').split('\n')[1].strip()
        print(data)
        return data
if __name__ == '__main__':

    conn = Dubbo('172.17.103.110', 9097)
    command_str1 = 'invoke com.welab.loan.finance.service.CreditUserAccountService.findByUserAndProductCode(624101,"abcded")'
    print(conn.do("ls"))#打印所有服务,方便调试
    print(conn.do(command_str1))
    data=(624101,"abcded")
    result = conn.invoke(
        "com.oppo.sso.service.onekey.IOnekeyRegister",
        "register",
        data
    )
    print(result)```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值