日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Cloud——Spring Cloud Alibaba 2021 Nacos Config bootstrap 配置文件失效解决方案

發布時間:2024/10/5 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 配置文件失效解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。