henry sneed
henry sneed

Reputation: 526

Docker Compose Config Server unreachable by Spring Cloud Data Flow Microservices

The config server is reachable from localhost:8888 but when I deploy my applications on SCDF the following error occurs:

Fetching config from server at : http://localhost:8888
2021-07-30 14:58:53.535  INFO 143 --- [           main] o.s.b.context.config.ConfigDataLoader    : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2021-07-30 14:58:53.535  WARN 143 --- [           main] o.s.b.context.config.ConfigDataLoader    : Could not locate PropertySource ([ConfigServerConfigDataResource@3de88f64 uris = array<String>['http://localhost:8888'], optional = true, profiles = list['default']]): I/O error on GET request for "http://localhost:8888/backend-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

The application(s) deploy successfully on SCDF apart from the config server connection. The only property I specify in SCDF is the docker network. I'm using spring.config.import and am not using any bootstraps. This all works correctly when deployed locally but the microservices can't connect to the config server when deployed on SCDF.

Spring Boot Version: 2.5.1

app properties

spring.application.name=backend-service
spring.cloud.config.fail-fast=true
spring.cloud.config.retry.max-attempts=6
spring.cloud.config.retry.max-interval=11000
spring.config.import=optional:configserver:http://localhost:8888

config server properties

spring.cloud.config.server.git.uri=...
management.endpoints.web.exposure.include=*
spring.cloud.config.fail-fast=true
spring.cloud.config.retry.max-attempts=6
spring.cloud.config.retry.max-interval=11000
spring.cloud.bus.id=my-config-server
spring.cloud.stream.rabbit.bindings.springCloudBus.consumer.declareExchange=false
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.cloud.bus.enabled=true
spring.cloud.bus.refresh.enabled: true
spring.cloud.bus.env.enabled: true
server.port=8888

docker-compose.yml

version: '3.1'

services:

  h2:
    ...

  rabbitmq-container:
    image: rabbitmq:3.7.14-management
    hostname: dataflow-rabbitmq
    expose:
      - '5672'
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - scdfnet

  dataflow-server:
    ...
    networks:
      - scdfnet

  app-import:
    ...
    networks:
      - scdfnet 
  skipper-server:
    ...
    networks:
      - scdfnet

  configserver-container:
    image: ...
    ports:
      - "8888:8888"
    expose:
      - '8888'
    environment:
      - spring_rabbitmq_host=rabbitmq-container
      - spring_rabbitmq_port=5672
      - spring_rabbitmq_username=guest
      - spring_rabbitmq_password=guest
    depends_on:
      - rabbitmq-container
    networks:
      - scdfnet

networks:
  scdfnet:
    external:
      name: scdfnet

volumes:
     h2-data:

Upvotes: 0

Views: 1394

Answers (1)

henry sneed
henry sneed

Reputation: 526

For anyone else having this problem, I have found two ways of solving it. The problem is that once the Spring Boot application is containerized, the localhost referred to in the properties file will cause the program to fetch the localhost of the application container's virtual network and not that of your local machine.

There are numerous Stack Overflow answers for this same error but all center around corrections to bootstrap properties. However, bootstrap context initialization is deprecated since Spring Boot 2.4.

The first solution is to use your IPv4 address instead of localhost.

spring.config.import=configserver:http://<insert IPv4 address>:8888

For Example:

spring.config.import=configserver:http://10.6.39.148:8888

A much better solution than hardwiring addresses is to reference the config server container running in docker compose:

spring.config.import=optional:configserver:http://configserver-container:8888

Make sure that all of the Docker Compose services are running on the same network (scdf_network in my case) and note that this address will only work when running on docker-compose so if you are building the maven file on Eclipse, you may need to remove or disable your tests to build successfully. That might be unnecessary; it could just be that there is some property that I failed to copy to my local application.properties file which is causing the context tests to fail. According to the documentation, the optional label should allow the config client to run even if contact cannot be established with the config server.

Upvotes: 2

Related Questions