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 配置内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
相关文章
- 这篇文章主要介绍了Bootstrap Table使用整理(二)的相关资料,需要的朋友可以参考下...2017-06-15
基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍
这篇文章主要介绍了基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍 的相关资料,需要的朋友可以参考下...2016-05-14- 这篇文章主要为大家分享了Bootstrap教程JS插件滚动监听学习笔记,内容很详细,感兴趣的小伙伴们可以参考一下...2016-05-20
- 这篇文章主要教大家如何利用Bootstrap3制作图片轮播效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-05-14
- 在网页中,经常见到进度条效果,那么这些个性的进度条组件效果是怎么实现的呢,下面脚本之家小编给大家分享Bootstrap进度条组件知识详解,感兴趣的朋友要求学习吧...2016-05-04
JS树形菜单组件Bootstrap TreeView使用方法详解
这篇文章主要为大家详细介绍了js组件Bootstrap TreeView使用方法,本文一部分针对于bootstrap的treeview的实践,另一部分是介绍自己写的树形菜单,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-09- 这篇文章主要为大家详细介绍了Bootstrap树形控件使用方法,感兴趣的小伙伴们可以参考一下...2016-01-29
- 这篇文章主要为大家详细介绍了Bootstrap选项卡学习笔记,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-02-19
基于BootStrap的Metronic框架实现页面链接收藏夹功能按钮移动收藏记录(使用Sortable进行拖动排序)
这篇文章主要介绍了基于BootStrap的Metronic框架实现页面链接收藏夹功能按钮移动收藏记录(使用Sortable进行拖动排序)的相关资料,非常不错,需要的朋友可以参考下...2016-09-01- 这篇文章主要为大家详细解析了BootStrap图片样式、辅助类样式和CSS组件源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-23
基于Bootstrap的Metronic框架实现页面链接收藏夹功能
本文给大家介绍基于Metronic的Bootstrap开发框架实现页面链接收藏夹功能,非常不错,具有参考借鉴价值,感兴趣的朋友一起学习吧...2016-09-01- 本文通过实例代码给大家介绍了bootstrap导航条效果,代码简单易懂,非常不错,具有参考借鉴价值,需要的的朋友参考下吧...2017-05-22
- Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架,主要基于 HTML、CSS、JAVASCRIPT 的。接下来通过本文给大家介绍BootStrap 附加导航组件的知识,感兴趣的朋友一起学习吧...2016-07-29
- 这篇文章主要介绍了Bootstrap Table使用整理(四)之工具栏的相关资料,需要的朋友可以参考下...2017-06-15
- 这篇文章主要介绍了BootStrap和jQuery相结合实现可编辑表格的相关资料,需要的朋友可以参考下...2016-04-23
Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法
这篇文章主要介绍了Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-06-24基于Bootstrap实现Material Design风格表单插件 附源码下载
Jquery Material Form Plugin是一款基于Bootstrap的Material Design风格的jQuery表单插件。这篇文章主要介绍了基于Bootstrap的Material Design风格表单插件附源码下载,感兴趣的朋友参考下...2016-04-19BootStrap Progressbar 实现大文件上传的进度条的实例代码
这篇文章主要介绍了BootStrap Progressbar 实现大文件上传的进度条的实例代码的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-07-01BootStrap学习系列之Bootstrap Typeahead 组件实现百度下拉效果(续)
这篇文章主要介绍了BootStrap学习系列之Bootstrap Typeahead 组件实现百度下拉效果的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-07-25- bootstrap布局是应用得很广泛的一种网页布局方法,下面通过本文给大家介绍bootstrap页面缩小变形的快速解决办法,需要的朋友参考下吧...2017-02-08