2017-07-28 2 views
0

Ich benutze die Rest-API auf niedriger Ebene, um auf Elastic Search in AWS (AWS Service) zuzugreifen. AWS stellt die HTTPS-URL bereit. Der folgende Code befindet sich in der SpringBoot-Konfigurationsklasse mit dem Namen com.example.configurations.EsConfig.SSL/HTTPS-Verbindung mit Low-Level-Rest-API?

@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; 
} 

Für esHost ich die AWS Elasticsearch Endpunkt-URL wie localhost nur am bereitstellt.

Für esPort sein 443 im Fall von HTTPS.

Im Folgenden sind die Fehlermeldungen:

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 

Ich bin nicht in der Lage RestClient über SSL-Verbindung zu erhalten. Bitte schlagen Sie mir vor, was benötigt wird, um einen RestClient über eine SSL-Verbindung zu erhalten. Danke

Antwort

0

Ich löste es übergeben "https" beim Erstellen von HttpPost Objekt als drittes Argument.

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

Jetzt funktioniert es gut.