基于Spring的远程调用的实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Spring的远程调用的实现相关的知识,希望对你有一定的参考价值。

    在Spring remoting中包含三种实现方式:http-invoker,hessian,burlap。具体的配置如下:

一、服务器端配置(applicationContext.xml):

<pre name="code"><?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:p="http://www.springframework.org/schema/p"

        xmlns:mvc="http://www.springframework.org/schema/mvc"

        xmlns:context="http://www.springframework.org/schema/context"

       

        xsi:schemaLocation="

               http://www.springframework.org/schema/beans

               http://www.springframework.org/schema/beans/spring-beans.xsd

               http://www.springframework.org/schema/mvc

               http://www.springframework.org/schema/mvc/spring-mvc.xsd

               http://www.springframework.org/schema/context

               http://www.springframework.org/schema/context/spring-context.xsd">

              

        <context:component-scan base-package="com.plateno.service" />

       

        <bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

        <!-- 类似rmi方式,采用java序列化,数据量较大 -->

        <bean name="/httpInvokerService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">

               <property name="service" ref="remoteService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

        <!-- 采用hessian序列化,数据量最小 -->

        <bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">

               <property name="service" ref="remoteService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

        <!-- xml传输数据,数据量比httpinvoker方式小 -->

        <bean name="/burlapService" class="org.springframework.remoting.caucho.BurlapServiceExporter">

               <property name="service" ref="remoteService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean> 

       

        <mvc:interceptors>

               <mvc:interceptor>

                       <mvc:mapping path="/**"/>

                       <bean class="com.plateno.interceptor.CommonInterceptor"/>

               </mvc:interceptor>

        </mvc:interceptors>

       

</beans>

 

二、服务器端配置(web.xml)

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns="http://java.sun.com/xml/ns/javaee"

        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

        id="WebApp_ID" version="2.5">

       

        <context-param>

               <param-name>contextConfigLocation</param-name>

               <param-value>classpath:applicationContext.xml</param-value>

        </context-param>

        <listener>

               <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

        </listener>

       

        <servlet>

               <servlet-name>springmvc</servlet-name>

                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

               <init-param>

                       <param-name>contextConfigLocation</param-name>

                       <param-value>classpath:spring-mvc.xml</param-value>

               </init-param>

        </servlet>

       

        <servlet-mapping>

               <servlet-name>springmvc</servlet-name>

               <url-pattern>/</url-pattern>

        </servlet-mapping>

       

</web-app>

三、写一个service,再写一个serviceImpl就ok了。

 

四、客户端配置:

<pre name="code"><bean id="httpInvokerProxyFactoryBean" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

               <property name="serviceUrl" value="http://localhost:9999/server/httpInvokerService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

       

        <bean id="hessianProxyFactoryBean" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

               <property name="serviceUrl" value="http://localhost:9999/server/hessianService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

       

        <bean id="burlapProxyFactoryBean" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">

               <property name="serviceUrl" value="http://localhost:9999/server/burlapService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

 

五、测试类:

@Autowired

        private RemoteService remoteService;

        @RequestMapping(value="/test")

        public ModelAndView test(@RequestParam(value="name", required=false) String name) {

               UserDTO dto = remoteService.getUser("hello");

               System.out.println(dto);

               Map<String, Object> model = new HashMap<String, Object>();

               return new ModelAndView("test/hello", model);

        }

 

    

本文出自 “12547096” 博客,请务必保留此出处http://12557096.blog.51cto.com/12547096/1902200

以上是关于基于Spring的远程调用的实现的主要内容,如果未能解决你的问题,请参考以下文章

Hessian怎样实现远程调用

Spring HttpIvoker实现Java的远程调用

使用Spring框架实现远程服务暴露与调用

spring源码学习spring的远程调用实现源码分析

基于spring http服务远程调用

基于spring http服务远程调用