spring httpInvoke 解決遠(yuǎn)程調(diào)用遠(yuǎn)程的類(lèi)的方法

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

          spring httpInvoke 基于spring架構(gòu)的服務(wù)器之間的遠(yuǎn)程調(diào)用實(shí)現(xiàn)。通過(guò)spring httpInvoke,可以調(diào)用遠(yuǎn)程接口,進(jìn)行數(shù)據(jù)交互、業(yè)務(wù)邏輯操作

          服務(wù)器端:(被調(diào)用一方)

          [java] view plain copy
          1. public  class User implements Serializable{//必須實(shí)現(xiàn)serializable接口,遠(yuǎn)程調(diào)用的基礎(chǔ)  
          2.     private String username;  
          3.     private Date birthday;  
          4.     //構(gòu)造方法  
          5.     //set get 方法  
          6. }  
          7. public interface UserService{  
          8.     User getUser(String username);  
          9. }  
          10. public UserServiceImpl implements UserService{  
          11.     //實(shí)現(xiàn)userService  
          12. }  
          重要的配置文件來(lái)了。。。。
          remote-servlet.xml放在項(xiàng)目根目錄下面,跟web.xml相同的級(jí)別

          暴露給調(diào)用端:服務(wù)的實(shí)現(xiàn),接口

          [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>  
          暴露了服務(wù)的實(shí)現(xiàn)和接口,那么怎么訪問(wèn)服務(wù)呢?
          spring封裝訪問(wèn)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共調(diào)用一方使用

          [html] view plain copy
          1. <!-- spring遠(yuǎn)程調(diào)用 -->  
          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>  
          到處為止:被調(diào)用端一方完畢!!!!
          客戶(hù)端調(diào)用:

          [html] view plain copy
          1. <!-- 通過(guò)http連接遠(yuǎn)程系統(tǒng) -->  
          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>  
          通過(guò)spring容器調(diào)用UserService,用到HttpInvokerProxyFactoryBean工廠,配置serviceUrl和serviceInterface
          為了提高效率:客戶(hù)端使用Commons-HttpClient,導(dǎo)入改包,改寫(xiě)配置

          [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"> //使用指定的執(zhí)行器執(zhí)行  
          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>    

          配置超時(shí)時(shí)間timeout和連接超時(shí)connectionTimeout兩個(gè)屬性
          優(yōu)化執(zhí)行器:多線程===被調(diào)用端響應(yīng)時(shí)間縮短很多

          [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 每個(gè)主機(jī)的最大并行鏈接數(shù),默認(rèn)為2 
          public static final int DEFAULT_MAX_HOST_CONNECTIONS = 2; 
          maxTotalConnections 客戶(hù)端總并行鏈接最大數(shù),默認(rèn)為20  
          public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 20; 

          posted on 2016-06-19 10:29 youngturk 閱讀(2259) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 筆試題

          <2016年6月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          this year :
          1 jQuery
          2 freemarker
          3 框架結(jié)構(gòu)
          4 口語(yǔ)英語(yǔ)

          常用鏈接

          留言簿(6)

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          相冊(cè)

          EJB學(xué)習(xí)

          Flex學(xué)習(xí)

          learn English

          oracle

          spring MVC web service

          SQL

          Struts

          生活保健

          解析文件

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 荣成市| 临沧市| 新丰县| 新宾| 密云县| 监利县| 绥宁县| 萨迦县| 墨脱县| 筠连县| 汉寿县| 德安县| 黄浦区| 赣州市| 吉木萨尔县| 收藏| 毕节市| 商水县| 论坛| 盐边县| 南京市| 汶上县| 德钦县| 砀山县| 来宾市| 体育| 璧山县| 禄劝| 牡丹江市| 宣威市| 六盘水市| 镶黄旗| 沛县| 普陀区| 涿州市| 台东县| 读书| 大新县| 许昌市| 高安市| 海伦市|