SpringCloud2020 bootstrap 配置文件失效的解决方法

 更新时间:2021年02月07日 08:56:24   作者:冯文议  
这篇文章主要介绍了SpringCloud2020 bootstrap 配置文件失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Spring Cloud 2020版本 bootstrap 配置文件(properties 或者 yml)无效

如何解决?

背景介绍

微服务是基于Spring Cloud框架搭建的,Spring Cloud Config作为服务配置中心。

业务服务只配置服务名称、启用环境和config的URL地址,其他都配置在配置中心,例如服务端口、服务注册中心地址等。可在开发环境(dev)、测试环境(test)和生产环境(prod)分别配置。

所以预想的启动流程是:先加载配置文件,再启动服务。

之前的做法是,将配置文件名称改为:bootstrap.properties。

问题

之前直接就可以用,而现在,启动的端口是8080,明显没有加载到bootstrap.properties文件,我以为我的文件名字写错了,核对了几次,确认无误,我猜想估计是bootstramp.properties配置文件没有生效。

之前的版本:

  • spring boot 2.3.1.RELEASE
  • spring cloud Hoxton.SR4

当前版本:

  • spring boot 2.4.2
  • spring cloud 2020.0.1

查找原因

根据上面出现的问题,我使用百度搜索了下,大概的原因知道了:从Spring Boot 2.4版本开始,配置文件加载方式进行了重构。

另外也有配置的默认值变化,如下:

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

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

传统解决方案

其实官网说得很明白。看下面这段:

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 is spring.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 the spring.cloud.config.uri bootstrap configuration property) and initializes Spring Environment 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 in spring.cloud.config.uri (it defaults to "http://localhost:8888").

两个关键点:

1、加一个依赖:spring-cloud-starter-bootstrap

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

2、加一个配置:spring.cloud.config.uri

bootstrap.properties

# 应用名称
spring.application.name=erwin-cloud-user
# 启用环境
spring.profiles.active=dev

# 配置文件
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

解决方案

现在,你只需要这样:

application.properties

# 应用名称
spring.application.name=erwin-cloud-user
# 启用环境
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}

到此这篇关于SpringCloud2020 bootstrap 配置文件失效的解决方法的文章就介绍到这了,更多相关SpringCloud2020 bootstrap 配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring如何自定义XML配置扩展

    Spring如何自定义XML配置扩展

    这篇文章主要介绍了Spring如何自定义XML配置扩展,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • java中把汉字转换成简拼的实现代码

    java中把汉字转换成简拼的实现代码

    本篇文章是对在java中把汉字转换成简拼的实现方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Java几个重要的关键字详析

    Java几个重要的关键字详析

    这篇文章主要介绍了Java几个重要的关键字详析,文章围绕主题展开详细的内容介绍,具有一定的参考一下,需要的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-07-07
  • Spring Boot 启动、停止、重启、状态脚本

    Spring Boot 启动、停止、重启、状态脚本

    今天给大家分享Spring Boot 项目脚本(启动、停止、重启、状态),通过示例代码给大家介绍的非常详细,需要的朋友参考下吧
    2021-06-06
  • Java如何对方法进行调用详解

    Java如何对方法进行调用详解

    今天给大家整理了Java如何对方法进行调用,文中有非常详细的介绍及代码示例,对正在学习java的小伙伴们很有帮助,需要的朋友可以参考下
    2021-06-06
  • ​​​​​​​Spring多租户数据源管理 AbstractRoutingDataSource

    ​​​​​​​Spring多租户数据源管理 AbstractRoutingDataSource

    本文技术了​​​​​​​Spring多租户数据源管理 AbstractRoutingDataSource,下文详细内容介绍,需要的小伙伴可以参考一下
    2022-05-05
  • 解决logback-classic 使用testCompile的打包问题

    解决logback-classic 使用testCompile的打包问题

    这篇文章主要介绍了解决logback-classic 使用testCompile的打包问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Java设计模式之责任链模式详解

    Java设计模式之责任链模式详解

    客户端发出一个请求,链上的对象都有机会来处理这一请求,而客户端不需要知道谁是具体的处理对象。这样就实现了请求者和接受者之间的解耦,并且在客户端可以实现动态的组合职责链。使编程更有灵活性
    2022-07-07
  • springboot实现增加黑名单和白名单功能

    springboot实现增加黑名单和白名单功能

    本文主要介绍了springboot实现增加黑名单和白名单功能,就是单纯的实现filter,然后注册到springboot里面,在filter里面进行黑白名单的筛选,感兴趣的可以了解一下
    2024-05-05
  • 浅谈Java线程Thread.join方法解析

    浅谈Java线程Thread.join方法解析

    本篇文章主要介绍了浅谈Java线程Thread.join方法解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01

最新评论