故障很多時候都是來源于你的不小心
周五給負(fù)責(zé)的一個EPush推送平臺發(fā)布的一個版本排查問題∑堆撸現(xiàn)象就是同事剛發(fā)布完線上,幾分鐘就出現(xiàn)瀏覽器端連不上EPush推送服務(wù),出現(xiàn)502錯誤肋乍。當(dāng)時第一件事就是讓同事把代碼立馬回滾到master。我們是采用分之發(fā)布的方式纵竖,EPush平臺負(fù)責(zé)去哪兒網(wǎng)商家訂單的實(shí)時推送⌒臃撸回滾以后推送服務(wù)正常靡砌。定位就是新上代碼的問題。所以開始扒拉代碼声邦。EPush采用兩種認(rèn)證方式一種就是cookie乏奥,另外一種就是API接口回調(diào)的方式。
我們先來看看一段API回調(diào)代碼亥曹。其實(shí)這次需求就是需要在請求頭上帶上uid邓了。
歷史代碼
<pre><code>
private boolean authApi(String app, String cookieStr, String authRule, HandshakeData handshakeData) {
if (StringUtils.isNotEmpty(authRule)) {
List<String> list = Lists.newArrayList(Splitter.on(",").trimResults().split(authRule));
if (list.size() >= 3) {
String apiUrl = list.get(0);
String[] urls = handshakeData.getUrl().split("\?");
if (urls.length > 1) {
// 如果定義的回調(diào)鏈接中已經(jīng)有參數(shù),拼接起來
if (apiUrl.contains("?")) {
apiUrl = apiUrl + "&" + urls[1];
} else {
apiUrl = apiUrl + "?" + urls[1];
}
}
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Cookie", cookieStr);
HttpEntity entity = null;
String result = null;
try {
CloseableHttpResponse response = httpClient.execute(post);
StatusLine status = response.getStatusLine();
if ((status != null) && (status.getStatusCode() == 200)) {
entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} else {
logger.warn("[EPush]連接權(quán)限校驗(yàn)時返回錯誤,app={},apiUrl={},status={}", app, apiUrl,
status != null ? status.getStatusCode() : "");
}
} catch (Exception e) {
logger.error("[EPush]連接權(quán)限校驗(yàn)時發(fā)生錯誤,app={},apiUrl={}", app, apiUrl, e);
} finally {
try {
EntityUtils.consume(entity);
} catch (IOException e) {
logger.error("", e);
}
}
if (StringUtils.isNotEmpty(result)) {
JSONObject jo = JSON.parseObject(result);
String authResult = jo.getString(list.get(1));
if (list.get(2).equalsIgnoreCase(authResult)) {
return true;
}
}
logger.warn("[EPush]auth fail, app={},apiUrl={},authResult={}", app, apiUrl, result);
}
}
logger.info("[EPush]auth fail, app={}, cookie={}", app, cookieStr);
return false;
}
</code></pre>
新修改的故障代碼
<pre>
<code>
private boolean authApi(String app, String cookieStr, String authRule, HandshakeData handshakeData) {
if (StringUtils.isNotEmpty(authRule)) {
List<String> list = Lists.newArrayList(Splitter.on(",").trimResults().split(authRule));
if (list.size() > 0) {
String apiUrl = list.get(0);
String[] urls = handshakeData.getUrl().split("\?");
if (urls.length > 1) {
// 如果定義的回調(diào)鏈接中已經(jīng)有參數(shù)媳瞪,拼接起來
if (apiUrl.contains("?")) {
apiUrl = apiUrl + "&" + urls[1];
} else {
apiUrl = apiUrl + "?" + urls[1];
}
}
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Cookie", cookieStr);
CloseableHttpResponse response = null;
HttpEntity entity = null;
String result = null;
try {
response = httpClient.execute(post);
StatusLine status = response.getStatusLine();
if ((status != null) && (status.getStatusCode() == 200)) {
Header header = response.getFirstHeader(EpushContants.EPUSH_UID);
String uid = header != null ? header.getValue() : "";
handshakeData.getHeaders().put(EpushContants.EPUSH_UID, Lists.newArrayList(uid));
return true;
} else {
logger.warn("[EPush]連接權(quán)限校驗(yàn)時返回錯誤,app={},apiUrl={},status={}", app, apiUrl,
status != null ? status.getStatusCode() : "");
}
} catch (Exception e) {
logger.error("[EPush]連接權(quán)限校驗(yàn)時發(fā)生錯誤,app={},apiUrl={}", app, apiUrl, e);
} finally {
try {
EntityUtils.consume(entity);
if (response != null) {
response.close();
}
} catch (IOException e) {
logger.error("", e);
}
}
logger.warn("[EPush]auth fail, app={},apiUrl={},authResult={}", app, apiUrl, result);
}
}
logger.info("[EPush]auth fail, app={}, cookie={}", app, cookieStr);
return false;
}
</code>
</pre>
排除問題的正常代碼
<pre><code>
private boolean authApi(String app, String cookieStr, String authRule, HandshakeData handshakeData) {
if (StringUtils.isNotEmpty(authRule)) {
List<String> list = Lists.newArrayList(Splitter.on(",").trimResults().split(authRule));
if (list.size() > 0) {
String apiUrl = list.get(0);
String[] urls = handshakeData.getUrl().split("\?");
if (urls.length > 1) {
// 如果定義的回調(diào)鏈接中已經(jīng)有參數(shù)骗炉,拼接起來
if (apiUrl.contains("?")) {
apiUrl = apiUrl + "&" + urls[1];
} else {
apiUrl = apiUrl + "?" + urls[1];
}
}
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Cookie", cookieStr);
CloseableHttpResponse response = null;
HttpEntity entity = null;
String result = null;
try {
response = httpClient.execute(post);
StatusLine status = response.getStatusLine();
if ((status != null) && (status.getStatusCode() == 200)) {
entity = response.getEntity();
Header header = response.getFirstHeader(EpushContants.EPUSH_UID);
String uid = header != null ? header.getValue() : "";
handshakeData.getHeaders().put(EpushContants.EPUSH_UID, Lists.newArrayList(uid));
return true;
} else {
logger.warn("[EPush]連接權(quán)限校驗(yàn)時返回錯誤,app={},apiUrl={},status={}", app, apiUrl,
status != null ? status.getStatusCode() : "");
}
} catch (Exception e) {
logger.error("[EPush]連接權(quán)限校驗(yàn)時發(fā)生錯誤,app={},apiUrl={}", app, apiUrl, e);
} finally {
try {
EntityUtils.consume(entity);
if (response != null) {
response.close();
}
} catch (IOException e) {
logger.error("", e);
}
}
logger.warn("[EPush]auth fail, app={},apiUrl={},authResult={}", app, apiUrl, result);
}
}
logger.info("[EPush]auth fail, app={}, cookie={}", app, cookieStr);
return false;
}
</code></pre>
其實(shí)經(jīng)過這三段代碼的對比相信大家也看到了問題的所在。同事在上線新功能的時候?qū)@取httpEntity實(shí)例的代碼刪除了蛇受,導(dǎo)致http連接的entity沒有被consume句葵。上線以后http連接一直沒有釋放。導(dǎo)致后面的連接服務(wù)失敗。所以大家在習(xí)慣代碼的時候需要注意原來代碼的每一項(xiàng)邏輯乍丈。這段代碼剂碴,其實(shí)主要是粗心的認(rèn)為entity沒有在try方法里面使用導(dǎo)致的。我們在修改代碼時候盡可能的擴(kuò)大編程影響范圍轻专。最最起碼得知道這個方法體中的所有處理邏輯忆矛。避免這種代碼級別的故障。