国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Spring HttpInvoke實現(xiàn),以及效率提升!
最近接手服務(wù)器總被人質(zhì)疑效率問題,說到底是質(zhì)疑Spring HttpInvoke的效率問題。好在經(jīng)過同事們的努力,找到了問題的根源,最終解決了這個問題。
我也順道整理一下Spring HttpInvoke——那曾經(jīng)最為熟悉的東西。
Spring HttpInvoke,一種較為常用的、基于Spring架構(gòu)的服務(wù)器之間的遠程調(diào)用實現(xiàn),可以說是輕量級的RMI。
最初,我們使用Spring HttpInvoke同步配置數(shù)據(jù),刷新多個服務(wù)器上的緩存,當(dāng)然如果用分布式緩存是不是更好
!
使用Spring HttpInvoke,你可以調(diào)用遠程接口,進行數(shù)據(jù)交互、業(yè)務(wù)邏輯操作等等。
廢話不說了,上代碼!
用戶操作接口:
Java代碼  
  1. /** 
  2.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁棟</a> 
  3.  * @since 1.0 
  4.  */  
  5. public interface UserService {  
  6.   
  7.     /** 
  8.      * 獲得用戶 
  9.      *  
  10.      * @param username 
  11.      *            用戶名 
  12.      * @return 
  13.      */  
  14.     User getUser(String username);  
  15. }  

用戶類,注意實現(xiàn)Serializable接口,這是執(zhí)行遠程調(diào)用傳遞數(shù)據(jù)對象的第一要求——數(shù)據(jù)對象必須實現(xiàn)Serializable接口,因為,要執(zhí)行序列化/反序列化操作!
Java代碼  
  1. /** 
  2.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁棟</a> 
  3.  * @since 1.0 
  4.  */  
  5. public class User implements Serializable {  
  6.   
  7.     private static final long serialVersionUID = 5590768569302443813L;  
  8.     private String username;  
  9.     private Date birthday;  
  10.   
  11.     /** 
  12.      * @param username 
  13.      * @param birthday 
  14.      */  
  15.     public User(String username, Date birthday) {  
  16.         this.username = username;  
  17.         this.birthday = birthday;  
  18.     }  
  19.        // 省略  
  20.     /* 
  21.      * (non-Javadoc) 
  22.      *  
  23.      * @see java.lang.Object#toString() 
  24.      */  
  25.     @Override  
  26.     public String toString() {  
  27.         return String.format("%s\t%s\t", username, birthday);  
  28.     }  
  29. }  

覆蓋toString()方法,輸出用戶信息!
再看UserServiceImpl實現(xiàn):
Java代碼  
  1. /** 
  2.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁棟</a> 
  3.  * @since 1.0 
  4.  */  
  5. public class UserServiceImpl implements UserService {  
  6.     private Logger logger = Logger.getLogger(UserServiceImpl.class);  
  7.   
  8.     /* 
  9.      * (non-Javadoc) 
  10.      *  
  11.      * @see 
  12.      * org.zlex.spring.httpinvoke.service.UserService#getUser(java.lang.String) 
  13.      */  
  14.     @Override  
  15.     public User getUser(String username) {  
  16.         if (logger.isDebugEnabled()) {  
  17.             logger.debug("username:[" + username + "]");  
  18.         }  
  19.         User user = new User(username, new Date());  
  20.         if (logger.isDebugEnabled()) {  
  21.             logger.debug("user:[" + user + "]");  
  22.         }  
  23.         return user;  
  24.     }  
  25.   
  26. }  

只把用戶信息打出來即可說明調(diào)用效果!
看applicationContext.xml
Xml代碼  
  1. <bean  
  2.     id="userService"  
  3. class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
  4.     <property  
  5.         name="service">  
  6.         <bean  
  7. class="org.zlex.spring.httpinvoke.service.impl.UserServiceImpl" />  
  8.     </property>  
  9.     <property  
  10.         name="serviceInterface"  
  11.         value="org.zlex.spring.httpinvoke.service.UserService" />  
  12. </bean>  

我們要把userService暴露出去,這樣外部就可以通過http接口調(diào)用這個接口的實現(xiàn)了。
說說HttpInvokerServiceExporter,這個類用來在服務(wù)器端包裝需要暴露的接口。
熟悉service,定義具體的實現(xiàn)類!
Xml代碼  
  1. <property  
  2.     name="service">  
  3.     <bean  
  4. lass="org.zlex.spring.httpinvoke.service.impl.UserServiceImpl" />  
  5. </property>  

熟悉serviceInterface指向需要暴露的接口,注意使用value標注接口名稱!
Xml代碼  
  1. <property  
  2.     name="serviceInterface"  
  3.     value="org.zlex.spring.httpinvoke.service.UserService" />  

最后再看servlet.xml配置
Xml代碼  
  1. <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  2.     <property  
  3.         name="urlMap">  
  4.         <map>  
  5.             <entry  
  6.                 key="*"  
  7.                 value-ref="userService" />  
  8.         </map>  
  9.     </property>  
  10. </bean>  

直接將請求指向剛才配置的userService
現(xiàn)在我們之間訪問一下http://localhost:8080/spring/service/

這就說明,服務(wù)器端配置已經(jīng)成功了!如果在日志中頻繁得到這種異常,那很可能服務(wù)器被惡意訪問了!
再看客戶端實現(xiàn):
Java代碼  
  1. /** 
  2.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁棟</a> 
  3.  * @since 1.0 
  4.  */  
  5. public class UserServiceTest {  
  6.     private Logger logger = Logger.getLogger(UserServiceTest.class);  
  7.     private ApplicationContext context;  
  8.   
  9.     private UserService userService;  
  10.   
  11.     @Before  
  12.     public void initialize() {  
  13.         context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  14.         userService = (UserService) context.getBean("userService");  
  15.     }  
  16.   
  17.     @Test  
  18.     public void getUser() {  
  19.         User user = userService.getUser("zlex");  
  20.         if (logger.isDebugEnabled()) {  
  21.             logger.debug("user[" + user + "]");  
  22.         }  
  23.     }  
  24. }  

我們做了什么?Nothing!就跟調(diào)用一般Spring容器中的實現(xiàn)一樣!
再看applicationContext.xml:
Xml代碼  
  1. <bean  
  2.     id="userService"  
  3. class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  4.     <property  
  5.         name="serviceUrl"  
  6.         value="http://localhost:8080/spring/service" />  
  7.     <property  
  8.         name="serviceInterface"  
  9.         value="org.zlex.spring.httpinvoke.service.UserService" />  
  10. </bean>  

這里我們可以通過Spring容器調(diào)用userService,而實際上,他是一個HttpInvokerProxyFactoryBean,在這個配置里,定義了訪問地址serviceUrl,和訪問接口serviceInterface。
執(zhí)行測試!

如果我們這樣寫,其實默認調(diào)用了SimpleHttpInvokerRequestExecutor做實現(xiàn),這個實現(xiàn)恐怕只能作為演示來用!
這也是效率問題所在!?。?br>為提高效率,應(yīng)該通過Commons-HttpClient!
我們需要做什么?導(dǎo)入這個jar,改改xml就行!
Xml代碼  
  1. <bean  
  2.     id="userService"  
  3.     class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
  4.     <property  
  5.         name="serviceUrl"  
  6.         value="http://localhost:8080/spring/service" />  
  7.     <property  
  8.         name="serviceInterface"  
  9.         value="org.zlex.spring.httpinvoke.service.UserService" />  
  10.     <property  
  11.         name="httpInvokerRequestExecutor">  
  12.         <ref  
  13.             bean="httpInvokerRequestExecutor" />  
  14.     </property>  
  15. </bean>  
  16. <bean  
  17.     id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">  
  18.     <property  
  19.         name="httpClient">  
  20.         <bean  
  21.             class="org.apache.commons.httpclient.HttpClient">  
  22.             <property  
  23.                 name="connectionTimeout"  
  24.                 value="2000" />  
  25.             <property  
  26.                 name="timeout"  
  27.                 value="5000" />  
  28.         </bean>  
  29.     </property>  
  30. </bean>  

通過HttpClient,我們可以配置超時時間timeout和連接超時connectionTimeout兩個屬性,這樣,服務(wù)器執(zhí)行操作時,如果超時就可以強行釋放連接,這樣可憐的tomcat不會因為HttpInvoke連接不釋放而被累死!

回頭看了一眼我N多年前的代碼,萬歲,我當(dāng)時確實是這么實現(xiàn)的!好在沒有犯低級錯誤!??!
執(zhí)行操作!

這時,轉(zhuǎn)為org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor實現(xiàn)了!
不過同事認為,這個效率還是不夠高!??!
再改,改什么?還是xml!
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,多線程!??!
測試就不說了,實踐證明:
默認實現(xiàn),服務(wù)器平均10s左右才能響應(yīng)一個請求。
多線程實現(xiàn),服務(wù)器平均20ms左右響應(yīng)一個請求。
這簡直不是一個數(shù)量級!??!

注意:在HttpClient的3.1版本中,已不支持如下配置,相應(yīng)的方法已經(jīng)廢棄!
Xml代碼  
  1. <property    
  2.     name="connectionTimeout"    
  3.     value="2000" />    
  4. <property    
  5.     name="timeout"    
  6.     value="5000" />   


如果仔細看看文檔,
引用
HttpClient that uses a default MultiThreadedHttpConnectionManager.

commons 系列的實現(xiàn)怎么會不考慮多線程呢?人家默認實現(xiàn)就是多線程的!同事多慮了!
當(dāng)然,同事還補充了一句,需要控制連接數(shù)!
難怪,這里要設(shè)置
Xml代碼  
  1. <bean  
  2.     class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">  
  3.     <property  
  4.         name="maxTotalConnections"  
  5.         value="600" />  
  6.     <property  
  7.         name="defaultMaxConnectionsPerHost"  
  8.         value="512" />  
  9. </bean>  


默認啥情況?
引用
maxConnectionsPerHost 每個主機的最大并行鏈接數(shù),默認為2
public static final int DEFAULT_MAX_HOST_CONNECTIONS = 2;
maxTotalConnections 客戶端總并行鏈接最大數(shù),默認為20 
public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 20;


技高一籌!

趕緊記下來,供大家參考!

詳情見附件!

許久沒寫博客了,今天終于舒展了一把!

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Spring、Hibernate和范型
SSH框架整合示例
Spring3.2.4 和 MyBatis 整合筆記
Struts調(diào)用Spring服務(wù)類的三種方法
使用Spring
Redis整合Spring結(jié)合使用緩存實例
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服