gpt4 book ai didi

Springboot 和 Mongo 容器未连接

转载 作者:行者123 更新时间:2023-12-03 15:59:09 24 4
gpt4 key购买 nike

我正在使用 docker compose 在本地计算机上运行 2 个容器。第一个是 mongo 容器,第二个是 Spring Boot 应用程序。

这是我的 Spring Boot 的 Dockerfile

VOLUME /tmp
ADD target/app.jar app.jar
EXPOSE 8080
RUN bash -c "touch /app.jar"
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongo/test", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

这是我的 Spring Boot 的应用程序属性

# mongodb configuration0
dockerspring.data.mongodb.uri= mongodb://mongo:27000/test

这是我的 docker-compose.yml

version: '3.1'

services:

level-2-springboot:
image: level-2:latest
restart: always
container_name: level_2_microservice
ports:
- 8081:8080
working_dir: /tmp
depends_on:
- mongo

mongo:
image: mongo
container_name: mongo
ports:
- 27000:27017
restart: always

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yash</groupId>
<artifactId>spring-sync-ms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-sync</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>app</finalName>
</build>

</project>

我执行此操作来为我的 Spring Boot 应用程序创建图像

docker build -t level-2 。

看到我的图像后,我执行docker-compose up,它们工作得很好,没有错误

enter image description here

这是正在运行的容器

enter image description here

但是当我向 localhost:8081/api/employees/localhost:8080/api/employees/ 发出 POST 或 GET 请求时,我会收到此消息来自 postman

Could not get any response

下面是我的项目结构

enter image description here

如果我在 eclipse 和 mongo 上手动运行应用程序并执行 POST/GET 请求,我会得到 201 OK,但是当我在容器中使用 docker 执行此操作时,它们不会相互通信。有谁知道这是什么原因造成的吗?

最佳答案

我查阅了互联网,我想我已经得到了你所需要的。

The question that follows is “how does our service container talk to the Mongo container?” For that, we get into container linking. When you run a container, you can pass an optional –link parameter, specifying the running container name that the new container needs to be able to communicate with.

So with our command

docker run -P -d --name employee --link mongodb microservicedemo/employee 

we fire up our new container image, exposing its ports (-P) in the background (-d), naming it employee (–name), and telling it to link to the container named “mongodb” (–link). Linking these does the following:

Creates a hosts file entry in the employee container pointing at the running location of the MongoDB container Inserts a number of environment variables in the employee container to assist with any other programmatic access needed.

To see them run: docker exec employee bash -c 'env |grep MONGODB' Allows containers to communicate directly over ports exposed, so there is no need to worry about the hose machine part mapping.

参见: https://www.3pillarglobal.com/insights/building-a-microservice-architecture-with-spring-boot-and-docker-part-iii

https://www.3pillarglobal.com/insights/building-a-microservice-architecture-with-spring-boot-and-docker-part-ii

关于Springboot 和 Mongo 容器未连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61376964/

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