美文网首页
spring data ElasticSearch x-pack

spring data ElasticSearch x-pack

作者: 五洋捉鳖zz | 来源:发表于2019-04-30 13:24 被阅读0次
  • 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>
  • ElasticConfig.java

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;
    }
}
  • application.yml
elasticsearch:
  clusterName: clusterName
  xPackSecurity: username:passwd
  clusterNodes: xxx.xxx.xxx.xxx
  port: 9300

相关文章

网友评论

      本文标题:spring data ElasticSearch x-pack

      本文链接:https://www.haomeiwen.com/subject/xzomnqtx.html