在30毫秒内连接客户端以发送帖子请求的所有方法是什么?生产者和使用者都在同一AWS区域和同一VPC中。
我编写了一个示例客户端,但是它花费了30毫秒以上的时间。同一vpc中的客户端向服务器请求私有IP。
客户端在2个实例上运行。
服务器逻辑占用的时间少于10ms。这将只连接redis并发送响应。
如果消费者和生产者都在同一VPC和同一AWS区域上,请告知在30毫秒内实现连接的可能方法是什么。
HttpPost httpPost = new HttpPost(
"elb/endpoint");
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json");
String stringRequest = request.toString();
StringEntity stringEntity = new StringEntity(stringRequest, Charsets.UTF_8);
httpPost.setConfig(getRequestConfig());
httpPost.setEntity(stringEntity);
logger.info("Client sent time");
long start = System.currentTimeMillis();
try (CloseableHttpClient client = getCloseableClient();
CloseableHttpResponse httpResponse = client.execute(httpPost)) {
logger.info("Client received time");
long endTime = System.currentTimeMillis();
HttpEntity httpEntity = httpResponse.getEntity();
responses = EntityUtils.toString(httpEntity, "UTF-8");
long duration = endTime - start;
report.duration = duration;
report.scriptTime = entity.processTime;
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
private RequestConfig getRequestConfig() {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
return requestConfig;
}
private CloseableHttpClient getCloseableClient() {
CloseableHttpClient httpClient = null;
long startTime = System.currentTimeMillis();
try {
initSSLSocketFactory();
Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory).build();
HttpClientConnectionManager connMgr = pooling
? new PoolingHttpClientConnectionManager(r)
: new BasicHttpClientConnectionManager(r);
HttpClientBuilder builder = HttpClients.custom();
builder.setConnectionManager(connMgr);
httpClient = builder.build();
long endTime = System.currentTimeMillis();
long getCloseableClientTime = endTime - startTime;
logger.info("getCloseableClientTime method {}", getCloseableClientTime);
return httpClient;
} catch (Exception ex) {
}
return httpClient;
}
private SSLConnectionSocketFactory initSSLSocketFactory() {
if (sslSocketFactory == null) {
try {
TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStrategy);
SSLContext sslContext = sslContextBuilder.build();
sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
NoopHostnameVerifier.INSTANCE);
} catch (Exception e) {
String message = "Unable to create the SSL socket factory.";
}
}
return sslSocketFactory;
}