Commit fb433408 by 姚冰

[chg] 添加es配置参数

1 parent 1ea49831
package com.viontech.match.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomElasticsearchProperties {
/**
* Comma-separated list of the Elasticsearch instances to use.
*/
private List<String> uris = new ArrayList<>(Collections.singletonList("http://localhost:9200"));
/**
* Username for authentication with Elasticsearch.
*/
private String username;
/**
* Password for authentication with Elasticsearch.
*/
private String password;
/**
* Connection timeout used when communicating with Elasticsearch.
*/
private int connectionTimeout = 1;
/**
* Socket timeout used when communicating with Elasticsearch.
*/
private int socketTimeout = 30;
/**
* Prefix added to the path of every request sent to Elasticsearch.
*/
private String pathPrefix;
private boolean customized = false;
private Integer ioThreadCount;
private Integer maxConnTotal;
private Integer maxConnPerRoute;
public List<String> getUris() {
return this.uris;
}
public void setUris(List<String> uris) {
customized = true;
this.uris = uris;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
customized = true;
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
customized = true;
this.password = password;
}
public int getConnectionTimeout() {
return this.connectionTimeout;
}
public void setConnectionTimeout(int connectionTimeout) {
customized = true;
this.connectionTimeout = connectionTimeout;
}
public int getSocketTimeout() {
return this.socketTimeout;
}
public void setSocketTimeout(int socketTimeout) {
customized = true;
this.socketTimeout = socketTimeout;
}
public String getPathPrefix() {
return this.pathPrefix;
}
public void setPathPrefix(String pathPrefix) {
customized = true;
this.pathPrefix = pathPrefix;
}
public boolean isCustomized() {
return customized;
}
public Integer getIoThreadCount() {
return ioThreadCount;
}
public void setIoThreadCount(Integer ioThreadCount) {
this.ioThreadCount = ioThreadCount;
}
public Integer getMaxConnTotal() {
return maxConnTotal;
}
public void setMaxConnTotal(Integer maxConnTotal) {
this.maxConnTotal = maxConnTotal;
}
public Integer getMaxConnPerRoute() {
return maxConnPerRoute;
}
public void setMaxConnPerRoute(Integer maxConnPerRoute) {
this.maxConnPerRoute = maxConnPerRoute;
}
}
package com.viontech.match.config;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.reactor.IOReactorException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class ElasticsearchConfiguration {
@Bean
@ConfigurationProperties(prefix = "spring.elasticsearch.rest")
public CustomElasticsearchProperties Properties() {
return new CustomElasticsearchProperties();
}
@Bean
public RestHighLevelClient createClient(CustomElasticsearchProperties properties) {
HttpHost[] esHosts = properties.getUris().stream().filter(StringUtils::hasLength).map(HttpHost::create).toArray(HttpHost[]::new);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if (properties.getUsername() != null) {
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(properties.getUsername(), properties.getPassword()));
}
final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(properties.getIoThreadCount()).setConnectTimeout(10).setRcvBufSize(5).setSoKeepAlive(true).build();
PoolingNHttpClientConnectionManager connectionManager = null;
try {
connectionManager = new PoolingNHttpClientConnectionManager(new
DefaultConnectingIOReactor(ioReactorConfig));
// 设置最大连接数
connectionManager.setMaxTotal(100); // 整个连接池的最大连接数
connectionManager.setDefaultMaxPerRoute(20);
} catch (IOReactorException e) {
throw new RuntimeException(e);
}
PoolingNHttpClientConnectionManager finalConnectionManager = connectionManager;
RestClientBuilder builder = RestClient.builder(esHosts).setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(properties.getConnectionTimeout());
requestConfigBuilder.setSocketTimeout(properties.getSocketTimeout());
requestConfigBuilder.setConnectionRequestTimeout(-1);
return requestConfigBuilder;
}).setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.disableAuthCaching();
httpClientBuilder.setMaxConnTotal(properties.getMaxConnTotal());
httpClientBuilder.setMaxConnPerRoute(properties.getMaxConnPerRoute());
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(finalConnectionManager);
});
return new RestHighLevelClient(builder);
}
}
......@@ -3,13 +3,21 @@ server.port=12000
spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#es
spring.elasticsearch.rest.uris=http://192.168.1.106:9200
spring.elasticsearch.rest.uris=http://182.92.184.40:9200
spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=viontech
spring.elasticsearch.rest.password=vion2021
spring.elasticsearch.rest.ioThreadCount=20
spring.elasticsearch.rest.maxConnTotal=100
spring.elasticsearch.rest.maxConnPerRoute=20
#\u5206\u7247\u6570\u91CF
vion.index.number_of_shards=1
#\u526F\u672C\u6570\u91CF
vion.index.number_of_replicas=0
vion.index.translog.durability=async
#??????
vion.index.refresh_interval=30s
vion.index.translog.sync_interval=30s
vion.index.merge.scheduler.max_thread_count=1
#delete-pool-after-days\u51E0\u5929\u540E\u5220\u9664\u5929\u6570\u914D\u7F6E
delete.after.days=5
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!