解决nacos升级spring cloud 2020.0无法使用bootstrap.yml的问题
作者:大造梦家 发布时间:2021-12-02 19:44:29
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 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的时候默认设置成了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时引入以下两个依赖:
<!--服务注册/发现-->
<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>
配置文件:
在启动服务后的第一条日志信息:
上面的 【conpon-service.properties】添加到nacos中
补充一下:nacos配置修改后,项目中要动态实时更新,项目中需要动态配置,在congtroller中加入一个注解
@RefreshScope // 动态刷新nacos配置
即可。
来源:https://blog.csdn.net/weixin_39841589/article/details/117248897
猜你喜欢
- 已知字符串“aabbbcddddeeffffghijklmnopqrst”编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2)/**
- paras.xml文件<?xml version="1.0" encoding="UTF-8"
- 前言今天遇到一个问题,我们有个ip.dat二进制文件,通过里面内容可以解析ip所在的地域信息,本地单元测试都是OK的,部署到测试环境后,发现
- Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullP
- 配置文件<!-- 文件上传 --> <bean id="multipartResolver" clas
- 1.概览该教程中,我将向你展示:如何在测试时设置spring boot 日志级别。虽然我们可以在测试通过时忽略日志,但是如果需要诊断失败的测
- 此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf
- 引言java中的Math.random()是一个在[0,1)范围等概率返回double数值类型的算法,基于此函数,我们来延申一些随机概率算法
- 前言:在纯 Java 代码里 我们一般都会用class.getResource(String name) 或者 class.getClass
- 近期用到了一位师兄写的C++程序,总体功能良好。使用不同的数据测试,发现了一个明显的缺点:大数据量下,预处理过程耗时很长。中科院的某计算集群
- 属性问题引入前面我们已经在父工程中的dependencyManagement标签中对项目中所使用的jar包版本进行了统一的管理,但是如果在标
- java Mybatis存进时间戳封装了一个实体类,里面有个字段 Integer createTime。要利用这个实体类将一个时间戳存进数据
- 在上一篇文章《驱动开发:内核字符串转换方法》中简单介绍了内核是如何使用字符串以及字符串之间的转换方法,本章将继续探索字符串的拷贝与比较,与应
- Environment的中文意思是环境,它表示整个spring应用运行时的环境信息,它包含两个关键因素profilespropertiesp
- 一.为什么要用线程池先来看个简单的例子1.直接new Thread的情况:public static void main(String[]
- import java.io.BufferedReader; import java.io.IOException;  
- 定义:/** * @author Administrator * @project: TestOne * @package: PACKAGE
- 树的同构备忘!定义:给定两棵树r1、r2,如果r1可以通过若干次的左子树和右子树互换,使之与r2完全相同,这说明两者同构。举例树的构造树可以
- 1、mybatis-plus @DS实现动态切换数据源原理首先mybatis-plus使用com.baomidou.dynamic.data
- 1、右值1.1 简介首先区分一下左右值:左值是指存储在内存中、有明确存储地址(可取地址)的数据;右值是指可以提供数据值的数据(不可取地址)如