`
yanlp
  • 浏览: 75550 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring远程服务

阅读更多

       java中可用的远程技术包括RMI、Caucho的Hessian和Burlap|、Spring的HTTP invoker、SOAP和JAX-RPC的web service。

      

spring简化RMI.

客户端

只需在spring配置文件中声明下面的<bean>

     <bean id="testService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
     	<property name="serviceUrl" value="rmi://127.0.0.1:1099/testService"></property>
     	<property name="serviceInterface" value="com.pm.service.TestService"></property>
     </bean> 

然后在需要的地方,就可以使用这个远程对象了,如:

     <bean id="employeeService" class="com.pm.service.imp.EmployeeServiceImp">
     	<property name="employeeDao" ref="employeeDao"></property>
     	<property name="testService" ref="testService"></property>
     </bean>

 

  服务端,输出一个RMI:

1.创建一个普通服务接口,

package com.test;

public interface TestService {
	public void print(String name);
}

2.创建一个接口实现类:

package com.test;

public class TestServiceImp  implements TestService{
	
	public void print(String name) {
		System.out.println(name+"你好啊!哈哈");
		
	}

}

 3.在spring配置文件中配置 TestServiceImp  bean:

<bean id="testService" class="com.test.TestServiceImp"></bean>

  4.使用RmiServiceExporter将testService公开为RMI服务:

 <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
 	<property name="service" ref="testService"></property>
 	<property name="serviceName" value="testService"></property>
 	<property name="serviceInterface" value="com.test.TestService"></property>
 	 <property name="registryPort" value="1099"></property>
 </bean>

 

其中registryPort默认就是1099。

 

至此,一个RMI客户端调用与服务端输出大功告成。但RMI可以使用任意端口,这是防火墙不允许的,还有,RMI是基于java的,这就意味着客户端和服务端都必须用java编写,为了解决这些限制,于是乎,引出了下面这些技术。

 

spring简化Hessian和Burlap

 Hessian和Burlap是基于HTTP的轻量级远程服务,两者其实差不多,Hessian是基于二进制消息来建立客户端与服务端 之间的交流,而Burlap是基于XML的。

首先无论客户端还是服务器端,都得导入hessian.jar

还是以上面的例子,如果是客户端,我们只要修改一下spring配置文件的一个bean即可

Hessian:

     <bean id="hessianTestService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
     	<property name="serviceUrl" value="http://127.0.0.1:8080/SpringRmi/test.service"></property>
     	<property name="serviceInterface" value="com.pm.service.TestService"></property>
     </bean>

 Burlap:

     <bean id="hessianTestService" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">
     	<property name="serviceUrl" value="http://127.0.0.1:8080/SpringRmi/test.service"></property>
     	<property name="serviceInterface" value="com.pm.service.TestService"></property>
     </bean>

 

而如果要配置服务端,则比RMI要稍微复杂点点:

1.在spring配置文件中:

<!--配置一个SimpleUrlHandlerMapping:-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="mappings">
		<props>
			<prop key="/test.service">hessianServiceExporter</prop>		
		</props>
	</property>
</bean>

<bean id="hessianServiceExporter" class="org.springframework.remoting.caucho.HessianServiceExporter">
	<property name="service" ref="testService"></property>
	<property name="serviceInterface" value="com.test.TestService"></property>
</bean>
<!-- 或者 -->
<bean id="burlapServiceExporter" class="org.springframework.remoting.caucho.BurlapServiceExporter">
	<property name="service" ref="testService"></property>
	<property name="serviceInterface" value="com.test.TestService"></property>
</bean>

 

2.在web.xml中:

	<servlet>
		<servlet-name>test</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath:applicationContext.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
	   <servlet-name>test</servlet-name>
	   <url-pattern>*.service</url-pattern>
	</servlet-mapping>

 在测试过程中遇到,在启动客户端容器时会报:

 org.springframework.web.HttpRequestMethodNotSupportedException: HessianServiceExporter only supports POST requests

找了好久,不知道是什么原因造成的,但似乎不影响程序运行。

 

 

HTTP invoker是基于Spirng的,用法跟Hessian、Burlap差不多,只需把org.springframework.remoting.caucho.HessianProxyFactoryBean与org.springframework.remoting.caucho.HessianServiceExporter替换成org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean与org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter

分享到:
评论

相关推荐

    Spring远程调用使用http方式

    Spring远程调用使用http方式,将server和client直接部署后,进入http://localhost/HttpClientSpringRMIClient/即可

    Spring攻略PDF版

    因为上传大小的限制,分为两部分上传,这是第二部分,... 第16章 Spring远程服务和Web服务   第17章 Spring对EJB和JMS的支持   第18章 Spring对JMX、电子邮件和调度的支持   第19章 Spring中的脚本编程 

    spring远程调用简单实例

    spirng远程调用可运行简单实例。包含所需所有jar spring-*-3.*.RELEASE.jar aopalliance.jar等。

    Spring攻略中文版PDF

    因为上传大小的限制,分为两部分上传,这是第一部分,... 第16章 Spring远程服务和Web服务   第17章 Spring对EJB和JMS的支持   第18章 Spring对JMX、电子邮件和调度的支持   第19章 Spring中的脚本编程 

    Spring攻略英文版(附带源码)

    Spring专家力作 理论与实践完美结合 问题描述→解决方案... 第16章 Spring远程服务和Web服务   第17章 Spring对EJB和JMS的支持   第18章 Spring对JMX、电子邮件和调度的支持   第19章 Spring中的脚本编程 

    spring远程调用

    使用 spring 的 httpinvoker 进行远程调用

    Spring 实现远程访问详解——rmi

    Spring远程访问通过使用普通POJOs,能更容易的开发远程访问服务。目前,Spring远程访问的主要技术如下: 1. 远程调用RMI(Remote Method Invocation): 通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,并且,...

    使用spring远程调用服务端接口实现WebService功能

    适合有spring框架的javaEE平台,出自spring的HttpInvokerServiceExporter导出器,依赖Spring.jar

    Spring 实现远程访问详解——httpinvoker

    上文我们利用Spring rmi实现了Spring的远程访问(Spring 实现远程访问详解——rmi),本文主要讲解利用HttpInvoke实现远程访问。 Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和...

    Spring 远程调用 -- C# 访问java WEB 服务

    Spring 远程调用 -- C# 访问java WEB 服务,之前写的Demo,希望对大家有所帮帮助!

    spring cloud feign实现远程调用服务传输文件的方法

    主要介绍了spring cloud feign实现远程调用服务传输文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring配置hessian远程服务

    使用eclipse maven工程搭建hessian远程服务demo 分服务端的整合和客户端 建议阅读相关博客http://blog.csdn.net/heisemuyangquan/article/details/79460528

    spring-boot+webSocket实现向日葵远程控制

    本项目为spring-boot+webSocket实现的向日葵远程控制项目 向日葵是一款很好用的远程操作软件。 一直很好奇这种软件的基本原理是如何的? 今天带大家通过一个简单的项目来探究一下,并实现一个简单的远程操控软件 ...

    java 实现上传文件到远程服务器

    java实现上传文件到远程服务器(spring mvc)

    Spring 实现远程访问详解——httpclient

    说得简单就是直接通过spring requestmapping即请求映射url访问远程服务。 1. 远程访问流程 1) 服务器在控制器定义远程访问请求映射路径 2) 客户端通过apache httpclient的 httppost方式访问远程服务 2. Httpclient...

    struts2.3.4+spring3.2.0+hibernate4+hibernate_generic_dao 全注释+远程调用

    struts2.3.4+spring3.2.0+hibernate4+hibernate_generic_dao struts hibernate spring最大化使用注释 基于spring的远程调用

    spring RMI 远程接口调用

    spring RMI 远程接口调用 包含服务端客户端程序,可完整运行

    Spring 实现远程访问详解——webservice

    Spring Web Services 是基于 Spring 框架的 Web 服务框架,主要侧重于基于文档驱动的Web服务,提供 SOAP 服务开发,允许通过多种方式创建 Web 服务。本章利用Apache CXF构建和开发webservice. 1. webservice远程...

    基于Spring的远程访问与Web Service

    基于Spring的远程访问与Web Service

    Spring Cloud OpenFeign 远程调用

    Spring Cloud OpenFeign 可以做到 让我们在使用 HTTP 请求远程服务时,就像调用本地方法一样。OpenFeign 和 Dubbo一样是一个RPC远程调用框架,目的是使的我们调用接口和调用本地方法一样简单,开发者无需关心和远程...

Global site tag (gtag.js) - Google Analytics