我试图在Docker环境中设置Spring应用程序和Elasticsearch之间的通信,但当我运行程序时,我得到以下警告:
.d.e.r.s.AbstractElasticsearchRepository:无法创建索引:连接被拒绝;嵌套异常为java.lang.RuntimeException:拒绝连接
在浏览器中输入命令时:http://localhost:9200/article/_search?pretty=true,我得到的信息是索引不存在
我尝试在docker-compose和spring应用程序中更改不同的配置,但无法修复错误
但是,当我手动运行程序时(* 没有Docker容器化 *),那么程序就能正常工作
我的docker-compose.yml配置已经尽量简化:
version: '3'
services:
demo:
container_name: demo
image: demo-elastic
build: .
ports:
- "8080:8080"
depends_on:
- es
networks:
- demo-network
es:
image: elasticsearch:7.6.2
environment:
- discovery.type=single-node
ports:
- "9200:9200"
networks:
- demo-network
networks:
demo-network:
driver: bridge
标准Dockerfile
# For Java 11
FROM adoptopenjdk/openjdk11:alpine-jre
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} demo.jar
ENTRYPOINT ["java","-jar","/demo.jar"]
Config.javajava应用中Elasticsearch配置文件
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo.repository")
public class Config extends AbstractElasticsearchConfiguration {
@Value("${elasticsearch.url}")
public String elasticsearchUrl;
@Bean
@Override
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration config = ClientConfiguration.builder()
.connectedTo(elasticsearchUrl)
.build();
return RestClients.create(config).rest();
}
}
应用.属性
elasticsearch.url=localhost:9200
spring.data.elasticsearch.cluster-nodes=localhost:9200
pom.xml文件中的主要依赖。Sping Boot 版本是2.7.5
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies>
1条答案
按热度按时间py49o6xq1#
当程序访问容器中的localhost时,它指向自己的容器。要解决这个问题,请更改Spring配置以使用“es:9200”作为Elasticsearch访问点。在本例中,“es”用作Elasticsearch服务器的主机名。
更新:
在application.properties文件中,您可以设置手动运行的配置。但是,在容器中运行时,可以使用环境变量覆盖这些设置。下面是一个例子:
在application.properties:
然后在Dockerfile或docker-compose文件中,您可以覆盖这些设置:
这样,当应用程序在容器中运行时,Spring应用程序中设置的属性将被环境变量覆盖。
从环境变量绑定配置的规则:https://stackoverflow.com/a/71680394/775640