Book-Microservices-v2 / chapter08d

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

Docker compose: Cannot connect to RabbitMQ #3

Open mpssantos opened 2 years ago

mpssantos commented 2 years ago

Hi Moisés Garcia,

How are you? I hope you are doing fine.

First of all, congratulations for this great book. You touch in all most every challenge that we face when adopting a microservice architecture. And you do it on a very pragmatic way. Your language is simple and easy to follow. At the same time you introduce several nice tools. A big thank you for writing this book to us.

I am almost finishing the book. I am no chapter 8 on Docker Compose sub chapter but I am with difficulties to bring up all application. Via docker composer i was able to bring up successfully Consul, Rabbitmq, frontend and gateway. multiplication and gamification are throwing exceptions because they cannot connect to Rabbitmq

Status of containers

root@mpssantos-Lenovo-Yoga-S740-14IIL:/home/mpssantos/Development/workspace/springbootmicroservices/docker-config# docker-compose ps
             Name                            Command               State                                                                            Ports                                                                         
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
consul                            docker-entrypoint.sh agent ...   Up       8300/tcp, 8301/tcp, 8301/udp, 8302/tcp, 8302/udp, 0.0.0.0:8500->8500/tcp,:::8500->8500/tcp, 8600/tcp, 0.0.0.0:8600->8600/udp,:::8600->8600/udp        
docker-config_consul-importer_1   /bin/sh -c until consul kv ...   Exit 0                                                                                                                                                         
docker-config_frontend_1          /docker-entrypoint.sh ngin ...   Up       0.0.0.0:3000->80/tcp,:::3000->80/tcp                                                                                                                  
docker-config_gamification_1      java -jar gamification-0.0 ...   Up       8081/tcp                                                                                                                                              
docker-config_gateway_1           /cnb/process/web                 Up       0.0.0.0:8000->8000/tcp,:::8000->8000/tcp                                                                                                              
docker-config_multiplication_1    /cnb/process/web                 Up                                                                                                                                                             
rabbitmq                          docker-entrypoint.sh rabbi ...   Up       15671/tcp, 0.0.0.0:15672->15672/tcp,:::15672->15672/tcp, 15691/tcp, 15692/tcp, 25672/tcp, 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp,:::5672->5672/tcp

Docker Compose

version: "3"

services:
  frontend:
    image: challenges-frontend:1.0
    ports:
      - '3000:80'
  multiplication:
    image: multiplication:0.0.1-SNAPSHOT
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - SPRING_CLOUD_CONSUL_HOST=consul
    depends_on:
      - rabbitmq-dev
      - consul-importer
    networks:
      - microservices
  gamification:
    image: gamification:0.0.1
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - SPRING_CLOUD_CONSUL_HOST=consul
    depends_on:
      - rabbitmq-dev
      - consul-importer
    networks:
      - microservices
  gateway:
    image: gateway:0.0.1-SNAPSHOT
    ports:
      - '8000:8000'
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - SPRING_CLOUD_CONSUL_HOST=consul
    depends_on:
      - rabbitmq-dev
      - consul-importer
    networks:
      - microservices
  consul-importer:
    image: consul-importer:1.0
    depends_on:
      - consul-dev
    networks:
      - microservices
  consul-dev:
    image: consul:1.7.2
    container_name: consul
    ports:
      - '8500:8500'
      - '8600:8600/udp'
    command: 'agent -dev -node=learnmicro -client=0.0.0.0 -log-level=INFO'
    networks:
      - microservices
  rabbitmq-dev:
    image: rabbitmq:3-management
    container_name: rabbitmq
    ports:
      - '5672:5672'
      - '15672:15672'
    networks:
      - microservices

networks:
  microservices:
    driver: bridge

consul-kv-docker.json

[
    {
        "key": "config/",
        "flags": 0,
        "value": ""
    },
    {
        "key": "config/defaults,docker/",
        "flags": 0,
        "value": ""
    },
    {
        "key": "config/defaults,docker/application.yml",
        "flags": 0,
        "value": "c3ByaW5nOgogIHJhYmJpdG1xOgogICAgaG9zdDogcmFiYml0bXEKICBjbG91ZDoKICAgIGNvbnN1bDoKICAgICAgZGlzY292ZXJ5OgogICAgICAgIGluc3RhbmNlLWlkOiAke3NwcmluZy5hcHBsaWNhdGlvbi5uYW1lfS0ke3JhbmRvbS5pbnQoMTAwMCl9"
    }
]

Exception

022-01-28 23:52:06.396  INFO 1 --- [nio-8080-exec-5] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
multiplication_1   | 2022-01-28 23:52:06.397  WARN 1 --- [nio-8080-exec-5] o.s.b.a.amqp.RabbitHealthIndicator       : Rabbit health check failed
multiplication_1   | 
multiplication_1   | org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused
multiplication_1   |    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:602) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:724) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:252) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2165) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2138) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2118) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.boot.actuate.amqp.RabbitHealthIndicator.getVersion(RabbitHealthIndicator.java:49) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.amqp.RabbitHealthIndicator.doHealthCheck(RabbitHealthIndicator.java:44) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:82) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthIndicator.getHealth(HealthIndicator.java:37) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointWebExtension.getHealth(HealthEndpointWebExtension.java:87) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointWebExtension.getHealth(HealthEndpointWebExtension.java:45) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointSupport.getContribution(HealthEndpointSupport.java:130) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointSupport.getAggregateContribution(HealthEndpointSupport.java:141) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointSupport.getContribution(HealthEndpointSupport.java:126) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointSupport.getHealth(HealthEndpointSupport.java:95) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointSupport.getHealth(HealthEndpointSupport.java:66) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(HealthEndpointWebExtension.java:73) ~[spring-boot-actuator-2.6.2.jar:2.6.2]
multiplication_1   |    at org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(HealthEndpointWebExtension.java:62) ~[spring-boot-actuator-2.6.2.jar:2.6.2]

MORE STACK STRACE

multiplication_1   | Caused by: java.net.ConnectException: Connection refused
multiplication_1   |    at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
multiplication_1   |    at java.base/sun.nio.ch.Net.pollConnectNow(Unknown Source) ~[na:na]
multiplication_1   |    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(Unknown Source) ~[na:na]
multiplication_1   |    at java.base/sun.nio.ch.NioSocketImpl.connect(Unknown Source) ~[na:na]
multiplication_1   |    at java.base/java.net.SocksSocketImpl.connect(Unknown Source) ~[na:na]
multiplication_1   |    at java.base/java.net.Socket.connect(Unknown Source) ~[na:na]
multiplication_1   |    at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.13.1.jar:5.13.1]
multiplication_1   |    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1223) ~[amqp-client-5.13.1.jar:5.13.1]
multiplication_1   |    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1173) ~[amqp-client-5.13.1.jar:5.13.1]
multiplication_1   |    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connectAddresses(AbstractConnectionFactory.java:640) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:615) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:565) ~[spring-rabbit-2.4.1.jar:2.4.1]
multiplication_1   |    ... 79 common frames omitted
multiplication_1   | 
consul             |     2022-01-28T23:52:06.403Z [WARN]  agent: Check is now critical: check=service:multiplication-8080

The gamification exceptions are identical

Looks like the auto configuration for Rabbitmq fails, but i cannot understand why. Can you spot anything strange? How can i debug? Do you need me to supply any other file config file.

Thank you so much for your help.

Once again congratulation for your book. 5*

mpssantos commented 2 years ago

I discovered that if i add the following line to docker-compose config file it works:

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

This means that microservices are not being able to read the application.yml from consul K/V

Any ideas why?

Thank you very much.

mpssantos commented 2 years ago

I already discovered the issue. it has something to do with an update to Spring Cloud Consul. Since version 3.1.0 (the one i am using in my source code) the bootstrap.properties file is deprecated. We should use application.properties file in conjunction with spring.config.import attribute.

This worked for me.

Thank you so much for this wonderful book. Congratulations.

Take care mate.

Tudo de bom para ti. Desejo-te o melhor sucesso.

mechero commented 2 years ago

Thanks for the kind words! Obrigado.

It's really cool you found the solution as well, thanks for sharing! I think it might be related to the changes described in this blog post.

Let me know if that fix works for you (in case you try it). There are some other issues in that same blog post that could be applicable for you as well.

If you find the book useful, don't forget to leave a review on Goodreads / Amazon or your preferred platform, that helps a lot! 😄