简体   繁体   中英

Connection Refused to Rabbitmq container from Spring boot app Container

I'm facing an error to connect from my spring boot app container to Rabbitmq.

I have attached the two docker containers ( Rabbitmq and spring boot app) with bridge.network in my docker compose file:

    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

the connection is refused by Rabbitmq when my Spring boot app attempts to connect to this latter. Below the log:

11:35:22.176 [main] WARN oac.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)

When I execute the below command inside my spring boot app container (orchestrator in my case) I get the following response:

nc -vz rabbitmq 5672

The response is:

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

So the tcp connection is succeeded between the two containers, but when I curl to rabbitmq inside my orchestrator container with the credentials that are defined in the above docker compose file, I get the connection refused:

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

But when I change localhost with rabbitmq as a host like below:

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

The response is succeeded !

Below my properties config in spring boot app ( orchestrator ):

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

I'm using apache camel to publish messages in rabbitmq (below an example of camel route that read from an API and publish the response in rabbitmq topic):

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();

The 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

I run mvn clean package before running the docker-compose file.

Thanks a lot for your help.

At the end of the journey, I have found the solution, and yes after modifying the spring properties to these below new properties:

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

In this manner, we can tell camel which is bounded to rabbitmq about the host, port, username, and password, before just rabbitmq knows these properties, as a result, camel cannot resolve the rabbitmq's host.

Then I changed the docker-compose environment for the orchestrator container:

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

I have modified the rabbitmq's host property (camel_component_rabbitmq_hostname) to rabbitmq for container resolution.

I hope that my solution works for you, enjoy !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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