国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看
打開APP
未登錄
開通VIP,暢享免費電子書等14項超值服
開通VIP
首頁
好書
留言交流
下載APP
聯(lián)系客服
Spring HttpInvoke實現(xiàn),以及效率提升!
redalx
>《spring》
2015.06.15
關(guā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代碼
/**
* @author <a href="mailto:zlex.dongliang@gmail.com">梁棟</a>
* @since 1.0
*/
public
interface
UserService {
/**
* 獲得用戶
*
* @param username
* 用戶名
* @return
*/
User getUser(String username);
}
用戶類,注意實現(xiàn)
Serializable
接口,這是執(zhí)行遠程調(diào)用傳遞數(shù)據(jù)對象的第一要求——數(shù)據(jù)對象必須實現(xiàn)
Serializable
接口,因為,要執(zhí)行序列化/反序列化操作!
Java代碼
/**
* @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實現(xiàn):
Java代碼
/**
* @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;
}
}
只把用戶信息打出來即可說明調(diào)用效果!
看applicationContext.xml
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接口調(diào)用這個接口的實現(xiàn)了。
說說HttpInvokerServiceExporter,這個類用來在服務(wù)器端包裝需要暴露的接口。
熟悉service,定義具體的實現(xiàn)類!
Xml代碼
<
property
name
=
"service"
>
<
bean
lass
=
"org.zlex.spring.httpinvoke.service.impl.UserServiceImpl"
/>
</
property
>
熟悉serviceInterface指向需要暴露的接口,
注意使用value標注接口名稱!
Xml代碼
<
property
name
=
"serviceInterface"
value
=
"org.zlex.spring.httpinvoke.service.UserService"
/>
最后再看servlet.xml配置
Xml代碼
<
bean
class
=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
<
property
name
=
"urlMap"
>
<
map
>
<
entry
key
=
"*"
value-ref
=
"userService"
/>
</
map
>
</
property
>
</
bean
>
直接將請求指向剛才配置的userService
現(xiàn)在我們之間訪問一下http://localhost:8080/spring/service/
這就說明,服務(wù)器端配置已經(jīng)成功了!如果在日志中頻繁得到這種異常,那很可能服務(wù)器被惡意訪問了!
再看客戶端實現(xiàn):
Java代碼
/**
* @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!就跟調(diào)用一般Spring容器中的實現(xiàn)一樣!
再看applicationContext.xml:
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"
/>
</
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代碼
<
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兩個屬性,這樣,服務(wù)器執(zhí)行操作時,如果超時就可以強行釋放連接,這樣可憐的tomcat不會因為HttpInvoke連接不釋放而被累死!
回頭看了一眼我N多年前的代碼,萬歲,我當(dāng)時確實是這么實現(xiàn)的!好在沒有犯低級錯誤!??!
執(zhí)行操作!
這時,轉(zhuǎn)為org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor實現(xiàn)了!
不過同事認為,這個效率還是不夠高!??!
再改,改什么?還是xml!
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
,多線程!??!
測試就不說了,實踐證明:
默認實現(xiàn),服務(wù)器平均10s左右才能響應(yīng)一個請求。
多線程實現(xiàn),服務(wù)器平均20ms左右響應(yīng)一個請求。
這簡直不是一個數(shù)量級!??!
注意:在HttpClient的3.1版本中,已不支持如下配置,相應(yīng)的方法已經(jīng)廢棄!
Xml代碼
<
property
name
=
"connectionTimeout"
value
=
"2000"
/>
<
property
name
=
"timeout"
value
=
"5000"
/>
如果仔細看看文檔,
引用
HttpClient that uses a default MultiThreadedHttpConnectionManager.
commons 系列的實現(xiàn)怎么會不考慮多線程呢?人家默認實現(xiàn)就是多線程的!同事多慮了!
當(dāng)然,同事還補充了一句,需要控制連接數(shù)!
難怪,這里要設(shè)置
Xml代碼
<
bean
class
=
"org.apache.commons.httpclient.params.HttpConnectionManagerParams"
>
<
property
name
=
"maxTotalConnections"
value
=
"600"
/>
<
property
name
=
"defaultMaxConnectionsPerHost"
value
=
"512"
/>
</
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ù)
首頁
萬象
文化
人生
生活
健康
教育
職場
理財
娛樂
藝術(shù)
上網(wǎng)
留言交流
回頂部
聯(lián)系我們
分享
收藏
點擊這里,查看已保存的文章
導(dǎo)長圖
關(guān)注
一鍵復(fù)制
下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!
聯(lián)系客服
微信登錄中...
請勿關(guān)閉此頁面
先別劃走!
送你5元優(yōu)惠券,購買VIP限時立減!
5
元
優(yōu)惠券
優(yōu)惠券還有
10:00
過期
馬上使用
×