14

My goal is to get the config for world-service from a config-service.

The architecture:

  1. config-service with dependency spring-cloud-config-server at localhost:8888
  2. world-service with dependency the spring-web and spring-cloud-starter-config.

What I have done:

  1. I have set up the Config Server and send a GET request to http://localhost:8888/hello-service/master and the config server get the hello-service.properties from the config-repo repository. (If you need the config-service's source code, I will push it to this repository.)

My expected result: The world-service use port 8081.

My actual result: The world-service use port 8080.

bootstrap.properties

spring.application.name=world-service
spring.cloud.config.uri=http://localhost:8888

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>world-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>world-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2020.0.0-M5</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>
Jason Rich Darmawan
  • 1,607
  • 3
  • 14
  • 31

3 Answers3

29

With Spring Cloud 2020, they made a change in how bootstrap works and you have to include a new starter: spring-cloud-starter-bootstrap.

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • 1
    Do you mean adding ` org.springframework.cloud spring-cloud-starter-bootstrap` to pom.xml? Thank you for pointing it out, I tried to google it but could not found any explanation. I also got an error when tried to add it to pom.xml `Cannot resolve org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.0-M5` – Jason Rich Darmawan Nov 29 '20 at 18:57
  • @kidfrom It looks correct. This a c&p from mine: org.springframework.cloud spring-cloud-starter-bootstrap3.0.0-M5 – SledgeHammer Nov 29 '20 at 19:07
  • @kidfrom it's in the milestone repo: https://repo.spring.io/milestone/org/springframework/cloud/spring-cloud-starter-bootstrap/3.0.0-M5/ – SledgeHammer Nov 29 '20 at 19:09
  • @kidfrom see info from the cloud team about the topic here: https://spring.io/blog/2020/10/07/spring-cloud-2020-0-0-m4-aka-ilford-is-available – SledgeHammer Nov 29 '20 at 19:10
  • Thank you, it's fixed. Thank you for the help. The issue was somehow maven decided to use `offline mode`. I switch it off and its fixed. Anyway it's 2 AM, I will keep you updated at 5 AM. – Jason Rich Darmawan Nov 29 '20 at 19:17
  • 1
    Or use the new spring.config.import https://github.com/spring-cloud/spring-cloud-release/wiki/Spring-Cloud-2020.0-Release-Notes#breaking-changes – spencergibb Nov 30 '20 at 01:43
16

I spent a day on it and finally found a solution. It may help others

You need to add new dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

According to the Spring Cloud 2020.0

Bootstrap, provided by spring-cloud-commons, is no longer enabled by default. If your project requires it, it can be re-enabled by properties or by a new starter.

To re-enable by properties set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true. These need to be set as an environment variable, java system property or a command line argument. The other option is to include the new spring-cloud-starter-bootstrap (in your POM file).

I used the first option and that worked for me.

Hafiz Hamza
  • 311
  • 3
  • 16
12

Spring Boot 2.4 introduced a new way to import configuration data via the spring.config.import property. This is now the default way to bind to Config Server.

To connect to config server set the following in application.yml:

spring:
  application:
    name: APPLICATION_NAME
  config:
    import: optional:configserver:http://USER:PASSWORD@MY_HOST:PORT/

You can see more details in: https://docs.spring.io/spring-cloud-config/docs/3.0.0/reference/html/#config-data-import

  • 8
    People who were downvoting this answer were wrong. Bootstrap is deprecated as of Spring Boot 2.4, and no longer enabled by default. You can re-enable it per the accepted answer, but in the future it will be removed and the way described in this answer is the new default. – Mzzl Feb 15 '21 at 01:30
  • 1
    @Mzzl where did you see that bootstrap is deprecated? I see it is no longer the default, but I don't see any mention of deprecation...? – LarryW Aug 09 '21 at 20:38
  • @LarryW I was looking for that source too saying it's been deprecated and don't see it mentioned on the Spring blog or in the source code. – cwa Jun 10 '22 at 21:18
  • 1
    It's not deprecated. Spring changed the behavior. Read about it here https://cloud.spring.io/spring-cloud-static/spring-cloud.html#_the_bootstrap_application_context – cwa Jun 10 '22 at 21:22