多个微服务在一个Docker容器中

huangapple go评论66阅读模式
英文:

Multiple microservices in one docker container

问题

I understand your request. Here's the translated content:

我有一个名为 'RestAPI' 的 Spring Boot 应用程序,它充当我的用户界面的后端服务。
RestAPI 变得有点复杂... 所以我需要将它拆分为微服务。

RestAPI 具有以下功能,我希望将其转换为微服务:

  1. 服务 1 - Protocol 1 的基本操作服务。
  2. 服务 2 - Protocol 2 的基本操作服务。
  3. 服务 3 - Protocol 1 的复杂操作服务。
  4. 服务 4 - Protocol 2 的复杂操作服务。

注意 - 只是一个想法... 还没有确定是否会按照上述步骤进行。但是需要最终确定的微服务。

主要问题是...
目前,我正在使用一个 Docker 容器来运行 RestAPI 应用程序。
那么,如果我将 RestAPI 拆分为微服务,将所有微服务运行在一个 Docker 容器中是一个好方法吗?

如果是的话... 我如何使用一个 Docker 文件或多个 Docker 文件来实现?

如果不是的话... 为什么这样做不可行?

英文:

I have a spring-boot application named 'RestAPI' which acts as backend service for my UI.
The RestAPI has become a bit complex... so I need to break it down in microservices.

The RestAPI has following functionalities I would like to convert to microservices:

  1. Service 1 - Service for basic operations of Protocol 1.
  2. Service 2 - Service for basic operations of Protocol 2.
  3. Service 3 - Service for complex operations of Protocol 1.
  4. Service 4 - Service for complex operations of Protocol 2.

Note - Just a thought... not finalized if I will follow the above mentioned steps.
But need microservices that is final.

The main question is...
Currently I am using 1 docker container for the RestAPI application.
So.. if I break the RestAPI into microservices will it be a good approach to run all the microservices in one docker container ?

If yes.. how can I achieve it using 1 Docker file or multiple Docker files ?

If no.. why is it not feasible ?

答案1

得分: 1

微服务提供了几个优势,如灵活性、可伸缩性、持续部署、可维护性、可测试性、独立部署能力、技术灵活性和高可靠性。它们促进了敏捷的工作方式,使团队能够尝试新功能,并允许快速轻松地独立部署各个功能。此外,微服务提供了技术灵活性、高可靠性,增加了团队的自主权。

然而,如果所有微服务运行在同一个容器中,它们将使用相同的资源,可能导致类似单块应用的情况。为了避免这种情况,每个微服务必须拥有自己的CPU、内存资源、数据库、存储等。虽然可以在同一个容器中运行多个微服务,但不建议这样做。

为了独立管理每个微服务,每个微服务都应该有自己的Docker容器,可以使用Docker Compose或Kubernetes进行编排。为了实现这一点,必须为每个微服务创建一个Dockerfile。这些服务也可以部署在AWS Lambda、GCP Cloud Run、App Engine和其他平台上。

有关更多有用的信息,请参考以下链接:

Source: https://mintymint.net/blog/tech/microservices-vs-monolithic-software-architecture-guide/

英文:

Microservices offer several advantages, such as agility, flexible scaling, continuous deployment, maintainability, testability, independent deployability, technology flexibility, and high reliability. They promote agile ways of working, enabling teams to experiment with new features and allowing for fast and easy independent deployment of individual features. Additionally, microservices provide technology flexibility, high reliability, and increase team autonomy.

However, if all microservices run in the same container, they will use the same resources, which can result in something like a monolithic application. To avoid this, each microservice must have its own CPU, memory resources, database, storage, etc. While it is possible to run multiple microservices in the same container, it is not recommended.

To manage each microservice independently, each one should have its own Docker container, which can be orchestrated using Docker Compose or Kubernetes. To achieve this, a Dockerfile must be created for each microservice. The services can also be deployed on AWS Lambda, GCP Cloud Run, App Engine, and other platforms.

多个微服务在一个Docker容器中
Source: https://mintymint.net/blog/tech/microservices-vs-monolithic-software-architecture-guide/

Here is some more useful information:

答案2

得分: 0

除了优势之外,如果您对在一个容器中运行所有服务感兴趣,请注意:不建议在生产环境中使用。

COPY start.sh /apps/start.sh
RUN chmod 755 /apps/start.sh
COPY path/target/service1.jar /apps/project1/service1.jar
COPY path/target/service2.jar /apps/project2/service2.jar
COPY path/target/service3.jar /apps/project3/service3.jar
COPY path/target/service4.jar /apps/project4/service4.jar
COPY path/target/service5.jar /apps/project5/service5.jar


EXPOSE 8080
CMD ["/bin/bash", "/apps/start.sh"]

Start.sh

nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project1/service1.jar > service1.log &
nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project2/service2.jar > service2.log &
nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project3/service3.jar > service3.log &
nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project4/service4.jar > service4.log &
nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project5/service5.jar > service5.log &

确保运行httpd代理将调用定向到正确的微服务。
英文:

besides the Advantage, if you are curious to run all your Service In One Container.
FYI:- Not Advisable to use in PROD

COPY start.sh /apps/start.sh
RUN  chmod 755 /apps/start.sh
COPY path/target/service1.jar /apps/project1/service1.jar
COPY path/target/service2.jar /apps/project2/service2.jar
COPY path/target/service3.jar /apps/project3/service3.jar
COPY path/target/service4.jar /apps/project4/service4.jar
COPY path/target/service5.jar /apps/project5/service5.jar


EXPOSE 8080
CMD ["/bin/bash", "/apps/start.sh"]

Start.sh

nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project1/service1.jar > service1.log &
nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project2/service2.jar > service2.log &
nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project3/service3.jar > service3.log &
nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project4/service4.jar > service4.log &
nohup java -jar -Xms2G -Xmx2G -Dspring.profiles.active=${ENVIRONMENT} /apps/project5/service5.jar > service5.log &

make sure to run httpd proxy to direct the calls to right MS

huangapple
  • 本文由 发表于 2023年5月11日 13:59:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定