远程调用方式总结

简介:

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



本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/396922,如需转载请自行联系原作者

相关文章
原理篇:Seata TCC模式是如何解决幂等性、资源悬挂、空回滚问题的
原理篇:Seata TCC模式是如何解决幂等性、资源悬挂、空回滚问题的
1939 0
|
存储 缓存 监控
|
消息中间件 RocketMQ Apache
消息队列 MQ产品使用合集之如何修改proxy的端口
阿里云消息队列MQ(Message Queue)是一种高可用、高性能的消息中间件服务,它允许您在分布式应用的不同组件之间异步传递消息,从而实现系统解耦、流量削峰填谷以及提高系统的可扩展性和灵活性。以下是使用阿里云消息队列MQ产品的关键点和最佳实践合集。
440 0
|
人工智能 SpringCloudAlibaba 自然语言处理
SpringCloud Alibaba AI整合DeepSeek落地AI项目实战
在现代软件开发领域,微服务架构因其灵活性、可扩展性和模块化特性而受到广泛欢迎。微服务架构通过将大型应用程序拆分为多个小型、独立的服务,每个服务运行在其独立的进程中,服务与服务间通过轻量级通信机制(通常是HTTP API)进行通信。这种架构模式有助于提升系统的可维护性、可扩展性和开发效率。
5816 2
List集合如何分页
List集合如何分页(List集合转Page分页)
444 0
|
人工智能 自然语言处理 Java
Spring Cloud Alibaba AI 入门与实践
本文将介绍 Spring Cloud Alibaba AI 的基本概念、主要特性和功能,并演示如何完成一个在线聊天和在线画图的 AI 应用。
4494 8
|
XML Java 数据格式
最近很火的SOFARPC是什么?带你快速入门SOFARPC
最近很火的SOFARPC是什么?带你快速入门SOFARPC
最近很火的SOFARPC是什么?带你快速入门SOFARPC
|
安全 Java 程序员
shiro学习三:shiro的源码分析
这篇文章是关于Apache Shiro安全框架的源码分析,主要探讨了Shiro的认证流程和自定义Realm的实现细节。
429 0
shiro学习三:shiro的源码分析
|
NoSQL 关系型数据库 MySQL
当查询的数据来自多个数据源,有哪些好的分页策略?
当查询的数据来自多个数据源,有哪些好的分页策略?
372 9