spring http invoker学习笔记

[b]概念[/b]
Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制在网络间传递对象。
[b]效率[/b]
远程调用效率: rmi>spring http invoker>webservice
[b]优点[/b]
1.spring http invoker 采用java的序列化机制,并采用类似rmi的方式调用,既能够使用http协议轻松穿过防火墙,又能够保证高效的远程调用。
2.轻量级的远程调用模型,对于采用spring的项目可以无缝嵌入,侵入性小。
3.非常简单,容易上手。

[b]例子[/b]
[b]服务端[/b]
1.web.xml

<servlet>
<servlet-name>service</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>service</servlet-name>
<url-pattern>/httpservice/*</url-pattern>
</servlet-mapping>

2. spring配置文件

<beans>
<bean id="userInfoServiceProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="userInfoService"/>
<property name="serviceInterface" value="cn.gov.zjport.skeleton.sso.UserInfoService"/>
</bean>
</beans>

其中userInfoService为项目原有的service
3. 增加一个service-servlet.xml,放在web-inf目录下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/userInfoService">userInfoServiceProxy</prop>
</props>
</property>
</bean>
</beans>

至此服务端发布完毕。

[b]客户端[/b]
1. spring配置文件

<bean id="userInfoService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://<server>:<port>/<domain>/httpservice/userInfoService"/>
<property name="serviceInterface" value="cn.gov.zjport.skeleton.sso.UserInfoService"/>
<property name="httpInvokerRequestExecutor">
<ref bean="httpInvokerRequestExecutor" />
</property>
</bean>
<bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<property name="httpClient">
<bean class="org.apache.commons.httpclient.HttpClient">
<property name="connectionTimeout" value="3000" />
<property name="timeout" value="5000" />
</bean>
</property>
</bean>

2. 调用代码

String xml=userInfoService.getUserInfo("simpleadmin", true);

与非远程调用代码一摸一样。

[b]单元测试[/b]
如果客户端只是想做个单元测试,要集成spring就比较重,可以采用以下代码测试服务接口:

HttpInvokerProxyFactoryBean bean = new HttpInvokerProxyFactoryBean();
bean.setServiceInterface(UserInfoService.class);
bean.setServiceUrl("http://<server>:<port>/<domain>/httpservice/userInfoService");
bean.afterPropertiesSet();
UserInfoService userInfoService= (UserInfoService) bean.getObject();
String xml=userInfoService.getUserInfo("simpleadmin", true);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值