gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:57:02 25 4
gpt4 key购买 nike

我跟踪了很多示例/文档,但无法弄清楚如何使用spring数据elastic和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>

配置类
@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

最佳答案

问题出在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

和配置类:
@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());
}
}

关于spring-boot - Spring Boot 2.0无法连接到ElasticSearch 5.5.0-NoNodeAvailableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49712532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com