远程调用方式总结

简介:

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

相关文章
|
6月前
|
Java 机器人 Maven
【Java用法】微服务之间的相互调用方式之一,通过FeignClient客户端调用其他微服务的方法包含熔断器(Hystrix)
【Java用法】微服务之间的相互调用方式之一,通过FeignClient客户端调用其他微服务的方法包含熔断器(Hystrix)
81 0
|
设计模式 负载均衡 Nacos
远程调用 OpenFeign 底层原理解析
Feign 是Springcloud 提供一个声明式的伪Http客户端 它使得调用远程服务就像调用本地服务一样简单 只需要创建一个接口 并且添加注解就可以 Nacos 很好的兼容Feign Feign 默认集成了Ribbon 所以在Nacos 下使用Fegin 默认就实现了负载均衡的效果
1461 0
远程调用 OpenFeign 底层原理解析
|
29天前
|
API 数据库管理
远程调用-其他服务
远程调用-其他服务
19 1
|
1月前
|
XML JSON 网络协议
RPC远程服务如何调用
【2月更文挑战第12天】一个完整的 RPC 调用框架包括:通信框架、通信协议、序列化和反序列化三部分。
|
4月前
|
Java Nacos Maven
服务注册、发现和远程调用
服务注册、发现和远程调用
35 0
|
5月前
解决Feign远程调用参数里面内容丢失的问题
解决Feign远程调用参数里面内容丢失的问题
100 0
|
9月前
|
存储 搜索推荐 API
如何设计 RPC 接口
如何设计 RPC 接口
178 0
|
10月前
|
网络协议 Dubbo Java
【远程调用框架概述 一】基于HTTP和RPC的远程调用方式
【远程调用框架概述 一】基于HTTP和RPC的远程调用方式
253 0
|
11月前
|
消息中间件 XML JSON
一文就读懂RPC远程调用核心原理
rpc的全称是Remote Procedure Call,即远程过程调用,是分布式系统的常用通信方法。 Remote,简单来说的话就是两个不同的服务之间,两个服务肯定是两个不同的进程。因此,我们就从跨进程进行访问的角度去理解就行了。 Procedure,意思是一串可执行的代码,我们写Java的方法,就是一段课程行的代码。 Call,即调用,调用的就是跨了进程的方法。
263 0
一文就读懂RPC远程调用核心原理
|
11月前
|
Java API Maven
动态代理-RPC实现核心原理
实现过统一拦截吗?如授权认证、性能统计,可以用 Spring AOP,不需要改动原有代码前提下,还能实现非业务逻辑跟业务逻辑的解耦。核心就是动态代理,通过对字节码进行增强,在方法调用时进行拦截,以便于在方法调用前后,增加处理逻辑。
130 0