2016-07-27
- 使用nginx中转时,如后端服务时间过长,导致前段页面返回http的504 gateway timeout错误。解决办法:nginx在转发时增加timeout。
Example:
location /weixinYdt {
proxy_pass http://weixinYdt_server;
proxy_connect_timeout 500s;
proxy_read_timeout 500s;
proxy_send_timeout 500s;
}
- com.ning.http.client.AsyncHttpClient类的使用
- 可通过AsyncHttpClientConfig设置http client相关参数
- AsyncHttpClient.BoundRequestBuilder设置http请求相关信息
Example:
public static String postJson(String url, Map<String, String> params,String requestBody) throws IOException, ExecutionException, InterruptedException {
//设置http请求相关参数
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
configBuilder.setRequestTimeout(240 * 1000);//设置timeout时间
AsyncHttpClient http = new AsyncHttpClient(configBuilder.build());
AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
builder.setBodyEncoding(DEFAULT_CHARSET);
builder.setBody(requestBody);
builder.setHeader("Content-Type", MediaType.APPLICATION_JSON.toString());
builder.setHeader("Accept",MediaType.APPLICATION_JSON.toString());
builder.setRequestTimeout(240 * 1000);
if (params != null && !params.isEmpty()) {
Set<String> keys = params.keySet();
for (String key : keys) {
builder.addQueryParam(key, params.get(key));
}
}
Future<Response> f = builder.execute();
String body = f.get().getResponseBody(DEFAULT_CHARSET);
http.close();
return body;
}
2016-07-20
- 在出现高并发时,如果数据库是有主键约束,那么需要在发生约束异常时进行处理。
网友评论