1. 程式人生 > >Hessian跨語言呼叫例項

Hessian跨語言呼叫例項

Hessian是跨語言基於HTTP的RPC框架,傳輸的資料格式是二機制。網上找了很多例子,基本上都是同語言之間的RPC呼叫例子,基本沒有跨語言的,自己折騰一下,使用Java編寫PRC服務端,使用Java和Python作為客戶端呼叫RPC服務。

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());
    }
}

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()