Spring远程访问Web服务

一、介绍
    目前,Spring提供对下面四种远程访问技术的支持:

  1. 远程方法调用(RMI):通过使用RmiProxyFactoryBean和RmiServiceExporter,Spring支持传统的RMI(使用java.rmi.Remote interfaces 和 java.rmi.RemoteException)和通过RMI调用器(可以使用任何Java接口)的透明远程调用。
  2. Spring的HTTP调用器:Spring提供一种特殊的远程调用策略支持任何Java接口(象RMI调用器一样),它允许Java序列化能够通过HTTP传送。对应的支持类是HttpInvokerProxyFactoryBean和HttpInvokerServiceExporter。      和Burlap和Hessian使用自身序列化机制的轻量级协议相反,Spring HTTP调用器使用标准Java序列化机制来通过HTTP输出业务。如果你的参数或返回值是复杂类型,并且不能通过Hessian和Burlap的序列化机制序列化,HTTP调用器就很有优势。Spring可以使用J2SE提供的标准功能或Commons的HttpClient来实现HTTP调用。如果你需要更先进,更好用的功能,就使用后者。
  3. Hessian:通过使用HessianProxyFactoryBean和HessianServiceExporter,你可以使用Caucho提供的轻量级基于HTTP的二进制协议透明地提供你的业务。Hessian提供了一个基于HTTP的二进制远程协议。它由Caucho创建。
  4. Burlap:Burlap是基于XML的,它可以完全代替Hessian。Spring提供的支持类有BurlapProxyFactoryBean和BurlapServiceExporter。 Burlap是Hessian的基于XML实现。

 
二、如何选择?
   当使用RMI时,通过HTTP协议访问对象是不可能的,除非你用HTTP包裹RMI流。RMI是一种很重的协议,因为他支持完全的对象序列化,这样的序列化在要求复杂数据结构在远程传输时是非常重要的。然而,RMI-JRMP只能绑定到Java客户端:它是一种Java-to-Java的远程访问的方案。

    如果你需要基于HTTP的远程访问而且还要求使用Java序列化,Spring的HTTP调用器是一个很好的选择。它和RMI调用器使用相同的基础设施,仅仅使用HTTP作为传输方式。注意HTTP调用器不仅只能用在Java-to-Java的远程访问,而且在客户端和服务器端都必须使用Spring。(Spring为非RMI接口提供的RMI调用器也要求客户端和服务器端都使用Spring)

    当在异构环境中,Hessian和Burlap就非常有用了。因为它们可以使用在非Java的客户端。然而,对非Java支持仍然是有限制的。已知的问题包括含有延迟初始化的collection对象的Hibernate对象的序列化。如果你有一个这样的数据结构,考虑使用RMI或HTTP调用器,而不是Hessian。

    最后但也很重要的一点,EJB优于RMI,因为它支持标准的基于角色的认证和授权,以及远程事务传递。用RMI调用器或HTTP调用器来支持安全上下文的传递是可能的,虽然这不是由核心Spring提供:而是由第三方或在定制的解决方案中插入拦截器来解决的。

三、例子
   定义好用于测试的接口和实现。

Java代码 复制代码
  1. package com.logcd.server.service;   
  2.   
  3. public interface IHelloService {      
  4.     public String doHello(String name);      
  5. }  
package com.logcd.server.service;

public interface IHelloService {   
    public String doHello(String name);   
}

 

Java代码 复制代码
  1. package com.logcd.server.service.impl;   
  2.   
  3. import com.logcd.server.service.IHelloService;   
  4.   
  5. public class HelloService implements IHelloService{   
  6.     public String doHello(String name) {   
  7.         return "Hello , " + name;      
  8.     }   
  9. }  
package com.logcd.server.service.impl;

import com.logcd.server.service.IHelloService;

public class HelloService implements IHelloService{
	public String doHello(String name) {
		return "Hello , " + name;   
	}
}


(一)使用Hessian
(1)server端:web.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"  
  3.  xmlns="http://java.sun.com/xml/ns/j2ee"  
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.        
  8.     <servlet>  
  9.         <servlet-name>Hessian</servlet-name>  
  10.         <servlet-class>  
  11.             org.springframework.web.servlet.DispatcherServlet   
  12.         </servlet-class>  
  13.         <init-param>  
  14.             <param-name>contextConfigLocation</param-name>  
  15.             <param-value>  
  16.                 classpath:Hessian-servlet.xml   
  17.             </param-value>               
  18.         </init-param>    
  19.         <load-on-startup>1</load-on-startup>  
  20.     </servlet>     
  21.                       
  22.     <servlet-mapping>  
  23.         <servlet-name>Hessian</servlet-name>  
  24.         <url-pattern>/hessian/*</url-pattern>  
  25.     </servlet-mapping>  
  26.   
  27. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
	<servlet>
		<servlet-name>Hessian</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
	        <param-name>contextConfigLocation</param-name>
	        <param-value>
	            classpath:Hessian-servlet.xml
	        </param-value>			
		</init-param> 
		<load-on-startup>1</load-on-startup>
	</servlet>  
		           
	<servlet-mapping>
		<servlet-name>Hessian</servlet-name>
		<url-pattern>/hessian/*</url-pattern>
	</servlet-mapping>

</web-app>


(2)server端:Hessian-servlet.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"       
  3.   "http://www.springframework.org/dtd/spring-beans.dtd">    
  4.   
  5. <beans>  
  6.        
  7.     <bean id="helloService" class="com.logcd.server.service.impl.HelloService"/>  
  8.        
  9.     <bean name="/helloService" class="org.springframework.remoting.caucho.HessianServiceExporter">  
  10.         <property name="service" ref="helloService"/>  
  11.         <property name="serviceInterface">  
  12.             <value>  
  13.                 com.logcd.server.service.IHelloService   
  14.             </value>  
  15.         </property>  
  16.     </bean>  
  17.        
  18. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"    
  "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans>
	
	<bean id="helloService" class="com.logcd.server.service.impl.HelloService"/>
	
	<bean name="/helloService" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="helloService"/>
		<property name="serviceInterface">
			<value>
				com.logcd.server.service.IHelloService
			</value>
		</property>
	</bean>
	
</beans>


(3)client端测试:Hessian-client.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"   
  3.    "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.   
  6. <bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
  7.     <property name="serviceUrl">  
  8.         <value>http://localhost:8080/hessian/helloService</value>  
  9.     </property>  
  10.     <property name="serviceInterface">  
  11.         <value>com.logcd.server.service.IHelloService</value>  
  12.     </property>  
  13. </bean>  
  14.   
  15. </beans>  
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
	<property name="serviceUrl">
		<value>http://localhost:8080/hessian/helloService</value>
	</property>
	<property name="serviceInterface">
		<value>com.logcd.server.service.IHelloService</value>
	</property>
</bean>

</beans>


(4)测试程序:

Java代码 复制代码
  1. package com.logcd.client.test;   
  2.   
  3. import java.net.MalformedURLException;   
  4.   
  5. import org.springframework.context.ApplicationContext;   
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  7.   
  8. import com.caucho.hessian.client.HessianProxyFactory;   
  9. import com.logcd.server.service.IHelloService;   
  10.   
  11. public class HessianTest {   
  12.   
  13.     public static void main(String args[]){   
  14.         clientSpringTest();   
  15.     }   
  16.        
  17.     public static void clientSpringTest(){   
  18.   
  19.         ApplicationContext context= new ClassPathXmlApplicationContext("Hessian-client.xml");       
  20.         IHelloService helloService = (IHelloService)context.getBean("helloService");      
  21.         System.out.println(helloService.doHello("logcd"));     
  22.           
  23.     }   
  24.        
  25.     public static void clientJavaTest(){   
  26.   
  27.         String url = "http://localhost:8080/hessian/helloService";    
  28.         HessianProxyFactory factory = new HessianProxyFactory();    
  29.         try {   
  30.             IHelloService helloService =(IHelloService)factory.create(IHelloService.class, url);   
  31.   
  32.             System.out.println(helloService.doHello("logcd"));      
  33.   
  34.         } catch (MalformedURLException e) {   
  35.             e.printStackTrace();   
  36.         }   
  37.            
  38.     }   
  39.        
  40. }  
package com.logcd.client.test;

import java.net.MalformedURLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caucho.hessian.client.HessianProxyFactory;
import com.logcd.server.service.IHelloService;

public class HessianTest {

	public static void main(String args[]){
		clientSpringTest();
	}
	
	public static void clientSpringTest(){

		ApplicationContext context= new ClassPathXmlApplicationContext("Hessian-client.xml");    
		IHelloService helloService = (IHelloService)context.getBean("helloService");   
		System.out.println(helloService.doHello("logcd"));  
       
	}
	
	public static void clientJavaTest(){

		String url = "http://localhost:8080/hessian/helloService"; 
		HessianProxyFactory factory = new HessianProxyFactory(); 
		try {
			IHelloService helloService =(IHelloService)factory.create(IHelloService.class, url);

			System.out.println(helloService.doHello("logcd"));   

		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
	}
	
}


(二)使用HTTP调用器
(1)server端:web.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"  
  3.  xmlns="http://java.sun.com/xml/ns/j2ee"  
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.        
  8.     <servlet>  
  9.         <servlet-name>httpInvoker</servlet-name>  
  10.         <servlet-class>  
  11.             org.springframework.web.servlet.DispatcherServlet   
  12.         </servlet-class>  
  13.         <init-param>  
  14.             <param-name>contextConfigLocation</param-name>  
  15.             <param-value>  
  16.                 classpath:httpinvoker-servlet.xml   
  17.             </param-value>               
  18.         </init-param>    
  19.         <load-on-startup>1</load-on-startup>  
  20.     </servlet>     
  21.                       
  22.     <servlet-mapping>  
  23.         <servlet-name>httpInvoker</servlet-name>  
  24.         <url-pattern>/httpInvoker/*</url-pattern>  
  25.     </servlet-mapping>  
  26.   
  27. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
	<servlet>
		<servlet-name>httpInvoker</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
	        <param-name>contextConfigLocation</param-name>
	        <param-value>
	            classpath:httpinvoker-servlet.xml
	        </param-value>			
		</init-param> 
		<load-on-startup>1</load-on-startup>
	</servlet>  
		           
	<servlet-mapping>
		<servlet-name>httpInvoker</servlet-name>
		<url-pattern>/httpInvoker/*</url-pattern>
	</servlet-mapping>

</web-app>


(2)server端:httpinvoker-servlet.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
  3.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.   
  6.     <bean id="helloService" class="com.logcd.server.service.impl.HelloService"/>  
  7.        
  8.     <bean name="/helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" lazy-init="false">  
  9.         <property name="service">  
  10.             <ref bean="helloService"/>  
  11.         </property>  
  12.         <property name="serviceInterface">  
  13.             <value>com.logcd.server.service.IHelloService</value>  
  14.         </property>  
  15.     </bean>  
  16.        
  17. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
	"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="helloService" class="com.logcd.server.service.impl.HelloService"/>
    
    <bean name="/helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" lazy-init="false">
        <property name="service">
            <ref bean="helloService"/>
        </property>
        <property name="serviceInterface">
            <value>com.logcd.server.service.IHelloService</value>
        </property>
    </bean>
    
</beans>


(3)client端:httpinvoker-client.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"   
  3.    "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.   
  6. <bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  7.   
  8.     <property name="serviceUrl">  
  9.         <value>http://localhost:8080/httpInvoker/helloService</value>  
  10.     </property>  
  11.   
  12.     <property name="serviceInterface">  
  13.         <value>com.logcd.server.service.IHelloService</value>  
  14.     </property>  
  15.        
  16.     <!--   
  17.         默认情况下,客户端的HttpInvokerProxy使用J2SE的HTTP Client来建立连接   
  18.         org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor   
  19.     -->  
  20.     <property name="httpInvokerRequestExecutor">  
  21.         <bean  
  22.         class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor" />  
  23.     </property>  
  24.        
  25. </bean>  
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

	<property name="serviceUrl">
		<value>http://localhost:8080/httpInvoker/helloService</value>
	</property>

	<property name="serviceInterface">
		<value>com.logcd.server.service.IHelloService</value>
	</property>
	
	<!--
		默认情况下,客户端的HttpInvokerProxy使用J2SE的HTTP Client来建立连接
		org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor
	-->
	<property name="httpInvokerRequestExecutor">
		<bean
	    class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor" />
	</property>
	
</bean>


(4)测试类

Java代码 复制代码
  1. package com.logcd.client.test;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  5. import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;   
  6.   
  7. import com.logcd.server.service.IHelloService;   
  8.   
  9. public class HttpInvokerTest {   
  10.   
  11.     public static void main(String args[]){   
  12.         clientJavaTest();   
  13.     }   
  14.        
  15.     public static void clientSpringTest(){   
  16.   
  17.         ApplicationContext context= new ClassPathXmlApplicationContext("httpinvoker-client.xml");       
  18.         IHelloService helloService = (IHelloService)context.getBean("helloService");      
  19.         System.out.println(helloService.doHello("logcd"));     
  20.           
  21.     }   
  22.        
  23.     public static void clientJavaTest(){   
  24.   
  25.         String url = "http://localhost:8080/httpInvoker/helloService";    
  26.         HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean ();    
  27.   
  28.         factory.setServiceInterface(IHelloService.class);   
  29.         factory.setServiceUrl(url);   
  30.         factory.afterPropertiesSet();   
  31.            
  32.         IHelloService helloService = (IHelloService)factory.getObject();   
  33.         System.out.println(helloService.doHello("logcd"));    
  34.            
  35.     }   
  36.        
  37. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值