SpringCloud版本升级后bootstrap.yml配置不生效

2022-11-24 20:49:29 浏览数 (1)

问题描述

由于springcloud gateway网关漏洞需要升级,想着直接把整个SpringCloud和SpringCloud Alibaba整体做个升级。升级后发现bootstrap.yml配置文件配置不生效。 之前版本:

代码语言:javascript复制
<spring-boot.version>2.3.4.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version>
<nacos.version>1.4.0</nacos.version>

当前版本:

代码语言:javascript复制
<spring-boot.version>2.6.3</spring-boot.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.1.0</spring-cloud-alibaba.version>
<nacos.version>1.4.2</nacos.version>

PS:升级需要注意nacos版本也要对应的版本升级,不然会报如下错误。具体版本对应关系参考SpringCloud Alibaba官方说明:https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明

代码语言:javascript复制
java.lang.NoClassDefFoundError: com/alibaba/nacos/client/logging/NacosLogging
	at com.alibaba.cloud.nacos.discovery.logging.NacosLoggingListener.onApplicationEvent(NacosLoggingListener.java:46)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
...
Caused by: java.lang.ClassNotFoundException: com.alibaba.nacos.client.logging.NacosLogging
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

问题原因

从Spring Boot 2.4版本开始,配置文件加载方式进行了重构。 另外也有配置的默认值变化,原来默认启用 true 现在变更为 false 如下: version:2.4之前

代码语言:javascript复制
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)) {
        }
    }
}

version:2.4.2

代码语言:javascript复制
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-data-import pom文件中引入如下配置

代码语言:javascript复制
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

指定运行参数 spring.cloud.bootstrap.enabled 值为 true

代码语言:javascript复制
IDEA中:
spring.cloud.bootstrap.enabled=true

启动命令中: 
java -jar -Dspring.cloud.bootstrap.enabled=true xxx.jar

图中两个配置二选一即可。

其他问题

升级后FallbackFactory路径由feign.hystrix.FallbackFactory;调整成org.springframework.cloud.openfeign.FallbackFactory,修改成org.springframework.cloud.openfeign.FallbackFactory后启动报如下错误。

代码语言:javascript复制
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.zbsz.cloud.system.ZbszSystemApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/cloud/openfeign/FallbackFactory.class] cannot be opened because it does not exist
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]

我把openfeign改回之前版本先这么处理后续在研究org.springframework.cloud.openfeign.FallbackFactory。

代码语言:javascript复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

0 人点赞