Renan Lalier
Renan Lalier

Reputation: 731

bootstrap.yml not loading in Spring Boot 2

I'm experiencing a problem when starting a spring boot client application that needs to connect to the configuration server. The bootstrap.yml file is being ignored

Configuration Server - This works!

server:
  port: 8888  
spring:
  application:
    name: configserver
  cloud:
    config:
      server:
        git:
          uri:https://[email protected]/eco/properties.git

bootstrap.yml - Config Client - Not working!

spring:
  application:
    name: api
  cloud:
    config:
      uri: http://localhost:8888

The file bootstrap.yml is being ignored when starting the application.

POM Client

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.RC2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-cloud.version>2.0.0.RC2</spring-cloud.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- Database dependencies -->
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.2</version>
        <scope>system</scope>
        <systemPath>${basedir}/src/main/resources/lib/ojdbc7-12.1.0.2.jar</systemPath>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-properties-migrator</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>
<build>
    <finalName>api-emissor</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.5.RELEASE</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Main class client

@SpringBootApplication
@EnableEurekaClient
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
@ComponentScan("br.com.eco.api.emissor")
@EnableJpaRepositories("br.com.eco.api.emissor.repository")
@EntityScan("br.com.eco.api.emissor.domain")
public class Application {
    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Why is the bootstrap.yml being ignored?

Upvotes: 23

Views: 35616

Answers (8)

user1961312
user1961312

Reputation: 159

I had the same problem. The pom.xml i used specified the files it includes from src/main/resources. So i also had to add the bootstrap.properties here:

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application.properties</include>
                    <include>bootstrap.properties</include>
                </includes>
            </resource>
        </resources>
...</build>

Upvotes: 3

Dima Sendrea
Dima Sendrea

Reputation: 138

You need to specify a config name and it must match the config file name on the configuration service.

spring:
  cloud:
    config:
      name: myService    # myService.yml or myService-[profile].yml( if you have a profile activated).
        uri: http://localhost:8888

Upvotes: 3

Javasick
Javasick

Reputation: 2993

Application will use bootstrap.yml in case when it uses some spring-cloud dependencies. That can be cloud discovery client:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

or cloud config client:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

Upvotes: 2

Rita
Rita

Reputation: 451

Add this dependency in Spring Boot config client:

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

Upvotes: 45

Tiago Medici
Tiago Medici

Reputation: 2194

You must add this dependency to the pom.xml:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Upvotes: 4

Hany Kamal
Hany Kamal

Reputation: 21

In my case I using STS4 and the produced POM file by default doesn't work and I hade the same problem like you, but after I changed a little bit in my POM file, everything is working and finally I found something like "Fetching config from server at" in my console. The changes in my POM were as follows:

  1. Change the spring boot version to 2.2.6.RELEASE.
  2. Change the spring cloud version to Hoxton.SR6 Or Hoxton.SR4 (and I believe any Hoxton version will work).

After I applied the previous changes, My POM file looks like:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!-- <version>2.4.0</version> -->
        <version>2.2.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.kha.microservices</groupId>
    <artifactId>limits-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>limits-service</name>
    <description>Demo project for Spring Boot - Cloud</description>

    <properties>
        <java.version>1.8</java.version>
        
        <!-- <spring-cloud.version>2020.0.0-SNAPSHOT</spring-cloud.version> -->
        <!-- <spring-cloud.version>2020.0.0-M4</spring-cloud.version> -->
        
        <!--  Both are working with spring boot version 2.2.6.RELEASE 
              you should find something like "Fetching config from server at" in your console 
              each time you run the client project as spring boot app 
        -->
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
        <!-- <spring-cloud.version>Hoxton.SR4</spring-cloud.version> -->    
    </properties>

Upvotes: 0

Lamine Mbn
Lamine Mbn

Reputation: 172

If you defined a spring.config.location it will override all paths to the configurations files including the path to bootstrap.yml so you have to change spring.config.location to spring.config.additional-location.

So when you launch your app you'll have to add -Dspring.config.additional-location=/path/to/application.yml.

For more info check this

Upvotes: 2

Sakalya
Sakalya

Reputation: 578

You need to add dependency for spring cloud starter.

Upvotes: 18

Related Questions