spring httpInvoke 解决远程调用远程的类的方法

http://zhidao.baidu.com/link?url=6FrnwvBQEZhjM-ooNCuiAra7T6qi9FsFhFvkHBKaOjqovZR86OCsIePi-05nM-fxRrlInEGbElSxlhgO6X7JsaGNdQdNrQ2xE58wglgeQO3 http://blog.csdn.net/liaq325/article/details/8281550 摘自以上 spring httpInvoke

spring httpInvoke 基于spring架构的服务器之间的远程调用实现。通过spring httpInvoke,可以调用远程接口,进行数据交互、业务逻辑操作

服务器端:(被调用一方)

[java] view plain copy
  1. public  class User implements Serializable{//必须实现serializable接口,远程调用的基础  
  2.     private String username;  
  3.     private Date birthday;  
  4.     //构造方法  
  5.     //set get 方法  
  6. }  
  7. public interface UserService{  
  8.     User getUser(String username);  
  9. }  
  10. public UserServiceImpl implements UserService{  
  11.     //实现userService  
  12. }  
重要的配置文件来了。。。。
remote-servlet.xml放在项目根目录下面,跟web.xml相同的级别

暴露给调用端:服务的实现,接口

[html] view plain copy
  1. <bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
  2.     <property name="service">  
  3.         <bean class="com.cd.Liaq.UserServiceImpl"/>  
  4.     </property>  
  5.     <property name="serviceInterface">  
  6.         <value>com.cd.Liaq.UserService</value>  
  7.     </property>  
  8. </bean>  
暴露了服务的实现和接口,那么怎么访问服务呢?
spring封装访问url

[html] view plain copy
  1. <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  2.     第一种:<property name="urlMap">  
  3.         <map>  
  4.             <entry key="TestUser" value-ref="userService"/>  
  5.         </map>  
  6.     </property>  
  7.     第二种:<prop key="/TestUser">userService</prop>  
  8. </bean>  
web.xml:配置dispatcherServlet共调用一方使用

[html] view plain copy
  1. <!-- spring远程调用 -->  
  2. <servlet>  
  3.     <servlet-name>remote</servlet-name>  
  4.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  5.     <load-on-startup>1</load-on-startup>  
  6. </servlet>  
  7. <servlet-mapping>  
  8.     <servlet-name>remote</servlet-name>  
  9.     <url-pattern>/remoting/*</url-pattern>  
  10. </servlet-mapping>  
到处为止:被调用端一方完毕!!!!
客户端调用:

[html] view plain copy
  1. <!-- 通过http连接远程系统 -->  
  2. <bean id="memberService"  
  3.     class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  4.     <property name="serviceUrl">  
  5.         <value>http://192.9.200.123:8080/MemberSystem/remoting/memberService</value>  
  6.     </property>  
  7.     <property name="serviceInterface">  
  8.         <value>com.cd.Liaq.UserService</value>  
  9.     </property>  
  10. </bean>  
通过spring容器调用UserService,用到HttpInvokerProxyFactoryBean工厂,配置serviceUrl和serviceInterface
为了提高效率:客户端使用Commons-HttpClient,导入改包,改写配置

[html] view plain copy
  1. <bean id="memberService"  
  2.     class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  3.     <property name="serviceUrl">  
  4.         <value>http://192.9.200.123:8080/MemberSystem/remoting/memberService</value>  
  5.     </property>  
  6.     <property name="serviceInterface">  
  7.         <value>com.cd.Liaq.UserService</value>  
  8.     </property>  
  9.      <property name="httpInvokerRequestExecutor"> //使用指定的执行器执行  
  10.         <ref bean="httpInvokerRequestExecutor" />    
  11.     </property>    
  12. </bean>  
  13. <bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">    
  14.     <property name="httpClient">    
  15.         <bean class="org.apache.commons.httpclient.HttpClient">    
  16.             <property name="connectionTimeout" value="2000" />    
  17.             <property name="timeout" value="5000" />    
  18.         </bean>    
  19.     </property>    
  20. </bean>    

配置超时时间timeout和连接超时connectionTimeout两个属性
优化执行器:多线程===被调用端响应时间缩短很多

[html] view plain copy
  1. <bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">    
  2.     <property name="httpClient">    
  3.         <bean class="org.apache.commons.httpclient.HttpClient">    
  4.             <property name="connectionTimeout" value="2000" />    
  5.             <property name="timeout" value="5000" />    
  6.             <property  name="httpConnectionManager">//控制连接  
  7.                     <ref  bean="multiThreadedHttpConnectionManager" />    
  8.             </property>    
  9.         </bean>    
  10.     </property>    
  11. </bean>    
  12. <bean id="multiThreadedHttpConnectionManager" class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">    
  13.     <property name="params">    
  14.         <bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">    
  15.             <property name="maxTotalConnections"  value="600" />    
  16.             <property name="defaultMaxConnectionsPerHost" value="512" />    
  17.         </bean>    
  18.     </property>    
  19. </bean>    
httpClient的3.1版本不支持这种配置

[html] view plain copy
  1. <property  name="connectionTimeout" value="2000" />      
  2. <property  name="timeout"  value="5000" />    

另外httpClient本身也是多线程的。。HttpClient that uses a default MultiThreadedHttpConnectionManage
<bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">  
    <property  name="maxTotalConnections"  value="600" />  
    <property  name="defaultMaxConnectionsPerHost"  value="512" />  
</bean>  
maxConnectionsPerHost 每个主机的最大并行链接数,默认为2 
public static final int DEFAULT_MAX_HOST_CONNECTIONS = 2; 
maxTotalConnections 客户端总并行链接最大数,默认为20  
public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 20; 

posted on 2016-06-19 10:29 youngturk 阅读(2239) 评论(0)  编辑  收藏 所属分类: 笔试题


只有注册用户登录后才能发表评论。


网站导航:
 
<2016年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

导航

统计

公告

this year :
1 jQuery
2 freemarker
3 框架结构
4 口语英语

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

EJB学习

Flex学习

learn English

oracle

spring MVC web service

SQL

Struts

生活保健

解析文件

搜索

最新评论

阅读排行榜

评论排行榜