【发布时间】:2022-08-21 11:12:49
【问题描述】:
我正在使用 Elasticsearch 客户端版本 7.15.2
这是我的Pom.xml
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.15.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.15.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.2</version>
</dependency>
我们使用 Elasticsearch 作为服务,其他公司为我们提供了一个 IP/端口来连接它。
这是我对RestHighLevelClient 的配置
@Bean
public RestHighLevelClient getElasticSearchClient() {
String encoding = Base64.getEncoder().encodeToString((username + \":\" + password).getBytes());
Header[] headers = {
new BasicHeader(HttpHeaders.CONTENT_TYPE, \"application/json;charset=utf-8\"),
new BasicHeader(HttpHeaders.ACCEPT, \"application/json\"),
new BasicHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, \"*\"),
new BasicHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, \"GET, PUT, POST, DELETE, OPTIONS\"),
new BasicHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS,
\"X-Requested-With,X-Auth-Token,Content-Type, Content-Length, Authorization\"),
new BasicHeader(HttpHeaders.AUTHORIZATION, \"basic \" + encoding)};
return new RestHighLevelClient(RestClient
.builder(new HttpHost(elasticSearchIp, elasticSearchPort))
.setHttpClientConfigCallback(configurer -> {
configurer.setMaxConnTotal(maxConnection);
configurer.setMaxConnPerRoute(Math.max(30, maxConnection / 10));
configurer.setKeepAliveStrategy((httpResponse, httpContext) -> keepAlive * 60 * 1000);
configurer.setDefaultIOReactorConfig(
IOReactorConfig.custom().setIoThreadCount(Math.min(ioThreadCount, 15)).build());
return configurer;
})
.setRequestConfigCallback(configurer -> {
configurer.setSocketTimeout(socketTimeout * 60 * 1000);
configurer.setConnectTimeout(connectionTimeout * 60 * 1000);
configurer.setConnectionRequestTimeout(0);
configurer.setContentCompressionEnabled(true);
return configurer;
})
.setCompressionEnabled(true)
.setDefaultHeaders(headers));
}
我们对此 Elasticsearch 运行大量、大量的自动查询,有时我会在日志中看到此 Elasticsearch 的唯一 Ip/端口被列入黑名单
[DEBUG]-2022/06/19 11:28:01-[http-nio-6060-exec-14]-RestClient.onFailure(514) - added [[host=http://xx.xx.xx.xx:3500]] to blacklist
[DEBUG]-2022/06/19 11:28:01-[http-nio-6060-exec-7]-RestClient.onFailure(521) - updated [[host=http://xx.xx.xx.xx:3500]] already in blacklist
- 如何防止这种行为(我的意思是将 IP/端口列入黑名单)?
- 我的配置对于繁重、大量的自动查询是否方便?
标签: java elasticsearch apache-httpclient-5.x