解決nacos升級spring cloud 2020.0無法使用bootstrap.yml的問題
nacos升級spring cloud 2020.0無法使用bootstrap.yml
之前用spring cloud整合nacos,需要一個bootstrap.yml作為spring啟動的初始化配置
bootstrap.yml內(nèi)容大概如下:
spring:
application:
# 應(yīng)用名稱
name: xxx
profiles:
active: dev
cloud:
nacos:
config:
file-extension: yml
server-addr: localhost:8848
username: nacos
password: nacos
把spring cloud升級到2020.0.x以后,nacos就讀不到bootstrap.yml了
spring官方其實在change note中說明了,在新版中將spring.cloud.bootstrap.enabled默認設(shè)置為false不啟用:
源碼部分如下:
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.util;
import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils;
public abstract class PropertyUtils {
/**
* Property name for checking if bootstrap is enabled.
*/
public static final String BOOTSTRAP_ENABLED_PROPERTY = "spring.cloud.bootstrap.enabled";
/**
* Property name for spring boot legacy processing.
*/
public static final String USE_LEGACY_PROCESSING_PROPERTY = "spring.config.use-legacy-processing";
/**
* Property name for bootstrap marker class name.
*/
public static final String MARKER_CLASS = "org.springframework.cloud.bootstrap.marker.Marker";
/**
* Boolean if bootstrap marker class exists.
*/
public static final boolean MARKER_CLASS_EXISTS = ClassUtils.isPresent(MARKER_CLASS, null);
private PropertyUtils() {
throw new UnsupportedOperationException("unable to instatiate utils class");
}
/**
* 判斷是否啟用bootstrap.yml的時候默認設(shè)置成了false
*/
public static boolean bootstrapEnabled(Environment environment) {
return environment.getProperty(BOOTSTRAP_ENABLED_PROPERTY, Boolean.class, false) || MARKER_CLASS_EXISTS;
}
public static boolean useLegacyProcessing(Environment environment) {
return environment.getProperty(USE_LEGACY_PROCESSING_PROPERTY, Boolean.class, false);
}
}
解決方案是在vm啟動參數(shù)中增加-Dspring.cloud.bootstrap.enabled=true,或者增加spring-cloud-starter-bootstrap依賴,至于spring cloud alibaba nacos后續(xù)是否能支持application.yml,目前alibaba官方還沒有回復(fù)
nacos避坑指南!nacos 配置中心無法加載bootstrap.yml/properties配置文件
在使用nacos時引入以下兩個依賴:
<!--服務(wù)注冊/發(fā)現(xiàn)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--配置中心來做配置管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
錯誤異常信息:
com.alibaba.nacos.api.exception.NacosException: java.lang.reflect.InvocationTargetException
......
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_211]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_211]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_211]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_211]
at com.alibaba.nacos.api.config.ConfigFactory.createConfigService(ConfigFactory.java:42) ~[nacos-api-1.1.1.jar:na]
... 34 common frames omitted
Caused by: com.alibaba.nacos.api.exception.NacosException: endpoint is blank
解決方案:加入以下依賴即可
<!-- 若bootstrap配置不生效,加入以下依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.1</version>
</dependency>
配置文件:


在啟動服務(wù)后的第一條日志信息:
上面的 【conpon-service.properties】添加到nacos中


補充一下:nacos配置修改后,項目中要動態(tài)實時更新,項目中需要動態(tài)配置,在congtroller中加入一個注解
@RefreshScope // 動態(tài)刷新nacos配置
即可。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Spring Cloud 配置中心多環(huán)境配置bootstrap.yml的實現(xiàn)方法
- 解決springboot與springcloud版本兼容問題(附版本兼容表)
- 將Springboot項目升級成Springcloud項目的圖文教程
- SpringCloud解決Feign異步回調(diào)問題(SpringBoot+Async+Future實現(xiàn))
- SpringBoot項目改為SpringCloud項目使用nacos作為注冊中心的方法
- SpringBoot(cloud)自動裝配bean找不到類型的問題
- springboot集成springCloud中g(shù)ateway時啟動報錯的解決
- 從零開始搭建springboot+springcloud+mybatis本地項目全過程(圖解)
- 詳解SpringBoot與SpringCloud的版本對應(yīng)詳細版
- Spring、Spring?Boot、Spring?Cloud?的區(qū)別與聯(lián)系分析
相關(guān)文章
使用@RequestBody配合@Valid校驗入?yún)?shù)
這篇文章主要介紹了使用@RequestBody配合@Valid校驗入?yún)?shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot微服務(wù)打包Docker鏡像流程解析
這篇文章主要介紹了Springboot微服務(wù)打包Docker鏡像流程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
基于Java實現(xiàn)互聯(lián)網(wǎng)實時聊天系統(tǒng)(附源碼)
Netty?是一個利用?Java?的高級網(wǎng)絡(luò)的能力,隱藏其背后的復(fù)雜性而提供一個易于使用的?API?的客戶端/服務(wù)器框架。本文將利用它實現(xiàn)互聯(lián)網(wǎng)實時聊天系統(tǒng),感興趣的可以了解一下2022-09-09
Spring配置shiro時自定義Realm中屬性無法使用注解注入的解決辦法
今天小編就為大家分享一篇關(guān)于Spring配置shiro時自定義Realm中屬性無法使用注解注入的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Java進階學(xué)習(xí):網(wǎng)絡(luò)服務(wù)器編程
Java進階學(xué)習(xí):網(wǎng)絡(luò)服務(wù)器編程...2006-12-12
Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu)
這篇文章主要介紹了Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu),本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09

