前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud 2020 bootstrap 配置文件失效

Spring Cloud 2020 bootstrap 配置文件失效

原创
作者头像
冯文议
修改2021-02-07 10:51:21
2.1K0
修改2021-02-07 10:51:21
举报
文章被收录于专栏:编程笔记编程笔记

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
复制代码

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

bootstrap.properties

代码语言:javascript
复制
# 应用名称
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

代码语言:javascript
复制
# 应用名称
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}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景介绍
  • 问题
  • 查找原因
  • 传统解决方案
  • 解决方案
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档