Spring HttpInvoke,一種較爲經常使用的、基於Spring架構的服務器之間的遠程調用實現,能夠說是輕量級的RMI。
最初,咱們使用Spring HttpInvoke同步配置數據,刷新多個服務器上的緩存,固然若是用分佈式緩存是否是更好
!
使用Spring HttpInvoke,你能夠調用遠程接口,進行數據交互、業務邏輯操做等等。
廢話不說了,上代碼!
用戶操做接口:
- /**
- * @author <a href="mailto:zlex.dongliang@gmail.com">樑棟</a>
- * @since 1.0
- */
- public interface UserService {
-
- /**
- * 得到用戶
- *
- * @param username
- * 用戶名
- * @return
- */
- User getUser(String username);
- }
用戶類,注意實現Serializable接口,這是執行遠程調用傳遞數據對象的第一要求——數據對象必須實現Serializable接口,由於,要執行序列化/反序列化操做!
- /**
- * @author <a href="mailto:zlex.dongliang@gmail.com">樑棟</a>
- * @since 1.0
- */
- public class User implements Serializable {
-
- private static final long serialVersionUID = 5590768569302443813L;
- private String username;
- private Date birthday;
-
- /**
- * @param username
- * @param birthday
- */
- public User(String username, Date birthday) {
- this.username = username;
- this.birthday = birthday;
- }
- // 省略
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- return String.format("%s\t%s\t", username, birthday);
- }
- }
覆蓋toString()方法,輸出用戶信息!
再看UserServiceImpl實現:
- /**
- * @author <a href="mailto:zlex.dongliang@gmail.com">樑棟</a>
- * @since 1.0
- */
- public class UserServiceImpl implements UserService {
- private Logger logger = Logger.getLogger(UserServiceImpl.class);
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.zlex.spring.httpinvoke.service.UserService#getUser(java.lang.String)
- */
- @Override
- public User getUser(String username) {
- if (logger.isDebugEnabled()) {
- logger.debug("username:[" + username + "]");
- }
- User user = new User(username, new Date());
- if (logger.isDebugEnabled()) {
- logger.debug("user:[" + user + "]");
- }
- return user;
- }
-
- }
只把用戶信息打出來便可說明調用效果!
看applicationContext.xml
- <bean
- id="userService"
- class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
- <property
- name="service">
- <bean
- class="org.zlex.spring.httpinvoke.service.impl.UserServiceImpl" />
- </property>
- <property
- name="serviceInterface"
- value="org.zlex.spring.httpinvoke.service.UserService" />
- </bean>
咱們要把userService暴露出去,這樣外部就能夠經過http接口調用這個接口的實現了。
說說HttpInvokerServiceExporter,這個類用來在服務器端包裝須要暴露的接口。
熟悉service,定義具體的實現類!
- <property
- name="service">
- <bean
- lass="org.zlex.spring.httpinvoke.service.impl.UserServiceImpl" />
- </property>
熟悉serviceInterface指向須要暴露的接口,注意使用value標註接口名稱!
- <property
- name="serviceInterface"
- value="org.zlex.spring.httpinvoke.service.UserService" />
最後再看service-servlet.xml配置
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property
- name="urlMap">
- <map>
- <entry
- key="userTest"
- value-ref="userService" />
- </map>
- </property>
- </bean>
直接將請求指向剛纔配置的userService
如今咱們之間訪問一下http://localhost:8080/spring/service/
這就說明,服務器端配置已經成功了!若是在日誌中頻繁獲得這種異常,那極可能服務器被惡意訪問了!
再看客戶端實現:
- /**
- * @author <a href="mailto:zlex.dongliang@gmail.com">樑棟</a>
- * @since 1.0
- */
- public class UserServiceTest {
- private Logger logger = Logger.getLogger(UserServiceTest.class);
- private ApplicationContext context;
-
- private UserService userService;
-
- @Before
- public void initialize() {
- context = new ClassPathXmlApplicationContext("applicationContext.xml");
- userService = (UserService) context.getBean("userService");
- }
-
- @Test
- public void getUser() {
- User user = userService.getUser("zlex");
- if (logger.isDebugEnabled()) {
- logger.debug("user[" + user + "]");
- }
- }
- }
咱們作了什麼?Nothing!就跟調用通常Spring容器中的實現同樣!
再看applicationContext.xml:
- <bean
- id="userService"
- class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
- <property
- name="serviceUrl"
- value="http://localhost:8080/spring/service/userTest" />
- <property
- name="serviceInterface"
- value="org.zlex.spring.httpinvoke.service.UserService" />
- </bean>
這裏咱們能夠經過Spring容器調用userService,而實際上,他是一個HttpInvokerProxyFactoryBean,在這個配置裏,定義了訪問地址serviceUrl,和訪問接口serviceInterface。
執行測試!
若是咱們這樣寫,其實默認調用了SimpleHttpInvokerRequestExecutor作實現,這個實現恐怕只能做爲演示來用!
這也是效率問題所在!!!
爲提升效率,應該經過Commons-HttpClient!
咱們須要作什麼?導入這個jar,改改xml就行!
- <bean
- id="userService"
- class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
- <property
- name="serviceUrl"
- value="http://localhost:8080/spring/service" />
- <property
- name="serviceInterface"
- value="org.zlex.spring.httpinvoke.service.UserService" />
- <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="2000" />
- <property
- name="timeout"
- value="5000" />
- </bean>
- </property>
- </bean>
經過HttpClient,咱們能夠配置超時時間timeout和鏈接超時connectionTimeout兩個屬性,這樣,服務器執行操做時,若是超時就能夠強行釋放鏈接,這樣可憐的tomcat不會由於HttpInvoke鏈接不釋放而被累死!
回頭看了一眼我N多年前的代碼,萬歲,我當時確實是這麼實現的!好在沒有犯低級錯誤!!!
執行操做!
這時,轉爲org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor實現了!
不過同事認爲,這個效率仍是不夠高!!!
再改,改什麼?仍是xml!
- <bean
- id="httpInvokerRequestExecutor"
- class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
- <property
- name="httpClient">
- <bean
- class="org.apache.commons.httpclient.HttpClient">
- <property
- name="connectionTimeout"
- value="2000" />
- <property
- name="timeout"
- value="5000" />
- <property
- name="httpConnectionManager">
- <ref
- bean="multiThreadedHttpConnectionManager" />
- </property>
- </bean>
- </property>
- </bean>
- <bean
- id="multiThreadedHttpConnectionManager"
- class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
- <property
- name="params">
- <bean
- class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
- <property
- name="maxTotalConnections"
- value="600" />
- <property
- name="defaultMaxConnectionsPerHost"
- value="512" />
- </bean>
- </property>
- </bean>
改用MultiThreadedHttpConnectionManager,多線程!!!
測試就不說了,實踐證實:
默認實現,服務器平均10s左右才能響應一個請求。
多線程實現,服務器平均20ms左右響應一個請求。
這簡直不是一個數量級!!!
注意:在HttpClient的3.1版本中,已不支持以下配置,相應的方法已經廢棄!
- <property
- name="connectionTimeout"
- value="2000" />
- <property
- name="timeout"
- value="5000" />
若是仔細看看文檔,
引用
HttpClient that uses a default MultiThreadedHttpConnectionManager.
commons 系列的實現怎麼會不考慮多線程呢?人家默認實現就是多線程的!同事多慮了!
固然,同事還補充了一句,須要控制鏈接數!
難怪,這裏要設置
- <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;
--如下是藝術家補充java
增長一個service-servlet.xm(對應urlMap)l,放在web-inf目錄下 web
web.xml中配置 spring
<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>/service/*</url-pattern>
</servlet-mapping>