bootstrap.yml没有在Spring Boot 2中加载

时间:2018-06-12 15:45:17

标签: java spring-boot spring-cloud

启动需要连接到配置服务器的spring boot客户端应用程序时遇到问题。 bootstrap.yml文件被忽略

配置服务器 - 这有效!

server:
  port: 8888  

spring:
  application:
    name: configserver
  cloud:
    config:
      server:
       git:
         uri:https://xxxxx@bitbucket.org/eco/properties.git

bootstrap.yml - 配置客户端 - 不工作!

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

启动应用程序时忽略文件bootstrap.yml

POM客户端

<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>

主类客户

@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);
}

}

为什么要忽略bootstrap.yml?

7 个答案:

答案 0 :(得分:8)

在spring boot config客户端中添加这个依赖:

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

答案 1 :(得分:3)

您需要为spring cloud starter添加依赖项。

答案 2 :(得分:1)

这是您必须添加的依赖项

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

答案 3 :(得分:1)

应用程序将使用 bootstrap.yml,以防它使用一些 spring-cloud 依赖项。那可以是云发现客户端

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

或云配置客户端

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

答案 4 :(得分:0)

您需要指定配置名称,并且必须与配置服务上的配置文件名匹配。

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

答案 5 :(得分:0)

如果您定义了spring.config.location,它将覆盖配置文件的所有路径,包括bootstrap.yml的路径,因此必须将spring.config.location更改为spring.config.additional-location

因此,当您启动应用程序时,必须添加-Dspring.config.additional-location=/path/to/application.yml

有关更多信息,请检查this

答案 6 :(得分:0)

在我的情况下,我使用STS4和默认情况下生成的POM文件不起作用,并且遇到了与您相同的问题,但是在对POM文件进行了一些更改之后,一切正常,最后我发现了类似的问题在我的控制台中“从服务器获取配置”。 我的POM中的更改如下:

  1. 将spring boot版本更改为2.2.6.RELEASE。
  2. 将spring cloud版本更改为Hoxton.SR6或Hoxton.SR4(我相信 Hoxton版本将可用)。

应用了之前的更改后,我的POM文件如下:

<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>