Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why bootstrap.properties is ignored by spring-cloud-starter-config?

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>
like image 405
kidfrom Avatar asked Nov 29 '20 18:11

kidfrom


People also ask

How do I enable bootstrapping in spring boot?

One of the ways to Bootstrapping a Spring Boot application is by using Spring Initializer. To do this, you will have to visit the Spring Initializer web page www.start.spring.io and choose your Build, Spring Boot Version and platform.

Are bootstrap properties deprecated?

With Spring Cloud Vault 3.0 and Spring Boot 2.4, the bootstrap context initialization ( bootstrap. yml , bootstrap. properties ) of property sources was deprecated.

Does bootstrap override application properties?

Overview. Spring Boot is an opinionated framework. Despite this, we usually end up overriding autoconfigured properties in an application configuration file such as application. properties.


3 Answers

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

like image 101
SledgeHammer Avatar answered Oct 18 '22 19:10

SledgeHammer


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.

like image 26
Hafiz Hamza Avatar answered Oct 18 '22 20:10

Hafiz Hamza


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

like image 11
Wallace Jackson Avatar answered Oct 18 '22 20:10

Wallace Jackson