Search code examples
spring-cloud-netflixspring-cloud-config

Docker - Spring Cloud Config Client, Issue with Config Server Discovery


I'm experimenting with Spring Cloud Netflix stack and Spring Cloud Config Server and clients.

For this, I have set a minimal example as shown from the following docker-compose file.

version: '3'
services:

#Eureka Service
  discovery:
    container_name: discovery
    image: jbprek/discovery:latest
    ports:
      - "8761:8761"

#Spring cloud config server
  configservice:
    container_name: configserver
    image: jbprek/configserver:latest
    ports:
      - "8888:8888"
    depends_on:
      - discovery

#Example microservice using discovery and spring cloud config
  constants-service:
    container_name: constants-service
    ports:
      - "8080:8080"
    image: jbprek/constants-service:latest
    depends_on:
      - discovery
      - configservice

The implementation of discovery and configserver are minimal following various samples and the full code can cloned by:

git clone https://[email protected]/prek/boot-netflix-problem.git

When the spring cloud config client "constants-service" uses the following configuration in bootstrap.properties

spring.application.name=constants-service
spring.cloud.config.uri=http://configserver:8888

then everything seems to work fine including registration with "Eureka" and retrieval of configuration from configserver.

Then to lookup configserver by discovery and then retrieve the configuration from constant-service, I modify the bootstrap.properties file as follows:

spring.application.name=constants-service
#Lookup through Eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=CONFIGSERVER

The above change has the effect of preventing "constants-service" to connect to eureka, by using as Eureka hostname, localhost instead of discovery and both the lookup of configserver service and self registration with Eureka fail.

The application.properties for discovery is:

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

The application.properties for configserver is:

server.port=8888
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://discovery:8761/eureka

spring.cloud.config.server.git.uri=https://bitbucket.org/prek/boot-netflix-problem-config-data.git
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.discovery.enabled=false

And for constants service is:

spring.application.name=constants-service

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://discovery:8761/eureka

Can someone advise on the above configuration?

Update

According to the answer provided below by @nmyk for constants-service which ise both Eureka (discovery) client and Spring Cloud Config client, the configuration for both the discovery and the config should be contained in bootstrap.properties files so given the examples mentioned above the boostrap.properties file for constants service could be:

spring.application.name=constants-service

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://discovery:8761/eureka

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=CONFIGSERVER

Solution

  • You are switching your application to be in 'Discovery First mode', so your constants-service should know about Eureka and get configserver URL from it by name.

    Problem is simple : your bootstrap.properties of constants-service does not contain URL to Eureka, your should move eureka client configuration from git repo (application.properties) to bootstrap.properties.