使用低级别Rest API进行SSL / HTTPS连接?

时间:2017-07-28 18:35:39

标签: amazon-web-services ssl elasticsearch spring-boot https

我正在使用低级别的rest API访问AWS中的Elastic Search(AWS服务)。 AWS提供HTTPS网址。以下代码位于名为com.example.configurations.EsConfig的SpringBoot配置类中。

@Bean
public RestClient restClient() throws Exception {
    RestClient restClient = RestClient.builder(new HttpHost(esHost, esPort))
            .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    HttpAsyncClientBuilder httpAsyncClientBuilder = null;
                    try {
                        httpAsyncClientBuilder = httpClientBuilder.setSSLContext(SSLContext.getDefault());
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    }
                    return httpAsyncClientBuilder;
                }
            })
            .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(5000)
                    .setSocketTimeout(60000))
            .setMaxRetryTimeoutMillis(60000)
            .build();
    return restClient;
}

对于esHost,我只提供AWS Elasticsearch端点URL,例如localhost。

对于esPort,如果是HTTPS,则为443。

以下是错误消息:

2017-07-31 09:22:43.001  INFO 6239 --- [/O dispatcher 1] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.018  INFO 6239 --- [/O dispatcher 2] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.037  INFO 6239 --- [/O dispatcher 3] com.example.configurations.EsConfig      : org.apache.http.ConnectionClosedException: Connection closed
2017-07-31 09:22:43.043  INFO 6239 --- [/O dispatcher 4] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 6] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 5] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 7] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.077  INFO 6239 --- [/O dispatcher 8] com.example.configurations.EsConfig      : org.apache.http.ConnectionClosedException: Connection closed
2017-07-31 09:22:43.099  INFO 6239 --- [/O dispatcher 2] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer

我无法通过SSL连接获取RestClient。请告诉我通过SSL连接获取RestClient所需的内容。 感谢

1 个答案:

答案 0 :(得分:1)

我解决了传递" https"同时创建HttpPost对象作为第三个参数。

RestClient restClient = RestClient.builder(new HttpHost(esHost, esPort, "https"))
            .setHttpClientConfigCallback(httpClientBuilder -> {
                HttpAsyncClientBuilder httpAsyncClientBuilder = httpClientBuilder.setSSLContext(sslcontext);
                return httpAsyncClientBuilder;
            })
            .build();

现在工作正常。