Why is Spring Cloud ignoring my bootstrap-prod.xml?

2/8/2022

I have this microservice based on Spring Boot 2.2.7 and it works very well when locally. However when I try to deploy in a Kubernetes cluster, it fails on loading the Config Map values at the startup and, consequently, everything fails from this point:

The container logging is this:

2022-02-08 14:54:50.696  WARN [myservice,,,] 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8761/config/myservice/dev/master": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused 

I have a bootstrap.yml file like this:

jhipster:
  registry:
    password: admin

spring:
  application:
    name: myservice
  profiles:
    # The commented value for `active` can be replaced with valid Spring profiles to load.
    # Otherwise, it will be filled in by maven when building the JAR file
    # Either way, it can be overridden by `--spring.profiles.active` value passed in the commandline or `-Dspring.profiles.active` set in `JAVA_OPTS`
    active: #spring.profiles.active#
  cloud:
    config:
      fail-fast: false # if not in "prod" profile, do not force to use Spring Cloud Config
      uri: http://admin:${jhipster.registry.password}@localhost:8761/config
      # name of the config server's property source (file.yml) that we want to use
      name: myservice
      profile: dev # profile(s) of the property source
      label: master # toggle to switch to a different version of the configuration as stored in git
      # it can be set to any label, branch or commit of the configuration source Git repository

This is my bootstrap-prod.yml

spring:
  application:
    name: myservice
  cloud:
    kubernetes:
      config:
        enabled: true # enables fetching configmap
        name: myservice
        profile: prod
        sources:
         - name: myservice
      enabled: true # enables all the sub-configurations

It's like when the application starts, Spring Boot just ignored my bootstrap-prod.yml.

The container's environment variable SPRING_PROFILES_ACTIVE value is "prod,swagger,no-liquibase".

-- Fabiano
kubernetes
spring
spring-boot

1 Answer

2/9/2022

I finally found out the cause of this problem.

It's necessary to include this dependency in the pom.xml:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-kubernetes-config</artifactId>
   <version>${spring-cloud-kubernetes.version}</version>
</dependency>

In my case it was a little more tricky because I had this dependency already, but it was inside a "prod" profile. So just had to run maven specifying this profile:

mvn package -P prod
-- Fabiano
Source: StackOverflow