Java高级Rest客户端连接给ssl错误

时间:2020-05-29 07:23:12

标签: java elasticsearch ssl resthighlevelclient

某些时候我遇到以下错误,无法获得搜索结果。

 2020-05-26 16:03:30.207 ERROR [default task-284][SearchMVCAction:148]
 exception in getting response java.io.IOException: Unrecognized SSL
 message, plaintext connection? 
 at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:954)
 at org.elasticsearch.client.RestClient.performRequest(RestClient.java:229)

我的代码如下。

try{
    RestClientBuilder builder = SearchResultsUtil.getRestClientBuilder();
    RestHighLevelClient esClient = new RestHighLevelClient(builder);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.timeout(new TimeValue(600, TimeUnit.SECONDS)); // Request timeout
    sourceBuilder.from(pagenumber);
    sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); //Result set ordering
    BoolQueryBuilder query = new BoolQueryBuilder();
         query.must(QueryBuilders.queryStringQuery("*"+searchWildKeyword+"*").field("content").field("title",10.0f).field("description").lenient(true).escape(true).analyzeWildcard(true).fuzziness(Fuzziness.ZERO).defaultOperator(Operator.OR).boost(1.0f));
    sourceBuilder.query(query);
    SearchRequest searchRequest = new SearchRequest(esIndice);
    searchRequest.source(sourceBuilder);
    SearchResponse searchResponse  = esClient.search(searchRequest, RequestOptions.DEFAULT);
    SearchHits hits = searchResponse.getHits();
    System.out.println("results:"+hits);
    }catch (Exception e) {
        _log.error("Exception Getting Response",e);

    }finally {
        _log.info("inside finally block");
        esClient.close();
    }

我也尝试过flush()但没有运气。

finally {
    _log.info("inside finally block");
    esClient.close();
    esClient.indices().flush(new FlushRequest(esIndice), RequestOptions.DEFAULT);
}

请提供可能的原因,我正在使用ES 9200 porthttps protocol

1 个答案:

答案 0 :(得分:0)

您将不得不重构一下代码。请在下面找到有关RestHighLevelClient的工作示例。

    @Bean
public RestHighLevelClient elasticRestClient () {
    String[] httpHosts = httpHostsProperty.split(";");
    HttpHost[] httpHostsAsArray = new HttpHost[httpHosts.length];
    int index = 0;

    for (String httpHostAsString : httpHosts) {
        HttpHost httpHost = new HttpHost(httpHostAsString.split(":")[0], new Integer(httpHostAsString.split(":")[1]), "http");
        httpHostsAsArray[index++] = httpHost;
    }

    RestClientBuilder restClientBuilder = RestClient.builder(httpHostsAsArray)
            .setRequestConfigCallback(builder -> builder
                    .setConnectTimeout(connectTimeOutInMs)
                    .setSocketTimeout(socketTimeOutInMs)
            );

    return new RestHighLevelClient(restClientBuilder);
}

以及使用RestHighLevelClient的类:

    @Autowired
private RestHighLevelClient restClient;

        IndexResponse indexResponse = restClient.index(indexRequest, RequestOptions.DEFAULT);