问题描述
由于 springcloud gateway 网关漏洞需要升级,想着直接把整个 SpringCloud 和 SpringCloud Alibaba 整体做个升级。升级后发现 bootstrap.yml 配置文件配置不生效。之前版本:
<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>
复制代码
当前版本:
<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/版本说明
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 之前
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
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-importpom 文件中引入如下配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
复制代码
指定运行参数 spring.cloud.bootstrap.enabled 值为 true
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 后启动报如下错误。
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。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
复制代码
本文内容到此结束了,
如有收获欢迎点赞👍收藏💖关注✔️,您的鼓励是我最大的动力
如有错误❌疑问💬欢迎各位指出。
主页:共饮一杯无的博客汇总👨💻
保持热爱,奔赴下一场山海。🏃🏃🏃
评论