Elasticsearch JAVA API ---Springboot2.x中配置使用(一)


Elasticsearch JAVA API ---Springboot2.x中配置使用(一)


Elasticsearch 会在7.0之后的版本废弃TransportClient,在8.0之后的版本移除TransportClient (文档)。因此,使用RestClient来进行相关的操作。

We plan on deprecating the TransportClient in Elasticsearch 7.0 and removing it completely in 8.0. Instead, you should be using the Java High Level REST Client, which executes HTTP requests rather than serialized Java requests. The migration guidedescribes all the steps needed to migrate.

取而代之的是High Level REST Client。

后续的JAVA API使用我们都是使用的High Level REST Client。

完整代码:https://github.com/qfjiaoyan/elasticsearch-examples

maven配置

新建SpringBoot2.0.3的WEB工程,在MAVEN的pom.xml文件中加入如下依赖

pom文件:

<code> <dependencies>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-web/<artifactid>
/<dependency>
<dependency>
<groupid>org.elasticsearch/<groupid>
<artifactid>elasticsearch/<artifactid>
<version>6.5.0/<version>
/<dependency>
<dependency>
<groupid>org.elasticsearch.client/<groupid>
<artifactid>elasticsearch-rest-high-level-client/<artifactid>
<version>6.5.0/<version>
/<dependency>
<dependency>
<groupid>com.alibaba/<groupid>
<artifactid>fastjson/<artifactid>
<version>1.2.56/<version>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-test/<artifactid>
<scope>test/<scope>
/<dependency>
/<dependencies>/<code>

配置文件配置

<code>#日志
logging.path=D:/logs/cache
#es 配置信息
spring.data.elasticsearch.host=192.168.157.140
spring.data.elasticsearch.port=9200
spring.data.elasticsearch.connectTimeOut=1000
spring.data.elasticsearch.socketTimeOut=30000
spring.data.elasticsearch.connectionRequestTimeOut=500
spring.data.elasticsearch.maxConnectNum=100
spring.data.elasticsearch.maxConnectPerRoute=100/<code>

elasticsearch配置类

<code>@Configuration
public class ElasticsearchConfig {
private final static Logger log = LoggerFactory.getLogger(ElasticsearchConfig.class);
private static final String SCHEMA = "http"; // 使用的协议
private ArrayList<httphost> hostList;
@Value("${spring.data.elasticsearch.host}")
private String hosts; // 集群地址,多个用,隔开
@Value("${spring.data.elasticsearch.port}")
private int port; // 集群地址,多个用,隔开
@Value("${spring.data.elasticsearch.connectTimeOut}")
private int connectTimeOut = 1000; // 连接超时时间
@Value("${spring.data.elasticsearch.socketTimeOut}")
private int socketTimeOut = 30000; // 连接超时时间
@Value("${spring.data.elasticsearch.connectionRequestTimeOut}")
private int connectionRequestTimeOut = 500; // 获取连接的超时时间
@Value("${spring.data.elasticsearch.maxConnectNum}")
private int maxConnectNum = 100; // 最大连接数
@Value("${spring.data.elasticsearch.maxConnectPerRoute}")
private int maxConnectPerRoute = 100; // 最大路由连接数
private RestClientBuilder builder;

@Bean
@ConditionalOnMissingBean(RestHighLevelClient.class)
public RestHighLevelClient client() {
hostList = new ArrayList<>();
String[] hostStrs = hosts.split(",");
for (String host : hostStrs) {
String server = host;
hostList.add(new HttpHost(host, port , SCHEMA));
}
builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
setConnectTimeOutConfig();
setMutiConnectConfig();
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}

// 异步httpclient的连接延时配置
public void setConnectTimeOutConfig() {
builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
}
});
}

// 异步httpclient的连接数配置
public void setMutiConnectConfig() {
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
return httpClientBuilder;
}
});
}

}/<httphost>/<code>

完整代码

https://github.com/qfjiaoyan/elasticsearch-examples


分享到:


相關文章: