javascript
Spring Cloud——Spring Cloud Alibaba 2021 Nacos Config bootstrap 配置文件失效解决方案
基本概念
微服務是基于Spring Cloud框架搭建的,Spring Cloud Config作為服務配置中心。
業務服務只配置服務名稱、啟用環境和config的URL地址,其他都配置在配置中心,例如服務端口、服務注冊中心地址等。可在開發環境(dev)、測試環境(test)和生產環境(prod)分別配置。
所以預想的啟動流程是:先加載配置文件,再啟動服務。
添加配置文件:bootstrap.properties或者bootstrap.yml。
環境配置
Spring Boot 2.4.5
Spring Cloud 2020.0.0
Spring Cloud Alibaba 2021.1
問題描述
沒有加載到bootstrap.properties文件,bootstrap.yml同樣無效。
問題分析
從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;}官方文檔
https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-first-bootstrap
解決方案
方法一:添加依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency>原來的bootstrap.properties或者bootstrap.yml不需要任何修改。
方法二:設置啟動參數或者環境變量
IDEA環境: select Run/Debug Configurations > Override parameters 中添加 spring.cloud.bootstrap.enabled=true
命令列: java -jar -Dspring.cloud.bootstrap.enabled=true gateway.jar
參考文章
Spring Cloud 2020 bootstrap 配置文件失效
Spring Cloud 2020.0.0 版本 bootstrap.yml 失效的原因與解決
總結
以上是生活随笔為你收集整理的Spring Cloud——Spring Cloud Alibaba 2021 Nacos Config bootstrap 配置文件失效解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《移动应用开发》实验报告——轮播图
- 下一篇: Element UI——表单验证输入的数