[英]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
。
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 中的变化如下:
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.