Spring 实现远程访问详解——rmi
来源:互联网 发布:web storm mac 编辑:程序博客网 时间:2024/05/21 21:42
一. 绪论
Spring为各种远程访问技术提供集成工具类。Spring远程访问通过使用普通POJOs,能更容易的开发远程访问服务。目前,Spring远程访问的主要技术如下:
1. 远程调用RMI(Remote Method Invocation): 通过使用RmiProxyFactoryBean 和 RmiServiceExporter,并且,Spring支持两个传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的暴露远程调用(支持任何Java接口)。
2. Spring的HTTP调用器(Spring’sHttp Invoker): Spring提供了一种特殊的允许通过HTTP进行Java串行化的远程调用策略,支持任意Java接口(就像RMI调用器)。相对应的支持类是 HttpInvokerProxyFactoryBean和 HttpInvokerServiceExporter。
3. Hessian: 通过HessianProxyFactoryBean 和 HessianServiceExporter,可以使用Caucho提供的基于HTTP的轻量级二进制协议来透明地暴露服务。
4. Burlap: Burlap是Caucho的另外一个子项目,可以作为Hessian基于XML的替代方案。Spring提供了诸如BurlapProxyFactoryBean 和 BurlapServiceExporter 的支持类。
5. JAX RPC: Spring通过JAX-RPC为远程Web服务提供支持(J2EE1.4's web service API)。
6. JAX-WS:Spring通过JAX-WS为远程Web服务提供支持(thesuccessor of JAX-RPC, as introduced in Java EE 5 and Java 6)。
7. JMS:远程访问通过类JmsInvokerServiceExporter和JmsInvokerProxyFactoryBean使用JMS的底层协议实现。
本文主要实现基于RMI(远程方法调用)的远程访问技术。
二. 远程访问------RMI
1. RMI远程访问基本流程
1). 服务端定义远程访问接口;
2). 服务端通过RmiServiceExporter暴露服务接口
3). 客户端定义与服务端已暴露的相同接口
4). 客户端通过RmiProxyFactoryBean调用服务接口
2. RMI---服务器
1).服务端定义远程访问接口
<span style="font-family:Times New Roman;"><span style="font-family:Microsoft YaHei;font-size:18px;">package com.lm.core.service;import java.util.List;import com.lm.core.entity.User;public interface UserRmiService {List<User> getUserByAcount(String name,String password);void insert(User user);}</span></span>
2). 接口实现
<span style="font-family:Times New Roman;"><span style="font-family:Microsoft YaHei;font-size:18px;">package com.lm.core.service.impl; import java.util.ArrayList;import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.lm.core.entity.User;import com.lm.core.mapper.UserMapper;import com.lm.core.service.UserRmiService; public class UserRmiServiceImpl implements UserRmiService { @Autowired private UserMapper userMapper; @Override public List<User> getUserByAcount(String name, String password) { System.err.println("获取用户信息:" + name + password); return new ArrayList<User>(); } @Override public void insert(User user) { System.err.println("开始插入用户信息:" + user.toString()); } }</span></span>
3). spring配置暴露接口
<span style="font-family:Times New Roman;"><span style="font-family:Microsoft YaHei;font-size:18px;"><!-- spring rmi--> <bean name="urseRmiService" class="com.lm.core.service.impl.UserRmiServiceImpl"/> <bean name="rmiTest" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service"ref="urseRmiService" /> <property name="serviceName"value="urseRmiServiceTest" /> <property name="serviceInterface"value="com.lm.core.service.UserRmiService"/> <property name="registryPort"value="1021" /> </bean></span></span>
4). 客户端定义相同暴露的接口
<span style="font-family:Times New Roman;"><span style="font-family:Microsoft YaHei;font-size:18px;">packagecom.lm.core.service; importjava.util.List; importcom.lm.core.entity.User; public interfaceUserRmiService { List<User> getUserByAcount(Stringname,String password); void insert(User user);}</span></span>
5). Spring配置远程访问接口信息
<span style="font-family:Times New Roman;"><span style="font-family:Microsoft YaHei;font-size:18px;"> <!-- spring rmi --> <bean id="clientRmiTest" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl"value="rmi://127.0.0.1:1021/urseRmiServiceTest"/> <property name="serviceInterface" value="com.lm.core.service.UserRmiService"/> <property name="lookupStubOnStartup" value="false"/> <property name="refreshStubOnConnectFailure" value="true"></property> </bean></span></span>
lookupStubOnStartup:预查找远程对象 默认为true
refreshStubOnConnectFailure:是否刷新远程调用缓存的stub
注意:如果上述两项不配置,当服务器未开启,客户端无法打包,会有拒绝连接异常。
6). 客户端调用服务器接口
<span style="font-family:Times New Roman;"><span style="font-family:Microsoft YaHei;font-size:18px;">package com.lm.web.controller; import org.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody; import com.lm.core.service.UserRmiService;import com.lm.web.vo.BaseMapVo; @Controller@RequestMapping(value = "/user")public class UserController { @SuppressWarnings("resource") @RequestMapping(value = "/rmiTest") @ResponseBody public BaseMapVo rmiTest(String name, String password) { BaseMapVo vo = new BaseMapVo(); System.out.println("rmi客户端开始调用"); ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:application-context.xml"); UserRmiService rmi = (UserRmiService) ctx.getBean("clientRmiTest"); rmi.getUserByAcount("张三", ":张三的密码"); System.out.println("rmi客户端调用结束"); vo.setRslt("sucess"); return vo; }}</span></span>
7).输出和结果
上述就为本文所有内容,
代码下载http://download.csdn.net/detail/a123demi/9494528
参考文献:http://docs.spring.io/spring/docs/2.5.x/reference/remoting.html
- Spring 实现远程访问详解——rmi
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Spring与RMI集成实现远程访问
- Java之——Spring与RMI集成实现远程访问(插曲)
- Spring 实现远程访问详解——httpinvoker
- Spring 实现远程访问详解——httpclient
- 玩味AndroidStudio的NewProject
- 解决列表 (ListView GrifView RecyclerView )结合CheckBox实现列表选择的的问题
- 我的博客
- 全局最小割模板 poj2914
- 无线传感网实验箱
- Spring 实现远程访问详解——rmi
- http块指令解析以及存储
- 如何通俗的解释计算机是如何实现1+1=2计算的?
- uva10534(最长递增子序列的算法变形 复杂度较低)
- 网易2016研发工程师编程题
- cas系列(三)--HTTP和HTTPS、SSL
- c# 字符串拼接效率
- 台大林轩田《机器学习基石》学习笔记:线性模型三(Multiclass Prediction)
- AndroidUI--SlidingMenu使用例子