之前只听别人说Spring Cloud的版本有坑,今天终于遇到一个,特此记录。
spring boot版本:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent>
spring cloud 版本:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.2</version> <type>pom</type> <scope>import</scope> </dependency>
======================================================================================================
想要生效bootstrap.yml配置文件,原来的时候需要加入以下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
但现在死活不生效,搞得一下午都懵了。
这是因为20版本把spring.cloud.bootstrap.enabled默认值由原来的ture改为false了:
package org.springframework.cloud.util;
public abstract class PropertyUtils {
public static boolean bootstrapEnabled(Environment environment) {
return (Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, false) || MARKER_CLASS_EXISTS;
}
看下官方说法:https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-first-bootstrap
Config First Bootstrap
To use the legacy bootstrap way of connecting to Config Server, bootstrap must be enabled via a property or the
spring-cloud-starter-bootstrap
starter. The property isspring.cloud.bootstrap.enabled=true
. It must be set as a System Property or environment variable. Once bootstrap has been enabled any application with Spring Cloud Config Client on the classpath will connect to Config Server as follows: When a config client starts, it binds to the Config Server (through thespring.cloud.config.uri
bootstrap configuration property) and initializes SpringEnvironment
with remote property sources.The net result of this behavior is that all client applications that want to consume the Config Server need a
bootstrap.yml
(or an environment variable) with the server address set inspring.cloud.config.uri
(it defaults to "http://localhost:8888").
所以需要加入以下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
或者
java -jar -Dspring.cloud.bootstrap.enabled=true 你的springboot.jar
问题解决!