Python, Java big battle, Python test Dubbo interface

Source: Internet
Author: User

Many small partners reflect the company asked to Test Dubbo (Dubbo is a Java distributed open-source framework) interface, will not write Java code, how to test, can be used in Python to Tune Dubbo interface. Of course, it is possible, recently studied, very simple, share to everyone.
About Dubbo This framework we do not describe too much, development, we only focus on how to call on the line, want to understand the principle of this article can be seen http://www.cnblogs.com/Javame/p/3632473.html
The Dubbo itself supports a variety of remote invocation methods, such as Dubbo RPC (binary serialization + TCP protocol), HTTP invoker (binary serialization + HTTP protocol), Hessian (binary serialization + HTTP protocol), WebServices (text serialization + HTTP protocol) and so on.
Dubbo is supported for Hessian+http protocol invocation, Hessian is a binary serialization method. When we call Dubbo with Python, we call it in hessian+http way, so the Dubbo project is configured to be serialized using Hessian, and if the small partner wants to invoke it with Python, Note to find the development of small brother in the project to change to Hessian mode of serialization, that is, change the configuration file, do not affect the original project, such as:

When we call with Python, hessian+http this way, we need to install a third-party module, Python-hessian this module, direct PIP installation:

12 pip Install python-hessian

Of course, we want to call the Dubbo interface, we need to know the Dubbo interface call address, methods, parameters and parameters, this needs to develop a small brother to provide you with the document.

If you don't have a document, you'll need to be able to read Java and Dubbo code. I have no documents here, just say how to find these we need to use.

1, first find the call address, interface, method.

Dubbo is a function with service monitoring, this has, tube development to address on the line, the inside can see you want to test the service, his inside address, method, such as, we can see in Dubbo service monitoring inside there is a HELLOAPI service:

Then we take this service to go in, we can see that this service is deployed on which server, and then click on the server's IP in, you can see the call address, interface, and methods, respectively:

Call Address:Http://192.168.1.100:8181/api/yz.dubbo.api.HelloApi, #那个页面里写的是hessian, we use the HTTP protocol to send, We'll change it to HTTP when we use it here.

method:Hello

Such as:

2. Find the Entry object and enter the parameter

Through the service monitoring of Dubbo, we can get the call address, interface, the entry parameter object and the entry to see the code, we open the project code, See the entry type is the Param object in the param package below Yz.dubbo.api, then the Parameter object is Yz.dubbo.api.param.Param, then we can see that there are several properties in this object, that is, its arguments, a string type of Sth, an integer array in TS, a string key value to maps, corresponds to the data type of our Python is a string, a list, a dictionary.

Entry object:Yz.dubbo.api.param.Param
entry:sth, INTs, maps


3. Call

Through the above things, we call the Dubbo need to use everything is ready, we encapsulate a function to call, the following is the code, write the comments

12345678910111213141516171819202122232425262728293031323334353637 from Pyhessian. Client import hessianproxy #从pyhessian导入HessianProxy, use it to send a requestfrom pyhessian import protocol #这个是用来进行把咱们python的数据类型序列化成二进制的 def dubbo_api(URL,interface,method,param_obj,** Kwargs): " ":p Aram Url:url address:p Aram Interface: interface name, because there may be other services to test, the interface name is not the same, this is defined as a variable:p Aram Method: What to call:p Aram Param_obj: An Entry object:p Aram Kwargs: This keyword parameter, because each interface parameters are different, not fixed, so here with the keyword parameter: return:" " req_param = protocol. Object_factory(param_obj,**Kwargs) #这个是用来构造二进制的入参的, that is, the serialization of the incoming parameter try:#用try捕捉一下异常 req_obj = hessianproxy(URL+interface) #这个req是生成一个请求对象 res = getattr(req_obj,method)(req_param) #getattr是python的内置方法, get the method of the object, we get the method from the constructed request object, and then call, put the previous generated #序列化好的参数传进去, and then gets the data returned to the except Exception as e: Print(' There is an exception, the exception information is:%s '%e) res = {"msg":"Exception:%s"%e,"code":$ #这个是自己定义的异常, if the call goes wrong, return this return res if __name__ = = ' __main__ ': url = ' http://192.168.1.100:8181/api/' interface = ' yz.dubbo.api.HelloApi ' method = ' Hello ' param_obj = ' yz.dubbo.api.param.Param '     params = { "STH" : "Dubbo" , "INTs" Span class= "Crayon-o" >:[1,2,3 , "maps" :{ "name" : "Dubbo" }" #这个入参, in order to not define more than one variable, we write it in a dictionary form, and it is the same as the Stu=dubbo call. Over = dubbo_api(URL,interface,method,param_obj ,**params) #测试调用一下 Print(over)#打印结果

Run the results to see:

The steps may be a bit more written, but it's easy to do it, so go ahead and try it.

Python, Java big battle, Python test Dubbo interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.