Spring HTTP invoker 入门

一、简介

    Spring开发团队意识到RMI服务和基于HTTP的服务(如,Hessian)之间的空白。一方面,RMI使用JAVA标准的对象序列化机制,很难穿透防火墙。另一方面,Hessian/Burlap能很好的穿透防火墙,但是使用私有的对象序列化机制。
    由此,Spring的HTTP invoker应运而生。HTTP invoker是一个新的远程调用模型,作为Spring框架的一部分,来执行基于HTTP的远程调用(穿透防火墙),并使用Java的序列化机制。

二、发布HTTP服务

    1、编写接口

1 package com.cnblogs.javalouvre.service;
2 
3 public interface GreetService {
4 
5     String sayHello(String name);
6 
7 }

    2、编写实现类

 1 package com.cnblogs.javalouvre.service;
 2 
 3 public class GreetServiceImpl implements GreetService {
 4 
 5     @Override
 6     public String sayHello(String name) {
 7         return "Hello " + name;
 8     }
 9 
10 }

    3、配置Spring文件(remoting-servlet.xml)

 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"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 6 
 7   <bean id="greetService" class="com.cnblogs.javalouvre.service.GreetServiceImpl" />
 8 
 9   <bean name="/GreetService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
10     <property name="service" ref="greetService" />
11     <property name="serviceInterface" value="com.cnblogs.javalouvre.service.GreetService" />
12   </bean>
13 
14 </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文件

 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"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 6 
 7   <bean id="greetService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
 8     <property name="serviceUrl" value="http://10.108.1.138:8080/remoting/GreetService"/>
 9     <property name="serviceInterface" value="com.cnblogs.javalouvre.service.GreetService"/>
10   </bean>
11 
12 </beans>

    2、编写客户端测试

 

 1 package com.cnblogs.javalouvre.client;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.cnblogs.javalouvre.service.GreetService;
 7 
 8 public class Client {
 9 
10     public static void main(String[] args) {
11         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
12         GreetService service = context.getBean("greetService", GreetService.class);
13         System.out.println(service.sayHello("Jobs"));
14     }
15 
16 }
posted @ 2014-05-14 17:05  Bruce.Chang.Lee  阅读(1596)  评论(0)    收藏  举报