spring httpInvoker 性能优化

Spring HttpInvoke,一种较为常用的、基于Spring架构的服务器之间的远程调用实现,可以说是轻量级的RMI

 

1.在web.xml配置spring 并添加对应的spring配置文件

 

Xml代码   收藏代码
  1. <listener>  
  2.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3.     </listener>  
  4.     <servlet>     
  5.      
  6.         <servlet-name>service</servlet-name>     
  7.      
  8.         <servlet-class>     
  9.      
  10.             org.springframework.web.servlet.DispatcherServlet      
  11.      
  12.         </servlet-class>     
  13.      
  14.         <load-on-startup>1</load-on-startup>     
  15.      
  16.     </servlet>     
  17.      
  18.        
  19.      
  20.     <servlet-mapping>     
  21.      
  22.         <servlet-name>service</servlet-name>     
  23.      
  24.         <url-pattern>/service/*</url-pattern>     
  25.      
  26.     </servlet-mapping>   

   对应的需要添加 service-servlet.xml

 

 

Xml代码   收藏代码
  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:p="http://www.springframework.org/schema/p"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.   
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  9.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  10.            http://www.springframework.org/schema/context     
  11.            http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  12.            http://www.springframework.org/schema/tx     
  13.            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     
  14.            http://www.springframework.org/schema/jdbc     
  15.            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
  16.         http://www.springframework.org/schema/mvc   
  17.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  18.   
  19.       
  20.   
  21. </beans>  

 

 

2.首先定义接口

Java代码   收藏代码
  1. public interface UserService {     
  2.     
  3.      
  4.     User getUser(String username);     
  5. }   

 3.定义返回的model

用户类,注意实现Serializable接口,这是执行远程调用传递数据对象的第一要求——数据对象必须实现Serializable接口,因为,要执行序列化/反序列化操作!

Java代码   收藏代码
  1. public class User implements Serializable {     
  2.     
  3.     private static final long serialVersionUID = 5590768569302443813L;     
  4.     private String username;     
  5.     private Date birthday;     
  6.     
  7.   
  8.     public User(String username, Date birthday) {     
  9.         this.username = username;     
  10.         this.birthday = birthday;     
  11.     }     
  12.   
  13.     @Override    
  14.     public String toString() {     
  15.         return String.format("%s\t%s\t", username, birthday);     
  16.     }     
  17. }    

 4. userservice 实现

Java代码   收藏代码
  1. public class UserServiceImpl implements UserService {     
  2.     private Logger logger = Logger.getLogger(UserServiceImpl.class);     
  3.     
  4.        @Override    
  5.     public User getUser(String username) {     
  6.         if (logger.isDebugEnabled()) {     
  7.             logger.debug("username:[" + username + "]");     
  8.         }     
  9.         User user = new User(username, new Date());     
  10.         if (logger.isDebugEnabled()) {     
  11.             logger.debug("user:[" + user + "]");     
  12.         }     
  13.         return user;     
  14.     }     
  15.     
  16. }    

 5.发不成http服务

 

Xml代码   收藏代码
  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:p="http://www.springframework.org/schema/p"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.   
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  9.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  10.            http://www.springframework.org/schema/context     
  11.            http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  12.            http://www.springframework.org/schema/tx     
  13.            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     
  14.            http://www.springframework.org/schema/jdbc     
  15.            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
  16.         http://www.springframework.org/schema/mvc   
  17.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  18.   
  19.     <bean id="httpService"  
  20.         class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
  21.         <property name="service" ref="userService" />  
  22.         <property name="serviceInterface" value="com.mpupa.service.IUserService" />  
  23.     </bean>  
  24.   
  25.     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  26.         <property name="mappings">  
  27.             <props>  
  28.                 <prop key="/httpService">httpService</prop>  
  29.             </props>  
  30.         </property>  
  31.     </bean>  
  32.   
  33. </beans>  

 

6.客户端调用

  首先要把 IUserService接口以及需要返回的Model放到客户端,打jar包或者直接把类拷贝过去

  然后配置 bean

 

Xml代码   收藏代码
  1. <bean id="httpService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  2.         <property name="serviceUrl">  
  3.             <value> https://apps.dotter.me/consumer/service/userHttpService  
  4.             </value>  
  5.         </property>  
  6.         <property name="serviceInterface" value="com.mpupa.service.IUserService">  
  7.         </property>  
  8. </bean>  

 

   在java类里调用

  

Java代码   收藏代码
  1. @Resource(name="httpService")  
  2.     IUserService userService;  
  3. User user = userService.getUser("test");  

 

 7.优化

    如果我们这样写,其实默认调用了SimpleHttpInvokerRequestExecutor做实现,这个实现恐怕只能作为演示来用! 
这也是效率问题所在!!! 
为提高效率,应该通过Commons-HttpClient! 
我们需要做什么?导入这个jar,改改xml就行!

Xml代码   收藏代码
  1. <bean id="httpService"  
  2.         class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  3.         <property name="serviceUrl">  
  4.             <value> https://apps.dotter.me/consumer/service/userHttpService  
  5.             </value>  
  6.         </property>  
  7.         <property name="serviceInterface" value="com.mpupa.service.IUserService">  
  8.         </property>  
  9.         <property name="httpInvokerRequestExecutor">  
  10.             <bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">  
  11.                 <property name="readTimeout" value="5000" />  
  12.                 <property name="connectTimeout" value="5000" />  
  13.             </bean>  
  14.          </property>  
  15.     </bean>  

 

或者

  

Java代码   收藏代码
  1. <bean id="httpService"  
  2.         class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  3.         <property name="serviceUrl">  
  4.             <value> https://apps.dotter.me/consumer/service/userHttpService  
  5.             </value>  
  6.         </property>  
  7.         <property name="serviceInterface" value="com.mpupa.service.IUserService">  
  8.         </property>  
  9.         <property    
  10.         name="httpInvokerRequestExecutor">    
  11.         <ref    
  12.             bean="httpInvokerRequestExecutor" />    
  13.     </property>    
  14.   
  15.     </bean>  
  16.   
  17. <bean    
  18.     id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">    
  19.     <property    
  20.         name="httpClient">    
  21.         <bean    
  22.             class="org.apache.commons.httpclient.HttpClient">    
  23.             <property    
  24.                 name="connectionTimeout"    
  25.                 value="2000" />    
  26.             <property    
  27.                 name="timeout"    
  28.                 value="5000" />    
  29.         </bean>    
  30.     </property>    
  31. </bean>    

这时,转为org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor实现了!,通过HttpClient,我们可以配置超时时间timeout和连接超时connectionTimeout两个属性,这样,服务器执行操作时,如果超时就可以强行释放连接,这样可怜的tomcat不会因为HttpInvoke连接不释放而被累死!

还可以控制线程数

Xml代码   收藏代码
  1. <bean    
  2.         id="httpInvokerRequestExecutor"    
  3.         class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">    
  4.         <property    
  5.             name="httpClient">    
  6.             <bean    
  7.                 class="org.apache.commons.httpclient.HttpClient">    
  8.                 <property    
  9.                     name="connectionTimeout"    
  10.                     value="2000" />    
  11.                 <property    
  12.                     name="timeout"    
  13.                     value="5000" />    
  14.                 <property    
  15.                     name="httpConnectionManager">    
  16.                     <ref    
  17.                         bean="multiThreadedHttpConnectionManager" />    
  18.                 </property>    
  19.             </bean>    
  20.         </property>    
  21.     </bean>    
  22.     <bean    
  23.         id="multiThreadedHttpConnectionManager"    
  24.         class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">    
  25.         <property    
  26.             name="params">    
  27.             <bean    
  28.                 class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">    
  29.                 <property    
  30.                     name="maxTotalConnections"    
  31.                     value="600" />    
  32.                 <property    
  33.                     name="defaultMaxConnectionsPerHost"    
  34.                     value="512" />    
  35.             </bean>    
  36.         </property>    
  37.     </bean>    

 改用MultiThreadedHttpConnectionManager,多线程!!! 
测试就不说了,实践证明: 
默认实现,服务器平均10s左右才能响应一个请求。 
多线程实现,服务器平均20ms左右响应一个请求。 
这简直不是一个数量级!!!


MultiThreadedHttpConnectionManager的使用原理参考:http://liuinsect.iteye.com/blog/1886237

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值