Python calls an example of a Hessian protocol interface based on Dubbo

Source: Internet
Author: User
Tags json serialization


About the Dubbo Hession protocol:

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 the implementation of these protocols is encapsulated, whether the server (development services) or the client (invoke the service), do not need to care about the details of the protocol, only in the configuration to specify the use of the Protocol to ensure that the service provider and service consumers between the transparency.

If we use the Dubbo Service registry component, so that the service provider publishes the service to the registered center, only exposes the name of the service to the outside, and the service consumer only needs to know the name of the service provided by the Registry and the service provider to invoke the service transparently.

Python calls the Hession protocol interface with the following basic process:

1. Download and install the Python Hessian Library, address: Https://github.com/theatlantic/python-hessian

2. Define the service, interface, method name of the Hession interface

3. Using the Protocol.object_factory method, call the method parameter construction method, construct the call parameter

4. Using the Hessianproxy method, call the interface

Sample code:
#-*-Coding:utf-8-*-
From pyhessian.client import Hessianproxy
From Pyhessian Import Protocol
Import JSON

Def invokehessian (service,interface,method,req,retcode= ' 000000 '):
    try:
         url= ' http://192.168.0.1:10883/' +service+ '. +interface
       
        Print url:\t%s '%url
        print ' method:\t%s '%method
         print ' req:\t%s '%req
        res=getattr ( Hessianproxy (URL), method) (req)
        print ' res:\t%s '%json.dumps Res, Ensure_ascii=false)
       
    except exception,e:
        print e
       

if __name__ = = ' __main__ ':
Service= ' Com.service.common.api.service '
Interface= ' Testhessianservice '
Method= ' Testhessian '
Req=protocol.object_factory (' Com.service.common.api.service.model.req.TestHessianRequest ',
Param1= ' Lovesoo ', param2=10086)

Invokehessian (Service, interface, Method,req)

Attached, official example demo:https://github.com/theatlantic/python-hessian/blob/master/tests/test_parser.py

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.