- spring boot Version:2.0.4.RELEASE
- ElasticSearch:5.6.6
- pom.xml
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>5.6.6</version>
</dependency>
...// 部分jar在elasticsearch自己的仓库
<repositories>
<!-- add the elasticsearch repo for transport-client lib-->
<repository>
<id>elasticsearch-releases</id>
<url>https://artifacts.elastic.co/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Configuration
public class ElasticSearchConfig {
@Value("${elasticsearch.clusterName}")
private String clusterName;
@Value("${elasticsearch.xPackSecurity}")
private String xPackSecurity;
@Value("${elasticsearch.clusterNodes}")
private String clusterNodes;
@Value("${elasticsearch.port}")
private Integer port;
@Bean
public TransportClient transportClient() throws UnknownHostException {
TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
.put("cluster.name", this.clusterName)
.put("xpack.security.user", this.xPackSecurity)
.build());
String[] ips = this.clusterNodes.split(",");
for(String ip : ips) {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));
}
return client;
}
}
elasticsearch:
clusterName: clusterName
xPackSecurity: username:passwd
clusterNodes: xxx.xxx.xxx.xxx
port: 9300
网友评论