httpInvoker提供远程服务调用总结(一)

1 使用httpInvoker提供远程服务

使用HttpInvoker,不需要额外的类库。和Hessian的轻量级传输协议不同的是,SpringHttpInvoker使用Java序列化来序列化参数和返回值,然后基于HTTP协议传输经序列化后的对象。当参数或返回值是复杂类型,并且不能通过Hessian的序列化机制序列化时,HttpInvoker就很有优势。

2 输出业务对象

通过HttpInvoker提供远程服务与Hessian非常相似:Spring为Hessian提供HessianServiceExporter,Spring为HttpInvoker提供HttpInvokerServiceExporter。该类也可将普通baen实例输出成远程服务。配置方法与HessianServiceExporter也非常相似。

下面是使用HttpInvokerServiceExporter提供远程服务的示例。在该示例里,远程服务bean的方法会返回一个Person对象,注意:Person对象必须实现Serializable或Externalizable接口,因为采用了Java序列化机制来传输参数和返回值。下面是远程服务提供类:

public class HelloImpl implements Hello

{

        //远程服务方法,该方法返回一个可序列化的对象

    publicPerson hello(String name)

    {

          Person p = new Person();

                  p.setName(name);

                  p.setAge(13);

                  return p;

    }

}

下面是Person的实现类,该类实现了Serializable接口:

public class Person implements Serializable

{

        //Person类的两个属性

        private String name;

        private int age;

        //name属性的getter方法

        public String getName()

        {

                  return name;

        }

        //name属性的setter方法

        public void setName(String name)

        {

                  this.name = name;

        }

        //age属性的getter方法

        public int getAge()

        {

                  return age;

        }

        //age属性的setter方法

        public void setAge(int age)

        {

                  this.age = age;

        }

}

然后修改web.xml配置文件,该配置文件里,需要让DispatcherServlet拦截匹配模式的请求,该请求由DispatcherServlet转发给context中对应的bean处理。修改后的web.xml配置文件如下:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Web应用配置文件的文件头,包含dtd等信息-->

<!DOCTYPE web-app

    PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

<!--  Web应用配置文件的根元素-->

<web-app>

        <!--  配置DispatcherServlet-->

   <servlet>

         <servlet-name>remoting</servlet-name>

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

                  <!--  配置该Servlet随应用启动时候启动-->

         <load-on-startup>1</load-on-startup>

   </servlet>

        <!--  配置DispatcherServlet映射的url-->

   <servlet-mapping>

         <servlet-name>remoting</servlet-name>

         <url-pattern>/remoting/*</url-pattern>

   </servlet-mapping>

</web-app>

上面配置文件中,没有使用ContextLoaderListener来初始化ApplicationContext,也没有指定ApplicationContext配置文件的位置。这些都是通过DispatcherServlet默认加载的,DispatcherServlet在创建时,会加载ApplicationContext,默认查找的配置文件为:[servlet-name]-servlet.xml文件。对该配置默认加载remoting-servlet.xml文件。因此,在WEB-INF下提供remoting-servlet.xml文件即可。remoting-servlet.xml文件如下:

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

<!-- Spring配置文件的文件头,包含dtd等信息-->

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<!--  Spring配置文件的根元素-->

<beans>

<!--  配置业务服务bean-->

    <beanid="helloService" class="lee.HelloImpl"/>

   <!--  将业务服务bean暴露成远程服务-->

    <beanname="/helloService"

                  class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">

                  <!--  指定需要暴露的业务服务bean-->

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

                  <!--  指定暴露的远程服务实现的接口-->

          <propertyname="serviceInterface" value="lee.Hello"/>

   </bean>

</beans>

DispatcherServlet转发请求的默认策略是:将请求转发到context中与请求同名的bean。上面配置中远程服务的url为:http://localhost:8888/httpInvoker/remoting/helloService,其中httpInvoker为该应用的虚拟路径。

客户端连接服务

Spring提供了HttpInvokerProxyFactoryBean工厂bean连接服务。类似于Hessian的HessianProxyFactoryBean,配置HttpInvokerProxyFactoryBean时,只需指定服务的url以及服务实现的接口。通过使用代理,Spring可将调用转换成POST请求发送到指定服务。详细的配置如下:

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

<!-- Spring配置文件的文件头,包含dtd等信息-->

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

   "http://www.springframework.org/dtd/spring-beans.dtd">

<!--  Spring配置文件的根元素-->

<beans>

        <!-- 配置测试bean,该测试bean依赖于远程服务bean-->

    <beanid="test" class="lee.Test">

                  <!--  配置依赖注入-->

          <propertyname="hello">

               <reflocal="helloService"/>

          </property>

   </bean>

        <!--  配置连接远程服务的bean-->

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

                  <!--  远程服务的url-->

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

                  <!--  远程服务所实现的接口-->

          <propertyname="serviceInterface" value="lee.Hello"/>

   </bean>

</beans>

通过上面的配置,客户端可以访问httpInvoker提供的服务,默认情况下,HttpInvokerPropxy使用J2SE的HTTPClient功能。可通过设置httpInvokerRequestExecutor属性使用CommonsHttpClient。通过对配置文件简单修改,将

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

                  <!--  远程服务的url-->

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

                  <!--  远程服务所实现的接口-->

          <propertyname="serviceInterface" value="lee.Hello"/>

   </bean>

修改成:

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

                  <!--  远程服务的url-->

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

                  <!--  远程服务所实现的接口-->

          <propertyname="serviceInterface" value="lee.Hello"/>

                  <!--  指定使用Commons HttpClient功能-->

                  <property name="httpInvokerRequestExecutor">

                           <bean

                                    class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/>

                  </property>

   </bean>

测试代码:    
               HelloService  service=(HelloService)applicationContext.getBean("helloService"); 
                Person person=service. hello (“my name”); 
               System.out.println("hello='"   person.getName()  "'");  

客户端可不配置,类似如下:

            HttpInvokerProxyFactoryBeanhttpInvoker = new HttpInvokerProxyFactoryBean();

               httpInvoker.setServiceInterface(com.synda.remote.ITerminal.class);

               httpInvoker.setServiceUrl("http://172.16.1.174:8080/remote/terminalHttpInvoker");

               ITerminal iterminal = (Iterminal)httpInvoker.getObject();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值