远程调用方式总结

1、Hessian

Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据

Hessian实现的样例见:http://www.alisdn.com/wordpress/?p=478

 

2、Web Service

 

3、RMI

spring对rmi进行了封装,操作起来更加的方便

 

  1. package com.alibaba.ural.rmi; 
  2.  
  3. import java.io.Serializable; 
  4. /** 
  5.  * POJO 类 
  6.  * @author keju.wangkj 
  7.  * 
  8.  */ 
  9. public class Account implements Serializable { 
  10.     private static final long serialVersionUID = 2093515007620385610L; 
  11.     private String name; 
  12.  
  13.     public String getName() { 
  14.         return name; 
  15.     } 
  16.  
  17.     public void setName(String name) { 
  18.         this.name = name; 
  19.     } 
  20.  

 

  1. package com.alibaba.ural.rmi; 
  2.  
  3. import java.util.List; 
  4.  
  5. public interface AccountService { 
  6.  
  7.       public void insertAccount(Account acc); 
  8.  
  9.       public List<String> getAccounts(String name); 
  10.     } 

 

  1. package com.alibaba.ural.rmi; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5.  
  6. public class AccountServiceImpl implements AccountService { 
  7.  
  8.       public void insertAccount(Account acc) { 
  9.         // do something 
  10.           System.out.println("rmi invoke: account name -> " + acc.getName()); 
  11.       } 
  12.  
  13.       public List<String> getAccounts(String name) { 
  14.         // do something 
  15.          List<String> list = new ArrayList<String>(); 
  16.          list.add("rmi invoke: " +  name); 
  17.          return list; 
  18.       } 
  19.     } 

 

  1. package com.alibaba.ural.rmi; 
  2.  
  3. import java.rmi.Remote; 
  4. import java.rmi.RemoteException; 
  5. import java.util.List; 
  6.  
  7. public interface RemoteAccountService extends Remote { 
  8.  
  9.     public void insertAccount(Account acc) throws RemoteException; 
  10.  
  11.     public List<String> getAccounts(String name) throws RemoteException; 

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
  4.     xmlns:tx="http://www.springframework.org/schema/tx" 
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
  7.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 
  8.  
  9.  
  10.     <bean id="accountService" class="com.alibaba.ural.rmi.AccountServiceImpl"> 
  11.         <!-- any additional properties, maybe a DAO? --> 
  12.     </bean> 
  13.     <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> 
  14.         <property name="serviceName" value="AccountService" /> 
  15.         <property name="service" ref="accountService" /> 
  16.         <property name="serviceInterface" value="com.alibaba.ural.rmi.AccountService" /> 
  17.          
  18.         <!-- defaults to 1099 --> 
  19.         <property name="registryPort" value="1199" /> 
  20.     </bean> 
  21.  
  22. </beans> 

启动服务:

  1. package com.alibaba.ural.rmi; 
  2.  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  4.  
  5. public class ServerStart { 
  6.     public static void main(String[] args) { 
  7.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 
  8.                 new String[] { "rmiServer.xml" }); // 加载提供者配置 
  9.         context.start(); 
  10.         synchronized (ServerStart.class) { 
  11.             while (true) { 
  12.                 try { 
  13.                     ServerStart.class.wait(); // 阻止JVM退出 
  14.                 } catch (InterruptedException e) { 
  15.                     // ignore 
  16.                 } 
  17.             } 
  18.         } 
  19.     } 

客户端代码:

  1. package com.alibaba.ural.rmi.client; 
  2.  
  3. import com.alibaba.ural.rmi.AccountService; 
  4.  
  5. public class SimpleObject { 
  6.     private AccountService accountService; 
  7.  
  8.     public void setAccountService(AccountService accountService) { 
  9.         this.accountService = accountService; 
  10.     } 
  11.  
  12.     public AccountService getAccountService() { 
  13.         return accountService; 
  14.     } 

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
  4.     xmlns:tx="http://www.springframework.org/schema/tx" 
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
  7.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 
  8.  
  9.  
  10.     <bean id="simpleObject" class="com.alibaba.ural.rmi.client.SimpleObject"> 
  11.         <property name="accountService" ref="accountService" /> 
  12.     </bean> 
  13.  
  14.     <bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
  15.         <property name="serviceUrl" value="rmi://localhost:1199/AccountService" /> 
  16.         <property name="serviceInterface" value="com.alibaba.ural.rmi.AccountService" /> 
  17.     </bean> 
  18.  
  19. </beans> 

开启调用:

  1. package com.alibaba.ural.rmi.client; 
  2.  
  3. import java.util.List; 
  4.  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  6.  
  7. import com.alibaba.ural.rmi.AccountService; 
  8.  
  9. public class Client { 
  10.     public static void main(String[] args) { 
  11.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 
  12.                 new String[] { "rmiClient.xml" }); // 加载消费者配置 
  13.         context.start(); 
  14.         AccountService helloService = ((SimpleObject) context 
  15.                 .getBean("simpleObject")).getAccountService(); // 获取远程服务代理 
  16.         for (int i = 0; i < Integer.MAX_VALUE; i++) { 
  17.             try { 
  18.                 Thread.sleep(1000); 
  19.                 List<String>  hello = helloService.getAccounts("world"); // 执行远程服务 
  20.                 System.out.println("(" + i + ") " + hello.toString()); 
  21.             } catch (Exception e) { 
  22.                 e.printStackTrace(); 
  23.             } 
  24.         } 
  25.     } 

 

4、COBAR