来自 Spring boot app Container 的 Rabbitmq 容器拒绝连接

Connection Refused to Rabbitmq container from Spring boot app Container

我在从 spring 引导应用程序容器连接到 Rabbitmq 时遇到错误。

我已将两个 docker 容器(Rabbitmqspring boot 应用程序连接到桥接网络在我的 docker 撰写文件中:

    version: '3.3'
services:
  rabbitmq:
    image: rabbitmq:3.8-management-alpine
    container_name: rabbitmq
    ports:
      - 5673:5673
      - 5672:5672
      - 15672:15672
    networks:
      - orchestrator-rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=adminsi
      - RABBITMQ_DEFAULT_PASS=test
  orchestrator:
    restart: on-failure
    build:
      context: .
      dockerfile: Dockerfile-orchestrator
      args:
        VERSION: ${VERSION}
    environment:
      - spring_rabbitmq_host=rabbitmq
      - spring_rabbitmq_port=5672
      - spring_rabbitmq_username=adminsi
      - spring_rabbitmq_password=test
    container_name: orchestrator
    depends_on:
      - rabbitmq
    networks:
      - orchestrator-rabbitmq
    ports:
      - 7127:7127

networks:
  orchestrator-rabbitmq:
    external:
      name: orchestrator-rabbitmq

当我的 Spring 引导应用程序尝试连接到后者时,Rabbitmq 拒绝连接。 日志下方:

11:35:22.176 [main] WARN o.a.c.c.rabbitmq.RabbitMQProducer - Failed to create connection. It will attempt to connect again when publishing a message. orchestrator | java.net.ConnectException: Connection refused orchestrator | at java.base/sun.nio.ch.Net.pollConnect(Native Method) orchestrator | at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) orchestrator | at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) orchestrator | at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) orchestrator | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) orchestrator | at java.base/java.net.Socket.connect(Socket.java:633) orchestrator |
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) orchestrator | at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62) orchestrator | at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:156) orchestrator | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1213) orchestrator | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1170) orchestrator | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1128) orchestrator | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1321) orchestrator | at org.apache.camel.component.rabbitmq.RabbitMQEndpoint.connect(RabbitMQEndpoint.java:247) orchestrator | at org.apache.camel.component.rabbitmq.RabbitMQProducer.openConnectionAndChannelPool(RabbitMQProducer.java:108) orchestrator | at org.apache.camel.component.rabbitmq.RabbitMQProducer.doStart(RabbitMQProducer.java:163) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.impl.engine.AbstractCamelContext.internalAddService(AbstractCamelContext.java:1554) orchestrator | at org.apache.camel.impl.engine.AbstractCamelContext.addService(AbstractCamelContext.java:1475) orchestrator | at org.apache.camel.processor.SendProcessor.doStart(SendProcessor.java:247) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130) orchestrator | at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1655) orchestrator | at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:60) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130) orchestrator | at org.apache.camel.impl.engine.DefaultChannel.doStart(DefaultChannel.java:126) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:116) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130) orchestrator | at org.apache.camel.processor.Pipeline.doStart(Pipeline.java:221) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:89) orchestrator | at org.apache.camel.processor.FilterProcessor.doStart(FilterProcessor.java:138) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:116) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130) orchestrator | at org.apache.camel.processor.ChoiceProcessor.doStart(ChoiceProcessor.java:185) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130) orchestrator | at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1655) orchestrator | at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:60) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130) orchestrator | at org.apache.camel.impl.engine.DefaultChannel.doStart(DefaultChannel.java:126) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:116) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130) orchestrator | at org.apache.camel.processor.Pipeline.doStart(Pipeline.java:221) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.support.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:89) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113) orchestrator | at org.apache.camel.impl.engine.RouteService.startChildServices(RouteService.java:396) orchestrator | at org.apache.camel.impl.engine.RouteService.doWarmUp(RouteService.java:193) orchestrator | at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:121) orchestrator | at org.apache.camel.impl.engine.InternalRouteStartupManager.doWarmUpRoutes(InternalRouteStartupManager.java:306) orchestrator | at org.apache.camel.impl.engine.InternalRouteStartupManager.safelyStartRouteServices(InternalRouteStartupManager.java:189) orchestrator | at org.apache.camel.impl.engine.InternalRouteStartupManager.doStartOrResumeRoutes(InternalRouteStartupManager.java:147) orchestrator | at org.apache.camel.impl.engine.AbstractCamelContext.doStartCamel(AbstractCamelContext.java:3300) orchestrator | at org.apache.camel.impl.engine.AbstractCamelContext.doStartContext(AbstractCamelContext.java:2952) orchestrator | at org.apache.camel.impl.engine.AbstractCamelContext.doStart(AbstractCamelContext.java:2903) orchestrator | at org.apache.camel.spring.boot.SpringBootCamelContext.doStart(SpringBootCamelContext.java:43) orchestrator | at org.apache.camel.support.service.BaseService.start(BaseService.java:119) orchestrator | at org.apache.camel.impl.engine.AbstractCamelContext.start(AbstractCamelContext.java:2587) orchestrator | at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:247) orchestrator | at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:119) orchestrator | at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:151) orchestrator | at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) orchestrator | at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) orchestrator | at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) orchestrator | at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421) orchestrator | at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) orchestrator | at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:938) orchestrator | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) orchestrator | at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) orchestrator | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) orchestrator | at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) orchestrator | at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) orchestrator | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) orchestrator | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) orchestrator | at fr.orange.oab.sie.service.orchestrator.OrchestratorApplication.main(OrchestratorApplication.java:31) orchestrator | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) orchestrator | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) orchestrator | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) orchestrator | at java.base/java.lang.reflect.Method.invoke(Method.java:568) orchestrator | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) orchestrator | at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) orchestrator | at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) orchestrator | at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

当我在我的 spring 启动应用程序容器(在我的例子中是协调器)中执行以下命令时,我得到以下响应:

nc -vz rabbitmq 5672

响应是:

Connection to rabbitmq (172.19.0.2) 5672 port [tcp/*] succeeded!

所以 tcp 连接在两个容器之间成功,但是当我使用上面定义的凭据 orchestrator 容器 卷曲到 rabbitmq 时 docker撰写文件,我发现连接被拒绝:

curl -u "adminsi:test" http://localhost:15672

但是当我使用 rabbitmq 作为主机更改 localhost 时,如下所示:

curl -u "adminsi:test" http://rabbitmq:15672

响应成功!

在 spring 启动应用程序(orchestrator)中我的属性配置下方:

spring.rabbitmq.host=rabbitmq
spring.rabbitmq.port=5672
spring.rabbitmq.username=adminsi
spring.rabbitmq.password=test

我正在使用 apache camel 在 rabbitmq 中发布消息(下面是从 API 读取并在 rabbitmq 主题中发布响应的 camel 路由示例):

from("direct:OrchestratorDtstoreLoadDataRoute")
                .routeId("orchestrator-dtstore-route")
                .log(LoggingLevel.INFO, "Reading dtstore projects")
                .removeHeaders("*")
                .setHeader("Content-Type", () -> "application/json")
                .setHeader("CamelHttpMethod", () -> "GET")
                .setHeader("Authorization", () -> dtstoreToken)
                .setHeader("x-apikey", () -> dtstoreXapiKey)
                .recipientList(simple("cxfrs:{{cmdb.service.out.dtstore.url}}/projects"))
                .log(LoggingLevel.DEBUG, "dtstore projects : ${body}")
                .to(ExchangePattern.InOnly, "rabbitmq:q.cmdb.dtstore.projects?routingKey=dtstore&autoDelete=false&exchangeType=topic")
                .end();

Orchestrator Dockerfile:

FROM openjdk:17.0.2-jdk-slim
ARG VERSION

ENV ORCHESTRATOR_VERSION=$VERSION

RUN apt-get update && apt-get install -y \
  nano \
  netcat \
  iputils-ping \
  curl \
  && rm -rf /var/lib/apt/lists/*


WORKDIR /opt/app

COPY target/orchestrator-$ORCHESTRATOR_VERSION.jar .

EXPOSE 7127

ENTRYPOINT java -jar orchestrator-$ORCHESTRATOR_VERSION.jar

我 运行 mvn clean package before 运行ning docker-compose 文件。

非常感谢您的帮助。

旅程结束时,我找到了解决方案,是的,在将 spring 属性修改为以下新属性后:

camel.component.rabbitmq.hostname=localhost
camel.component.rabbitmq.port=5672
camel.component.rabbitmq.username=adminsi
camel.component.rabbitmq.password=test

通过这种方式,我们可以告诉绑定到rabbitmq的camel主机、端口、用户名和密码,在rabbitmq知道这些属性之前,camel无法解析rabbitmq的主机。

然后我更改了 orchestrator 容器的 docker-compose 环境:

version: '3.3'
services:
  rabbitmq:
    image: rabbitmq:3.8-management
    container_name: rabbitmq
    ports:
      - 5673:5673
      - 5672:5672
      - 15672:15672
    networks:
      - orchestrator-rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=adminsi
      - RABBITMQ_DEFAULT_PASS=test
  orchestrator:
    restart: on-failure
    build:
      context: .
      dockerfile: Dockerfile
      args:
        VERSION: ${VERSION}
    environment:
      - camel_component_rabbitmq_hostname=rabbitmq
      - camel_component_rabbitmq_port=5672
      - camel_component_rabbitmq_username=adminsi
      - camel_component_rabbitmq_password=test
    container_name: orchestrator
    depends_on:
      - rabbitmq
    networks:
      - orchestrator-rabbitmq
    ports:
      - 7127:7127

networks:
  orchestrator-rabbitmq:
    external:
      name: orchestrator-rabbitmq

我已经将rabbitmq的主机属性(camel_component_rabbitmq_hostname)修改为rabbitmq用于容器解析

希望我的解决方案对您有用,祝您愉快!