远程调用方式总结

简介:

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,如需转载请自行联系原作者

相关文章
|
存储 安全 Java
SpringBoot搭建Spring Security 入门
SpringBoot搭建Spring Security 入门
609 0
|
Java 开发工具 对象存储
Aliyun OSS Java SDK超时时间设置
Aliyun OSS Java SDK超时时间设置
17325 0
|
10月前
|
算法 Java 微服务
2025 年 Java 面试宝典社招春招秋招实操全方位攻略
2025年Java面试宝典涵盖核心技术及最新趋势,分为四大板块:1. Java基础:深入数据类型、多态等特性,结合学生信息管理等实例;2. JVM核心:解析内存模型与GC算法,附多线程转账等场景应用;3. 高并发方案:详解synchronized与线程池配置,提供Web服务器优化案例;4. Spring生态:剖析IoC/AOP原理,演示微服务架构实现。特别新增Java 17+特性实操,包括Record类、密封接口等语法糖,整合Spring Boot 3、响应式编程及云原生技术,通过订单状态机、API网关配置。
486 1
|
负载均衡 API 数据格式
RPC和HTTP的区别?
RPC和HTTP的区别?
1380 0
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
1028 0
|
安全 Java 程序员
shiro学习三:shiro的源码分析
这篇文章是关于Apache Shiro安全框架的源码分析,主要探讨了Shiro的认证流程和自定义Realm的实现细节。
339 0
shiro学习三:shiro的源码分析
List集合如何分页
List集合如何分页(List集合转Page分页)
374 0
|
NoSQL 关系型数据库 MySQL
当查询的数据来自多个数据源,有哪些好的分页策略?
当查询的数据来自多个数据源,有哪些好的分页策略?
315 9
|
前端开发 JavaScript API
用的前端框架都有什么
【8月更文挑战第26天】用的前端框架都有什么
1671 2

热门文章

最新文章

下一篇
开通oss服务