我有個(gè)web項(xiàng)目,接受postjson請求,解析字符串入索引,實(shí)現(xiàn)批量插入?,F(xiàn)在遇到一個(gè)問題,大多數(shù)請求都是正常的,但是偶爾會(huì)出現(xiàn)幾個(gè)請求,它們提交給我的json是殘缺的,不完整,導(dǎo)致我的json解析失敗??蛻舳耸莂pache HttpClient.
服務(wù)端現(xiàn)象:
][1]
客戶端httpclient是通過解析實(shí)體生成json發(fā)過來的,httpclient部分代碼:
注:buildHttpClient 方法的是做重試的,沒有別的業(yè)務(wù)
public static String postJson(String url, String authorName, String authorPass, String json) throws Exception {
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
long begin = System.currentTimeMillis();
try {
httpClient = buildHttpClient();
httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
StringEntity se = new StringEntity(json, ContentType.APPLICATION_JSON);
se.setContentType("application/json");
if (!StringUtils.isBlank(authorName) && !StringUtils.isBlank(authorPass)) {
String author = getAuthor(authorName, authorPass);
if (StringUtils.isNotBlank(author)) {
httpPost.setHeader("Authorization", author);
}
}
httpPost.setEntity(se);
begin = System.currentTimeMillis();
HttpResponse response = httpClient.execute(httpPost, HttpClientContext.create());
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, CHARSET_UTF8);
}
}
} catch (Exception e) {
long end = System.currentTimeMillis();
logger.error("postEntity error,cost time:"+(end - begin)+" exception :", e);
throw e;
}finally {
httpClient.close();
}
return result;
}
private static CloseableHttpClient buildHttpClient(){
return HttpClients.custom().setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
System.out.println("retrying.... "+executionCount);
if (executionCount > 3) {
System.out.println("final try.... "+executionCount);
logger.info("retry reach to 3 times");
return false;
}
if (exception instanceof NoHttpResponseException) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("NoHttpResponse start to retry times:"+executionCount);
System.out.println("NoHttpResponse start to retry times:"+executionCount);
System.out.println("retry success, NoHttpResponse retry times: "+executionCount);
return true;
}
if (exception instanceof SocketTimeoutException){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("SocketTimeout start to retry times:"+executionCount);
System.out.println("SocketTimeout start to retry times:"+executionCount);
System.out.println("retry success, SocketTimeout retry times: "+executionCount);
return true;
}
if (exception instanceof ConnectionPoolTimeoutException){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("ConnectionPoolTimeout start to retry times:"+executionCount);
System.out.println("ConnectionPoolTimeoutException start to retry times:"+executionCount);
System.out.println("retry success, ConnectionPoolTimeoutException retry times: "+executionCount);
return true;
}
if (exception instanceof ConnectTimeoutException){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("ConnectionPoolTimeout start to retry times:"+executionCount);
System.out.println("ConnectTimeoutException start to retry times:"+executionCount);
System.out.println("retry success, ConnectTimeoutException retry times: "+executionCount);
return true;
}
if (exception instanceof ConnectException){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("ConnectException start to retry times:"+executionCount);
System.out.println("ConnectException start to retry times:"+executionCount);
System.out.println("retry success, ConnectException retry times: "+executionCount);
return true;
}
if (exception instanceof IOException){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("IOException start to retry times:"+executionCount);
System.out.println("IOException start to retry times:"+executionCount);
System.out.println("retry success, IOException retry times: "+executionCount);
return true;
}
return false;
}
}).build();
}
服務(wù)端通過HttpServletRequest讀取流解析,沒有用RequestBody 因?yàn)槌霈F(xiàn)這個(gè)問題后,我為了得到每次插入的數(shù)據(jù)大小就自己做解析了。
這是個(gè)批量插入的rest接口,發(fā)現(xiàn)每個(gè)異常出現(xiàn)的時(shí)候,json大小都不固定,有的60多K,有的300k,但是大多數(shù)請求json都是可以被正常解析的。
順便說明下,這個(gè)接口的調(diào)用量還是比較頻繁的,都是寫入,一共6臺(tái)機(jī)器,單臺(tái)TPS大概在20-30/s,寫入elasticsearch平均耗時(shí)80ms-200ms,http請求響應(yīng)時(shí)間平均耗時(shí)300-800ms
有沒有遇到同類現(xiàn)象的朋友,你們是怎么處理的呢?
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。