• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

解决nacos升级spring cloud 2020.0无法使用bootstrap.yml的问题

java 搞代码 4年前 (2022-01-05) 50次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了解决nacos升级spring cloud 2020.0无法使用bootstrap.yml的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

nacos升级spring cloud 2020.0无法使用bootstrap.yml

之前用spring cloud整合nacos,需要一个bootstrap.yml作为spring启动的初始化配置

bootstrap.yml内容大概如下:

 spring: application: # 应用名称 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默认设置为false不启用:

https://github.com/spring-cloud/spring-cloud-release/wiki/Spring-Cloud-2020.0-Release-Notes#breaking-changes

源码部分如下:

 /* * 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 c<i style="color:transparent">来源gaodai$ma#com搞$$代**码网</i>opy 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的时候默认设置成了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启动参数中增加-Dspring.cloud.bootstrap.enabled=true,或者增加spring-cloud-starter-bootstrap依赖,至于spring cloud alibaba nacos后续是否能支持application.yml,目前alibaba官方还没有回复

nacos避坑指南!nacos 配置中心无法加载bootstrap.yml/properties配置文件

在使用nacos时引入以下两个依赖:

 <!--服务注册/发现--> com.alibaba.cloudspring-cloud-starter-alibaba-nacos-discovery<!--配置中心来做配置管理--> com.alibaba.cloudspring-cloud-starter-alibaba-nacos-config

错误异常信息:

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配置不生效,加入以下依赖 --> org.springframework.cloudspring-cloud-starter-bootstrap3.0.1

配置文件:

在启动服务后的第一条日志信息:

上面的 【conpon-service.properties】添加到nacos中

补充一下:nacos配置修改后,项目中要动态实时更新,项目中需要动态配置,在congtroller中加入一个注解

 @RefreshScope // 动态刷新nacos配置

即可。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是解决nacos升级spring cloud 2020.0无法使用bootstrap.yml的问题的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:解决nacos升级spring cloud 2020.0无法使用bootstrap.yml的问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址