The Spring Cloud 2020 version Bootstrap configuration file (properties or YML) is invalid
How to solve it?
background
Microservices are built based on the Spring Cloud framework, and Spring Cloud Config serves as the service configuration center.
For service services, only the service name, enabling environment, and THE URL of Config are configured. Other parameters, such as the service port and service registry address, are configured in the configuration center. It can be configured in the development environment (DEV), test environment (Test), and production environment (PROD).
Therefore, the expected startup process is: load the configuration file first, and then start the service.
Previously, you changed the name of the configuration file to: bootstrap.properties.
The problem
The bootstrap.properties file was not loaded. I thought I wrote the name of the file wrong. I checked it several times and found it correct.
Previous version:
Spring boot 2.3.1. RELEASE
spring cloud Hoxton.SR4
Current version:
Spring boot 2.4.2
Spring cloud 2020.0.1
To find the reason
Based on the above problem, I did a baidu.com search and found the probable cause: Since Spring Boot 2.4, the configuration file loading method has been reconfigured.
There is also a change in the default value of the configuration, as follows:
Spring Boot 2.3.8. RELEASE
package org.springframework.cloud.bootstrap;
public class BootstrapApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
if ((Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, true)) {
Copy the code
Spring Boot 2.4.2
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;
}
Copy the code
Traditional solutions
In fact, the official website is very clear. Look at this:
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”).
Two key points:
1. Add a dependency: spring-cloud-starter-bootstrap
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Copy the code
2. Add a configuration: spring.cloud.config.uri
bootstrap.properties
# app name
spring.application.name=erwin-cloud-user
# Enable environment
spring.profiles.active=dev
# config file
spring.cloud.config.label=${spring.application.name}
spring.cloud.config.name=${spring.application.name}
spring.cloud.config.profile=${spring.profiles.active}
spring.cloud.config.uri=http://localhost:9000
Copy the code
The solution
Now, all you need is this:
application.properties
# app name
spring.application.name=erwin-cloud-user
# Enable environment
spring.profiles.active=dev
spring.config.import=optional:configserver:http://localhost:9000
spring.cloud.config.label=${spring.application.name}
spring.cloud.config.name=${spring.application.name}
spring.cloud.config.profile=${spring.profiles.active}
Copy the code