spring boot 整合 elasticsearch 5.x

NO IMAGE

目錄

專案環境:

  • jdk 1.8
  • spring boot 1.5.1

由於spring boot 目前SpringBoot 1.5.1.RELEASE和Spring Data Elasticsearch 2.10.RELEASE僅支援ElasticSearch 2.4.0。
他們不支援最新版本的ElasticSearch 5.x版本。

Spring Boot Version (x)Spring Data Elasticsearch Version (y)Elasticsearch Version (z)
x <= 1.3.5y <= 1.3.4z <= 1.7.2*
x >= 1.4.x2.0.0 <=y < 5.0.0**2.0.0 <= z < 5.0.0**

mavan 配置 pom.xml

   <!-- 版本控制  -->
<properties>
<log4j-api.version>2.8.2</log4j-api.version>
<log4j-core.version>2.8.2</log4j-core.version>
<elasticsearch.version>5.4.1</elasticsearch.version>
<transport.version>5.4.1</transport.version>
</properties>
<!-- elasticsearch 5.x 依賴 --> 
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-core.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${transport.version}</version>
</dependency>
</dependencies> 
: 特注
 ElasticSearch 5.x 根據官網配置maven 依賴, 由於 5.0x的 jar 內部使用的 apache log4日誌。
所以要配置額外的依賴支援 org.apache.logging.log4j。

application-dev.yml

spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300

配置 java Client連線池


package com.****.****.config;
import com.****.****.common.utils.StringUtils;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
*TransportClientFactoryBean
*
* @author 
*/
@Configuration
public class ElasticsearchConfiguration implements FactoryBean<TransportClient>, InitializingBean, DisposableBean {
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchConfiguration.class);
//由於專案從2.2.4配置的升級到 5.4.1版本 原配置檔案不想動還是指定原來配置引數
@Value("${spring.data.elasticsearch.cluster-nodes}")
private String clusterNodes ;
@Value("${spring.data.elasticsearch.cluster-name}")
private String clusterName;
private TransportClient client;
@Override
public void destroy() throws Exception {
try {
logger.info("Closing elasticSearch client");
if (client != null) {
client.close();
}
} catch (final Exception e) {
logger.error("Error closing ElasticSearch client: ", e);
}
}
@Override
public TransportClient getObject() throws Exception {
return client;
}
@Override
public Class<TransportClient> getObjectType() {
return TransportClient.class;
}
@Override
public boolean isSingleton() {
return false;
}
@Override
public void afterPropertiesSet() throws Exception {
buildClient();
}
protected void buildClient()  {
try {
PreBuiltTransportClient  preBuiltTransportClient = new PreBuiltTransportClient(settings());
if (!"".equals(clusterNodes)){
for (String nodes:clusterNodes.split(",")) {
String InetSocket [] = nodes.split(":");
String  Address = InetSocket[0];
Integer  port = Integer.valueOf(InetSocket[1]);
preBuiltTransportClient.addTransportAddress(new
InetSocketTransportAddress(InetAddress.getByName(Address),port ));
}
client = preBuiltTransportClient;
}
} catch (UnknownHostException e) {
logger.error(e.getMessage());
}
}
/**
* 初始化預設的client
*/
private Settings settings(){
Settings settings = Settings.builder()
.put("cluster.name",clusterName) 
.put("client.transport.sniff",true)
build();
client = new PreBuiltTransportClient(settings);
return settings;
}
}

參考文獻

[1] : https://www.mkyong.com/spring-boot/spring-boot-spring-data-elasticsearch-example/