【问题标题】:SSL / HTTPS Connection with Low Level Rest API?SSL / HTTPS 与低级 Rest API 的连接?
【发布时间】:2018-01-04 22:08:28
【问题描述】:

我正在使用低级别的 rest API 来访问 AWS(AWS 服务)中的 Elastic Search。 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 需要什么。 谢谢

【问题讨论】:

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


    【解决方案1】:

    我在创建 HttpPost 对象作为第三个参数时通过“https”解决了它。

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

    现在它工作正常。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2017-09-10
      • 2017-12-10
      • 2017-01-01
      • 2021-02-11
      • 2012-10-13
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多