简体   繁体   English

RabbitMq Spring Boot 部署 docker

[英]RabbitMq Spring Boot deploy docker

I've got a spring boot application with producer and receiver which uses the local rabbitmq to send and receive messages.我有一个带有生产者和接收者的 Spring Boot 应用程序,它使用本地 rabbitmq 来发送和接收消息。 I am trying to deploy both the app and rabbit on docker container and once deployed run the boot app.我正在尝试在 docker 容器上部署应用程序和兔子,并在部署后运行启动应用程序。 Once the app is started it has a producer which implements Spring CommandLineRunner meaning that the app immediately starts sending messages so there must be a rabbitmq broker running prior.应用程序启动后,它有一个生产者,它实现了 Spring CommandLineRunner,这意味着应用程序立即开始发送消息,因此必须先运行一个 rabbitmq 代理。 I am very new to docker.我对 docker 很陌生。 I tried so far:我到目前为止尝试过:

1). 1)。 I created image for the spring boot app as the following我为 spring boot 应用程序创建了如下图像

FROM java:8
EXPOSE 8080
ADD /target/MessagingApp.jar MessagingApp.jar
ENTRYPOINT ["java","-jar","MessagingApp.jar"]

2). 2)。 Then created docker.compose file for rabbit and my newly created image然后为兔子和我新创建的图像创建了 docker.compose 文件

rabbitmq:
  image: rabbitmq:management
  ports:
    - "5672:5672"
    - "15672:15672"
messagingapp:
  image: messagingapp:latest
  ports:
    - "80:8080"
  links:
    - rabbitmq

I then ran docker-compose up and I can see rabbit started to some extend and then spring boot app but fails sending messages with然后我运行了 docker-compose up,我可以看到 rabbit 开始进行一些扩展,然后 spring boot 应用程序但无法发送消息

at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create..

I think my issue is that my spring boot app automatically tries to connect to the localhost rabbit host.我认为我的问题是我的 Spring Boot 应用程序会自动尝试连接到 localhost rabbit 主机。 So how do I make it point to the docker rabbitmq server?那么如何让它指向 docker rabbitmq 服务器呢?

Any help?有什么帮助吗?

Try update part of links for depends_on .尝试更新depends_on部分links Your application probably start before messagingapp .您的应用程序可能在messagingapp之前启动。

Part of documentation for depends_on depends_on部分文档

docker-compose up will start services in dependency order. docker-compose up 将按依赖顺序启动服务。 When docker-compose execute V2 files, it will automatically build a network between all of the containers defined in the file, and every container will be immediately able to refer to the others just using the names defined in the docker-compose.yml file.当 docker-compose 执行 V2 文件时,它会自动在文件中定义的所有容器之间建立一个网络,每个容器都可以立即使用 docker-compose.yml 文件中定义的名称来引用其他容器。

But

Note: depends_on will not wait for db and redis to be “ready” before starting web - only until they have been started.注意:depends_on 不会等待 db 和 redis 在启动 web 之前“准备好” - 只有在它们启动之前。

For that check Controlling startup order .对于该检查Controlling startup order

You need add command for checking state of service.您需要添加command来检查服务状态。 More in documentation...更多文档...

depends_on:
      - "db"
    command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]

Your Dockerfile and docker-compose.yml files seem correct except for two things:你的 Dockerfile 和 docker-compose.yml 文件似乎是正确的,除了两件事:

  1. instead of the keyword links , you need to use depends_on .您需要使用depends_on而不是关键字links This will build and run RabbitMQ instance first, then your web service.这将首先构建和运行 RabbitMQ 实例,然后是您的 Web 服务。

  2. you should add this line under your messagingapp service:你应该在你的消息应用服务下添加这一行:

--> -->

environment:
-  SPRING_RABBITMQ_HOST=rabbitmq

This will let your Spring Boot application to recognize the RabbitMQ image in Docker.这将使您的 Spring Boot 应用程序能够识别 Docker 中的 RabbitMQ 映像。

So, try to update your docker-compose.yml file like this:所以,试着像这样更新你的 docker-compose.yml 文件:

rabbitmq:
  image: rabbitmq:management
  ports:
    - "5672:5672"
    - "15672:15672"
messagingapp:
  image: messagingapp:latest
  ports:
    - "80:8080"
  environment:
    - SPRING_RABBITMQ_HOST=rabbitmq
  depends_on:
    - rabbitmq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM