一、简介
Spring开发团队意识到RMI服务和基于HTTP的服务(如,Hessian)之间的空白。一方面,RMI使用JAVA标准的对象序列化机制,很难穿透防火墙。另一方面,Hessian/Burlap能很好的穿透防火墙,但是使用私有的对象序列化机制。
由此,Spring的HTTP invoker应运而生。HTTP invoker是一个新的远程调用模型,作为Spring框架的一部分,来执行基于HTTP的远程调用(穿透防火墙),并使用Java的序列化机制。
二、发布HTTP服务
1、编写接口
package com.cnblogs.javalouvre.service; public interface GreetService { String sayHello(String name); }
2、编写实现类
package com.cnblogs.javalouvre.service; public class GreetServiceImpl implements GreetService { @Override
public String sayHello(String name) {
return "Hello " + name;
} }
3、配置Spring文件(remoting-servlet.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="greetService" class="com.cnblogs.javalouvre.service.GreetServiceImpl" /> <bean name="/GreetService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="greetService" />
<property name="serviceInterface" value="com.cnblogs.javalouvre.service.GreetService" />
</bean> </beans>
4、配置WEB文件(web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"> <servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app>
三、客户端调用
1、配置Spring文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="greetService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://10.108.1.138:8080/remoting/GreetService"/>
<property name="serviceInterface" value="com.cnblogs.javalouvre.service.GreetService"/>
</bean> </beans>
2、编写客户端测试
package com.cnblogs.javalouvre.client; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cnblogs.javalouvre.service.GreetService; public class Client { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
GreetService service = context.getBean("greetService", GreetService.class);
System.out.println(service.sayHello("Jobs"));
} }