Java高级REST客户端连接出现SSL错误。

huangapple 未分类评论44阅读模式
英文:

java highlevel rest client connection is giving ssl error

问题

我有时会遇到以下错误,无法获取搜索结果。

 2020-05-26 16:03:30.207 错误 [默认任务-284][SearchMVCAction:148]
 在获取响应时出现异常 java.io.IOException: 无法识别的 SSL
 消息,是否为纯文本连接?
 位于 org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:954)
 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)); // 请求超时
	sourceBuilder.from(pagenumber);
	sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); // 结果集排序
	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 端口https 协议

英文:

I am getting bellow error some time and not able to get search results.

 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)

my code is as below.

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();
	}

also i tried flush() but no luck.

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

kindly help what might be the cause and i am using ES 9200 port and https protocol.

答案1

得分: 0

你需要稍微重构你的代码。以下是关于RestHighLevelClient的工作示例。

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

    for (String httpHostAsString : httpHosts) {
        String[] hostInfo = httpHostAsString.split(":");
        HttpHost httpHost = new HttpHost(hostInfo[0], Integer.parseInt(hostInfo[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);
英文:

You will have to refactor your code a bit. Please find working example with regards to RestHighLevelClient below.

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

and the class using the RestHighLevelClient:

    @Autowired
private RestHighLevelClient restClient;

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

huangapple
  • 本文由 发表于 2020年5月29日 15:23:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/62080740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定