Spring RPC 介绍(Burlap)

Spring为各种远程访问技术的集成提供了工具类。Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易。目前,Spring支持四种远程技术:
1.远程方法调用(RMI)。通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,Spring同时支持传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的透明远程调用(支持任何Java接口)。
2.Spring的HTTP调用器。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服务提供支持,以Web Services的形式发布。常见开源的项目有Axis,CXF,Xfire等

6.JMS(待实现)。

上一节介绍了HttpInvoker的使用,Burlap、RIM和Hessian的使用与其类似。下面以Burlap为例测试。

 Burlap是一个轻量级的XML RPC协议,这使得他可以移植到任何可以解析XML的语言中。比起Hessian,它的可读性更强。和其他基于Xml的远程技术(如SOAP和XML-RPC)不同,它的消息结构尽可能简单,不需要额外的外部定义语言(如WSDL或IDL)。Hessian,Burlap是Caucho提供的两种基于Http的轻量级远程服务, Burlap现在已经集成到hessian.jar中,不作为一个单独的项目了。

代码示例如下:

1.添加依赖jar包

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.             <groupId>org.springframework</groupId>  
  3.             <artifactId>spring-context</artifactId>  
  4.             <version>4.0.9.RELEASE</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.springframework</groupId>  
  8.             <artifactId>spring-expression</artifactId>  
  9.             <version>4.0.9.RELEASE</version>  
  10.         </dependency>  
  11.         <dependency>  
  12.             <groupId>org.springframework</groupId>  
  13.             <artifactId>spring-web</artifactId>  
  14.             <version>4.0.9.RELEASE</version>  
  15.         </dependency>  
  16.         <dependency>  
  17.             <groupId>org.springframework</groupId>  
  18.             <artifactId>spring-webmvc</artifactId>  
  19.             <version>4.0.9.RELEASE</version>  
  20.         </dependency>  
  21.         <dependency>  
  22.             <groupId>org.springframework</groupId>  
  23.             <artifactId>spring-tx</artifactId>  
  24.             <version>4.0.9.RELEASE</version>  
  25.         </dependency>  
  26.         <dependency>  
  27.             <groupId>com.caucho</groupId>  
  28.             <artifactId>hessian</artifactId>  
  29.             <version>4.0.38</version>  
  30.         </dependency>  

2.接口、实现类

对象User.Java

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.slimsmart.burlap.spring.demo;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class User implements Serializable{  
  6.     private static final long serialVersionUID = 1L;  
  7.       
  8.     private String name;  
  9.     private int age;  
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13.     public void setName(String name) {  
  14.         this.name = name;  
  15.     }  
  16.     public int getAge() {  
  17.         return age;  
  18.     }  
  19.     public void setAge(int age) {  
  20.         this.age = age;  
  21.     }  
  22.     @Override  
  23.     public String toString() {  
  24.         return "name="+name+",age="+age;  
  25.     }  
  26. }  

接口:UserService.java

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.slimsmart.burlap.spring.demo;  
  2.   
  3. public interface UserService {  
  4.     User getUser(String name);  
  5. }  

实现类:UserServiceImpl.java

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.slimsmart.burlap.spring.demo;  
  2.   
  3. public class UserServiceImpl implements UserService{  
  4.   
  5.     @Override  
  6.     public User getUser(String name) {  
  7.         User user = new User();  
  8.         user.setAge(30);  
  9.         user.setName(name);  
  10.         return user;  
  11.     }  
  12. }  

3.服务端配置

applicationContext.xml

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  7.                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"  
  9.     default-lazy-init="true">  
  10.       
  11.     <bean  id="userServiceImpl" class="cn.slimsmart.burlap.spring.demo.UserServiceImpl"/>  
  12. </beans>  

applicationContext-burlap-exporter.xml

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  7.                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"  
  9.     default-lazy-init="true">  
  10.       
  11.      <bean name="/userService" class="org.springframework.remoting.caucho.BurlapServiceExporter">  
  12.         <property name="service" ref="userServiceImpl" />  
  13.         <property name="serviceInterface" value="cn.slimsmart.burlap.spring.demo.UserService"/>  
  14.     </bean>  
  15.       
  16. </beans>  

web.xml

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.     <display-name>ThriftTest</display-name>  
  7.   
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>  
  11.             classpath:applicationContext.xml  
  12.         </param-value>  
  13.     </context-param>  
  14.       
  15.     <servlet>  
  16.         <servlet-name>dispatcherServlet</servlet-name>  
  17.         <servlet-class>  
  18.             org.springframework.web.servlet.DispatcherServlet  
  19.         </servlet-class>  
  20.         <init-param>  
  21.             <param-name>contextConfigLocation</param-name>  
  22.             <param-value>classpath*:/applicationContext-burlap-exporter.xml</param-value>  
  23.         </init-param>  
  24.         <load-on-startup>1</load-on-startup>  
  25.     </servlet>  
  26.       
  27.     <servlet-mapping>  
  28.         <servlet-name>dispatcherServlet</servlet-name>  
  29.         <url-pattern>/remote/*</url-pattern>  
  30.     </servlet-mapping>  
  31.       
  32.     <listener>  
  33.         <listener-class>  
  34.             org.springframework.web.context.ContextLoaderListener  
  35.         </listener-class>  
  36.     </listener>  
  37.    
  38.     <session-config>  
  39.         <session-timeout>30</session-timeout>  
  40.     </session-config>  
  41.       
  42.     <welcome-file-list>  
  43.         <welcome-file>index.html</welcome-file>  
  44.     </welcome-file-list>  
  45.       
  46. </web-app>  

4.客户端配置

applicationContext-burlap.xml

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  7.                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"  
  9.     default-lazy-init="true">  
  10.   
  11.     <bean id="userService"  
  12.         class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">  
  13.         <property name="serviceUrl"  
  14.             value="http://127.0.0.1:8080/burlap-spring-demo/remote/userService" />  
  15.         <property name="serviceInterface" value="cn.slimsmart.burlap.spring.demo.UserService" />  
  16.     </bean>  
  17. </beans>  

5.测试运行类

UserServiceClientTest.java

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.slimsmart.burlap.spring.demo;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class UserServiceClientTest {  
  7.     @SuppressWarnings("resource")  
  8.     public static void main(String[] args) {  
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-burlap.xml");  
  10.         UserService client = context.getBean(UserService.class);  
  11.         System.out.println(client.getUser("lucy"));  
  12.     }  
  13. }  
通过http请求抓包可以看见协议定义

请求:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <burlap:call>  
  2.     <method>getUser</method>  
  3.     <string>lucy</string>  
  4. </burlap:call>  

相应:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <burlap:reply>  
  2.     <map>  
  3.         <type>cn.slimsmart.burlap.spring.demo.User</type>  
  4.         <string>name</string>  
  5.         <string>lucy</string>  
  6.         <string>age</string>  
  7.         <int>30</int>  
  8.     </map>  
  9. </burlap:reply>  

代码:http://download.csdn.net/detail/tianwei7518/8468333

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值