Dubbo接口调用
一 命令行调用dubbo远程服务
命令行调用dubbo远程服务
telnet远程连接到dubbo
telnet 127.0.0.1 20880
查看提供服务的接口
dubbo>ls
com.test.service.TestInfoQueryService
ls 接口名对外提供的方法
dubbo>ls com.test.service.TestInfoQueryService
queryByInfoCode
queryInfo
调用服务
invoke 接口名.方法名(参数) 进行调用
dubbo>invoke com.test.service.TestInfoQueryService.queryByInfoCode("00000A0")
{"result":{"infoCode":"info0","stat":"001","ip":"192.168.1.0","infoOwncode":"自编号0","phone":"13600000","infoAreaCode":"12345","address":"地址0","date":"2017-01-22 00:00:00","addressType":"0001","name":"名称0","id":14001,"contacts":"联系人0","infoCode":"00000A0"},"errorCode":"INFOCODE000000","errorMsg":"获取信息成功。"}
elapsed: 12 ms.
二 Python调用dubbo远程服务
调用服务
import json import socket import telnetlib class dubbo: __init = False __encoding = "gbk" __finish = "dubbo>" __connect_timeout = 10 __read_timeout = 10 def __init__(self, host, port): self.host = host self.port = port if host is not None and port is not None: self.__init = True def set_finish(self, finish): """ defualt is ``dubbo>`` """ self.__finish = finish def set_encoding(self, encoding): """ If ``result retured by dubbo`` is a ``str`` instance and is encoded with an ASCII based encoding other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to ``unicode`` first. """ self.__encoding = encoding def set_connect_timeout(self, timeout): """ Defines a timeout for establishing a connection with a dubbo server. It should be noted that this timeout cannot usually exceed 75 seconds. defualt is ``10`` """ self.__connect_timeout = timeout def set_read_timeout(self, timeout): """ Defines a timeout for reading a response expected from the dubbo server. defualt is ``10`` """ self.__read_timeout = timeout def do(self, command): try: tn = telnetlib.Telnet(host=self.host, port=self.port, timeout=self.__connect_timeout) except socket.error as err: print("[host:%s port:%s] %s" % (self.host, self.port, err)) return tn.write(b"\n") #tn.read_until(b"self.__finish", timeout=self.__read_timeout) tn.write(command.encode("ascii")+ b"\n") """ data = "" while data.find(self.__finish) == -1: data = tn.read_very_eager() data = data.split("\n") try: data = json.loads(data[0], encoding=self.__encoding) except Exception as ValueError: data=str(data[0]).decode(encoding="gbk") """ tn.close() # tn.write("exit\n") #return data def invoke(self, interface, method, param): command = "%s %s.%s(%s)" % ("invoke", interface, method, param) return self.do(command) def connect(host, port): return dubbo(host, port) if __name__ == "__main__": Host = "10.12.xx.xx" Port = "20880" conn = dubbo(Host, Port) conn.set_connect_timeout(15) conn.set_encoding("UTF-8") interface = "com.cmiot.uom.service.order.hoa.OrderUnboundLicense" method = "invoke" param = "{"class":"com.cmiot.dhap.core.impl.PoolableBaseContext"},{"days":"1","num":"2","packageCode":"test_001","storeId":"100000000000000001"}" print(conn.invoke(interface, method, param))
对于使用 python 的兄弟,使用 telnetlib 搭建 dubbo 测试框架非常好用,亲测有效!不过有个限制就是 dubbo 的接口必须都是接受 string 类型的请求参数!