Spring Boot 2.0 无法连接到 ElasticSearch 5.5.0 - NoNodeAvailableException

Spring Boot 2.0 can't connect to ElasticSearch 5.5.0 - NoNodeAvailableException

我遵循了很多示例/文档,但无法弄清楚如何使用 spring 数据弹性和 TransportClient 从 Spring Boot 2.0 连接到 ElasticSearch 5.5.0。我在尝试连接到 elasticsearch 时总是得到 NoNodeAvailableException。

我的代码如下:

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.BUILD-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

配置class

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo")
public class EsConfig {

    @Value("${elasticsearch.host}")
    private String EsHost;

    @Value("${elasticsearch.port}")
    private int EsPort;

    @Value("${elasticsearch.clustername}")
    private String EsClusterName;

    @Bean
    public Client client() throws Exception {

        Settings esSettings = Settings.builder()
                .put("cluster.name", EsClusterName)
                .build();

        TransportClient client = new PreBuiltTransportClient(esSettings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), Integer.valueOf(EsPort)));
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchTemplate(client());
    }
}

application.properties

elasticsearch.clustername = elasticsearch
elasticsearch.host = localhost
elasticsearch.port = 9300

当我 运行 应用程序时,我收到一个错误:

failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{ru3fMzoqTQygBpT5C6KXXw}{localhost}{127.0.0.1:9300}]

我的 elasticsearch 运行在 docker 容器中

run -p 9200:9200 -p 9300:9300 --name elasticsearch -d --network mynetwork elasticsearch:5.5.0

我遇到了同样的问题,这就是为我解决的问题:

这是我的第一期:

调试日志:

Parameter 0 of method elasticsearchTemplate in org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration required a single bean, but 2 were found:
    - buildClient: defined by method 'buildClient' in class path resource [nl/dummypackage/config/ElasticsearchConfig.class]
    - elasticsearchClient: defined by method 'elasticsearchClient' in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]

我删除了 ElasticsearchConfig class 和 将此 属性 添加到

application.properties

spring.data.elasticsearch.cluster-nodes=localhost:9300

下一期:缺少身份验证令牌

调试日志:

org.elasticsearch.transport.RemoteTransportException: [elasticsearch-docker-single-node][172.19.0.2:9300][cluster:monitor/nodes/liveness]
Caused by: org.elasticsearch.ElasticsearchSecurityException: missing authentication token for action [cluster:monitor/nodes/liveness]

现在,我通过禁用 xpack.security:

解决了这个问题

elasticsearch.yml

xpack.security.enabled: false

问题出在 docker 容器本身。要从 java 连接到 docker 容器,您需要将 http.host 设置为 0 transport.host 到 0 禁用 xpack 安全和 禁用嗅探 如果其他人在这里挣扎解决方案:

docker network create mynetwork --driver=bridge
docker run -p 9200:9200 -p 9300:9300 -e "http.host=0.0.0.0" -e "transport.host=0.0.0.0" -e "xpack.security.enabled=false" -d --name thesiselasticsearch -d --network mynetwork docker.elastic.co/elasticsearch/elasticsearch:5.5.0
docker run -d --network mynetwork -e ELASTICSEARCH_URL=http://thesiselasticsearch:9200 --name thesiskibana -p 5601:5601 kibana:5.5.0

和配置class:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo")
public class EsConfig {

    @Value("${elasticsearch.host}")
    private String EsHost;

    @Value("${elasticsearch.port}")
    private int EsPort;

    @Value("${elasticsearch.clustername}")
    private String EsClusterName;

    @Bean
    public Client client() throws Exception {

        Settings esSettings = Settings.builder()
                .put("cluster.name", "docker-cluster")
                .put("client.transport.sniff", false)
                .build();


        TransportClient client = new PreBuiltTransportClient(esSettings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchTemplate(client());
    }
}