简体   繁体   English

bootstrap.yml 未在 Spring Boot 2 中加载

[英]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.我在启动需要连接到配置服务器的 spring 引导客户端应用程序时遇到问题。 The bootstrap.yml file is being ignored bootstrap.yml 文件被忽略

Configuration Server - This works!配置服务器 - 这有效!

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

bootstrap.yml - Config Client - Not working! bootstrap.yml - 配置客户端 - 不工作!

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

The file bootstrap.yml is being ignored when starting the application .启动应用程序时将忽略文件 bootstrap.yml

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

Main class client主要客户 class

@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?为什么 bootstrap.yml 被忽略了?

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

Add this dependency in spring boot config client:在 spring boot 配置客户端中添加此依赖项:

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

this is the dependency you must add这是您必须添加的依赖项

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

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

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

So when you launch your app you'll have to add -Dspring.config.additional-location=/path/to/application.yml .因此,当您启动应用程序时,您必须添加-Dspring.config.additional-location=/path/to/application.yml

For more info check this有关更多信息,请检查

Application will use bootstrap.yml in case when it uses some spring-cloud dependencies.应用程序将使用bootstrap.yml以防它使用一些 spring-cloud 依赖项。 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>

I had the same problem.我有同样的问题。 The pom.xml i used specified the files it includes from src/main/resources.我使用的 pom.xml 指定了它包含的来自 src/main/resources 的文件。 So i also had to add the bootstrap.properties here:所以我还必须在此处添加 bootstrap.properties:

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

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.在我的情况下,我使用 STS4 并且默认情况下生成的 POM 文件不起作用,我遇到了和你一样的问题,但是在我对 POM 文件进行了一些更改后,一切正常,最后我发现了类似“获取配置”的内容从服务器在”在我的控制台。 The changes in my POM were as follows:我的 POM 中的变化如下:

  1. Change the spring boot version to 2.2.6.RELEASE.将 spring boot 版本更改为 2.2.6.RELEASE。
  2. Change the spring cloud version to Hoxton.SR6 Or Hoxton.SR4 (and I believe any Hoxton version will work).将 spring cloud 版本更改为 Hoxton.SR6 或 Hoxton.SR4(我相信任何 Hoxton 版本都可以)。

After I applied the previous changes, My POM file looks like:应用之前的更改后,我的 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 spring boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别? - What is the difference between putting a property on application.yml or bootstrap.yml in spring boot? Spring 引导:使用 bootstrap.yml 加载通用配置应用程序属性文件 - Spring Boot: Load common config application properties file using bootstrap.yml 在spring-boot类构造函数中调用first或bootstrap.yml? - In spring-boot Class constructor get called first or bootstrap.yml? 在 Spring 上未找到/读取 Bootstrap.yml 部署在 Tomcat 上的 Boot 2 应用程序 - Bootstrap.yml not being found/read on Spring Boot 2 application deployed on Tomcat 创建依赖于Spring Boot并具有自己的bootstrap.yml的库 - Create Library that has a dependency on Spring Boot that has a it's own bootstrap.yml 无法从spring-boot中的Properties文件和Bootstrap.yml中选择值 - Unable to pick the values from Properties file and Bootstrap.yml in spring-boot 是否需要 bootstrap.yml? - Is bootstrap.yml required? Spring Cloud 2020.0 不再处理 bootstrap.yml 配置 - bootstrap.yml configuration not processed anymore with Spring Cloud 2020.0 在 spring-cloud 项目中,可以完全用 bootstrap.yml 替换 application.yml 吗? - In spring-cloud project, is it ok to replace application.yml with bootstrap.yml totally? 使用Java而不是bootstrap.yml通过Eureka查找Spring Cloud Config Server - Spring Cloud Config Server lookup through Eureka using Java instead of bootstrap.yml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM