Hessian跨语言调用实例

Hessian是跨语言基于HTTP的RPC框架,传输的数据格式是二机制。网上找了很多例子,基本上都是同语言之间的RPC调用例子,基本没有跨语言的,自己折腾一下,使用Java编写PRC服务端,使用Java和Python作为客户端调用RPC服务。

Java版的Hessian例子可以参考这个http://www.voidcn.com/blog/qw765811529/article/p-4530324.html

Java 服务端代码:

package app.demo;
import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 153519254199840035L;

    String userName = "snoopy";
    String password = "showme";

    public User(String user, String pwd) {
        this.userName = user;
        this.password = pwd;
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }
}

package app.demo;

public interface BasicAPI {

    public void setGreeting(String greeting);

    public String hello();

    public User getUser();
}

package app.demo;

public class BasicService implements BasicAPI {

    private String _greeting = "Hello, world";

    public void setGreeting(String greeting) {
        _greeting = greeting;
        System.out.println("set greeting success:" + _greeting);
    }

    public String hello() {
        return _greeting;
    }

    public User getUser() {
        return new User("prance", "meshow");
    }
}

Java 客户端代码:
(因为Hessian是跨语言的,所以客户端调用数据返回时,转化成对象并不要求包名,类名一致。当然一般情况下服务端开发的同学会提供客户端调用的同学sdk,sdk里数据返回的类跟服务端是一致的,这里只是为了测试)

public class User2 implements Serializable {

    private static final long serialVersionUID = 153519254199830056L;

    String userName = "snoopy";
    String password = "showme";

    public User2(String user, String pwd) {
        this.userName = user;
        this.password = pwd;
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }
}

public interface BasicAPI2 {

    public void setGreeting(String greeting);

    public String hello();

    public User2 getUser();
}

import app.demo2.BasicAPI2;
import com.caucho.hessian.client.HessianProxyFactory;

public class BasicClient {
    public static void main(String[] args) throws Exception {
        //String url = "http://127.0.0.1:8080/Hessian/hello";
        String url = "http://localhost:8080/EasyHessian/hello";
        HessianProxyFactory factory = new HessianProxyFactory();
        //BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
        BasicAPI2 basic = (BasicAPI2) factory.create(BasicAPI2.class, url);
        System.out.println("Hello:" + basic.hello());
        System.out.println("Hello:" + basic.getUser());
        System.out.println("Hello:" + basic.getUser().getUserName());
        System.out.println("Hello:" + basic.getUser().getPassword());
        basic.setGreeting("HelloGreeting");
        System.out.println("Hello:" + basic.hello());
    }
}

详细完整源码:https://github.com/Kingson4Wu/Frieza-revenge/tree/master/RPC-framework/src/main/java/app/demo

Python客户端代码:

#!python
# encoding: utf-8

'''
使用Python语言实现远程调用
使用Python客户端来进行远程调用,我们可以从https://github.com/bgilmore/mustaine下载,然后安装Hessian的代理客户端Python实现库:
git clone https://github.com/bgilmore/mustaine.git
cd mustaine
sudo python setup.py install

http://san-yun.iteye.com/blog/1628405
http://pydoc.net/Python/mustaine/0.1.7/mustaine.parser/

https://github.com/Kingson4Wu/Frieza-revenge/blob/master/RPC-framework%2Fsrc%2Fmain%2Fjava%2Fapp%2Fdemo%2FBasicService.java

'''


from mustaine.client import HessianProxy
test = HessianProxy("http://localhost:8080/EasyHessian/hello")
print test._headers
print test.hello()
print test.getUser()
print test.getUser().userName
user = test.getUser()
print user.password
test.setGreeting("Hessian Python")
print test.hello()

你可能感兴趣的:(Hessian跨语言调用实例)