gpt4 book ai didi

java - 如何在单个 Docker 容器中提供多模块 spring boot 应用程序?

转载 作者:行者123 更新时间:2023-12-05 06:54:51 26 4
gpt4 key购买 nike

我有一个类似于此项目结构的多模块 spring boot 应用程序:

hello-world

├── hello-world-core
│ └── HelloWorldApplication.java
│ └── pom.xml <--- hello-world-core POM
│ └── src <--- hello-world-core src
│ └── target <--- hello-world-core target with jar
├── hello-world-users
│ └── pom.xml <--- hello-world-users POM
│ └── src <--- hello-world-users src
│ └── target <--- hello-world-users target with jar
├── hello-world-website
│ └── pom.xml <--- hello-world-website POM
│ └── src <--- hello-world-website src
│ └── target <--- hello-world-website target with jar
└── pom.xml

现在我想在单个 Docker 容器中使用像这样的图像来提供应用程序:

# jdk 11 image
FROM maven:3.6-jdk-11 as builder

# Copy local code to the container image.
WORKDIR /app

# copy the project files
COPY pom.xml .

# copy your other files
COPY src ./src

# Build a release artifact.
RUN mvn package -DskipTests

# Use AdoptOpenJDK for base image.
FROM adoptopenjdk/openjdk11:alpine-slim

# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/hello-world*.jar /hello-world.jar

EXPOSE 8080

# Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/hello-world.jar"]

为了使用此 Dockerfile 为其提供服务,我需要从该项目创建一个单个 jar

现在我想知道:

  1. 有没有更好的方法在单个 Docker 容器中提供此类应用程序(不使用 docker-compose)?
  2. 如何使用 maven 从这个结构中创建一个 jar?

最佳答案

如果你想在一个容器中做到这一点,有一些方法可以做到这一点,

  1. 您可以像已经建立的那样分别构建 3 个不同的 spring 模块,并在同一镜像的不同端口中运行这 3 个 jar。因此,为此您必须使用 maven 更改绑定(bind)在 jar 中的端口,因为我们试图在 1 个容器中运行所有 3 个进程,因此如果您尝试这样做,使用 8080(默认)会导致错误。

server.port=9090

像上面那样在 application.properties 中有帮助。因此,之后您将必须在容器内手动运行它们或将它们全部附加到容器启动脚本。

例如:

RUN java -jar hello-world-core.jar & java -jar hello-world-users.jar

因此,当您执行此操作时,不同的 jar 将服务于您在 pom 文件中分别提到的不同端口。因此,为了从外部访问它们,您还必须公开这些端口。

  1. 如果您真的希望将它们放在一个容器中,为什么首先要将它们分开?并有 3 个单独的 tomcats 执行?我的意思是所有 3 个应用程序内部都有单独的 tomcat,这就是我们努力将其合并为 1 的原因。那么为什么我们不转义那些 tomcat 并构建 war 文件,然后在容器中安装一个 tomcat 并将所有 war 放入 webapps 文件夹?这意味着您必须将 tomcat 放到 docker 上并使用它,这样所有应用程序都将使用相同的 tomcat,并且可以通过 8080 访问。

  2. 我相信微服务的观点,将它们分开很好,是的,这样做有利也有弊。我也同意@khmarbaise 在他的评论中提出的观点,跳过测试不是一个好主意。 :D

关于java - 如何在单个 Docker 容器中提供多模块 spring boot 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65446919/

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