Book-Microservices-v2 / chapter08d

Learn Microservices with Spring Boot (2nd edition) - Chapter 8 (FINAL)
https://tpd.io/book-extra
11 stars 19 forks source link

Can not connect to RabbitMQ #2

Open andreykirson opened 3 years ago

andreykirson commented 3 years ago

I did the final docker-compose.yml file and when I start it I got the following error

Here is my docker-file: https://github.com/andreykirson/docker_mental_arithmetic

Perhaps it needs to change the settings somewhere else?

logs_1 | logs1 | . ____ logs1 | /\ / '_ () _ \ \ \ \ logs1 | ( ( )\ | ' | '| | ' \/ ` | \ \ \ \ logs_1 | \/ )| |)| | | | | || (_| | ) ) ) ) logs_1 | ' |__| .|| ||| |__, | / / / / logs1 | =========||==============|_/=///_/ logs_1 | :: Spring Boot :: (v2.5.4) logs_1 | logs_1 | [ ] [,] INFO HV000001: Hibernate Validator 6.2.0.Final logs_1 | [ ] [,] INFO Starting LogCenralizationApplication v0.0.1-SNAPSHOT using Java 16.0.2 on 0b35fc6a05c3 with PID 1 (/workspace/BOOT-INF/classes started by cnb in /workspace) logs_1 | [ ] [,] INFO The following profiles are active: docker logs_1 | [ ] [,] INFO BeanFactory id=688ff25e-fb79-3dc8-af85-a379b9ede61f logs_1 | [ ] [,] INFO Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [org.springframework.retry.annotation.RetryConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) logs_1 | [ ] [,] INFO Tomcat initialized with port(s): 8580 (http) logs_1 | [ ] [,] INFO Initializing ProtocolHandler ["http-nio-8580"] logs_1 | [ ] [,] INFO Starting service [Tomcat] logs_1 | [ ] [,] INFO Starting Servlet engine: [Apache Tomcat/9.0.52] logs_1 | [ ] [,] INFO Initializing Spring embedded WebApplicationContext logs_1 | [ ] [,] INFO Root WebApplicationContext: initialization completed in 3024 ms logs_1 | [ ] [,] INFO Exposing 1 endpoint(s) beneath base path '/actuator' logs_1 | [ ] [,] INFO Starting ProtocolHandler ["http-nio-8580"] logs_1 | [ ] [,] INFO Tomcat started on port(s): 8580 (http) with context path '' logs_1 | [ ] [,] INFO Attempting to connect to: [localhost:5672] logs_1 | [ ] [,] INFO Broker not available; cannot force queue declarations during start: java.net.ConnectException: Connection refused logs_1 | [ ] [,] INFO Attempting to connect to: [localhost:5672] logs_1 | [ ] [,] ERROR Failed to check/redeclare auto-delete queue(s). logs_1 | org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused logs_1 | at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) logs_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:602) logs_1 | at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:724) logs_1 | at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:252) logs_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2163) logs_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2136) logs_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2116) logs_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:407) logs_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:391) logs_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1914) logs_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1895) logs_1 | at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1347) logs_1 | at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1193) logs_1 | at java.base/java.lang.Thread.run(Unknown Source) logs_1 | Caused by: java.net.ConnectException: Connection refused logs_1 | at java.base/sun.nio.ch.Net.pollConnect(Native Method) logs_1 | at java.base/sun.nio.ch.Net.pollConnectNow(Unknown Source) logs_1 | at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(Unknown Source) logs_1 | at java.base/sun.nio.ch.NioSocketImpl.connect(Unknown Source) logs_1 | at java.base/java.net.SocksSocketImpl.connect(Unknown Source) logs_1 | at java.base/java.net.Socket.connect(Unknown Source) logs_1 | at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) logs_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1220) logs_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1170) logs_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connectAddresses(AbstractConnectionFactory.java:640) logs_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:615) logs_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:565) logs_1 | ... 12 common frames omitted logs_1 | [ ] [,] INFO Attempting to connect to: [localhost:5672] logs_1 | [ ] [,] INFO Started LogCenralizationApplication in 40.558 seconds (JVM running for 41.286) logs_1 | [ ] [,] WARN Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused logs_1 | [ ] [,] INFO Restarting Consumer@22368ddc: tags=[[]], channel=null, acknowledgeMode=AUTO local queue size=0 logs_1 | [ ] [,] INFO Attempting to connect to: [localhost:5672] logs_1 | [ ] [,] ERROR Failed to check/redeclare auto-delete queue(s). logs_1 | org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused logs_1 | at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) logs_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:602) logs_1 | at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:724) logs_1 | at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:252) logs_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2163) logs_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2136) logs_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2116) logs_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:407) logs_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:391) logs_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1914) logs_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1895) logs_1 | at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1347) logs_1 | at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1193) logs_1 | at java.base/java.lang.Thread.run(Unknown Source) logs_1 | Caused by: java.net.ConnectException: Connection refused logs_1 | at java.base/sun.nio.ch.Net.pollConnect(Native Method) logs_1 | at java.base/sun.nio.ch.Net.pollConnectNow(Unknown Source) logs_1 | at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(Unknown Source) logs_1 | at java.base/sun.nio.ch.NioSocketImpl.connect(Unknown Source) logs_1 | at java.base/java.net.SocksSocketImpl.connect(Unknown Source) logs_1 | at java.base/java.net.Socket.connect(Unknown Source) logs_1 | at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) logs_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1220) logs_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1170) logs_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connectAddresses(AbstractConnectionFactory.java:640) logs_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:615) logs_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:565)

mpssantos commented 2 years ago

i had a similar issue. apparently multiplication and gamification are not reading the application.yml from K/V from Consul.

I added the following line on docker-compose file config and it started to work:

multiplication: image: multiplication:0.0.1-SNAPSHOT environment:

  • SPRING_PROFILES_ACTIVE=docker
  • SPRING_CLOUD_CONSUL_HOST=consul - SPRING_RABBITMQ_HOST=rabbitmq depends_on:
  • rabbitmq-dev
  • consul-importer networks:
  • microservices gamification: image: gamification:0.0.1 environment:
  • SPRING_PROFILES_ACTIVE=docker
  • SPRING_CLOUD_CONSUL_HOST=consul - SPRING_RABBITMQ_HOST=rabbitmq depends_on:
  • rabbitmq-dev
  • consul-importer networks:
  • microservices

I hope it helps you

mpssantos commented 2 years ago

What version of Spring Cloud Consul are you using? I was having this very same issue using version 3.1.0

For me, the issue had something to do with an update to Spring Cloud Consul. Since version 3.1.0 the bootstrap.properties file is deprecated. We should use those properties in application.properties file in conjunction with spring.config.import attribute.

check documentation:

This worked for me. I hope it help you.

Best regards and take care

mechero commented 2 years ago

Sorry for the late reply. Please check the changes that were introduced in new versions of Spring Cloud, specifically the one about the application name. The proposed solution might fix your problem.

Blog post: Book update: Spring Boot 2.4 / Spring Cloud 2020.0