Spring Boot整合Nacos时遇到 Failed to rename context [logback] as [nacos] 的解决办法
问题概述在进行项目中通过Spring Boot整合Nacos服务注册和服务发现时,遇到个梗,启动时打印出一串错误日志信息 “ Failed to rename context [logback] as [nacos] java.lang.IllegalStateException: Context has been already given a name ” ,但是不会影响项目的正常运行和使用,
问题概述
在进行项目开发过程中,通过Spring Boot整合Alibaba Nacos的服务注册与发现和配置中心时,遇到个梗,启动时打印出一串错误日志信息 “ Failed to rename context [logback] as [nacos] java.lang.IllegalStateException: Context has been already given a name ” ,但是不会影响项目的正常运行和使用,
如下图:
具体错误信息如下:
2021-04-21 18:03:57.947 INFO [main]s.d.s.web.PropertySourcedRequestMappingHandlerMapping.initHandlerMethods:69 -Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]
2021-04-21 18:03:57.982 INFO [main]o.s.scheduling.concurrent.ThreadPoolTaskExecutor.initialize:181 -Initializing ExecutorService 'applicationTaskExecutor'
2021-04-21 18:03:58.250 INFO [main]o.s.scheduling.concurrent.ThreadPoolTaskScheduler.initialize:181 -Initializing ExecutorService 'Nacso-Watch-Task-Scheduler'
18:03:58,258 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@748aa7dc - URL [jar:file:/D:/LOCAL_INSTALLER/apache-maven-3.6.3/.m2/repository/com/alibaba/nacos/nacos-client/1.1.4/nacos-client-1.1.4.jar!/nacos-logback.xml] is not of type file
18:03:58,259 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
18:03:58,259 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [jar:file:/D:/LOCAL_INSTALLER/apache-maven-3.6.3/.m2/repository/com/alibaba/nacos/nacos-client/1.1.4/nacos-client-1.1.4.jar!/nacos-logback.xml]
18:03:58,259 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 30 seconds
18:03:58,260 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as [nacos]
18:03:58,260 |-ERROR in ch.qos.logback.classic.joran.action.ContextNameAction - Failed to rename context [logback] as [nacos] java.lang.IllegalStateException: Context has been already given a name
at java.lang.IllegalStateException: Context has been already given a name
at at ch.qos.logback.core.ContextBase.setName(ContextBase.java:194)
at at ch.qos.logback.classic.LoggerContext.setName(LoggerContext.java:107)
at at ch.qos.logback.classic.joran.action.ContextNameAction.body(ContextNameAction.java:31)
at at ch.qos.logback.core.joran.spi.Interpreter.callBodyAction(Interpreter.java:289)
at at ch.qos.logback.core.joran.spi.Interpreter.characters(Interpreter.java:172)
at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:57)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)
at at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
at at com.alibaba.nacos.client.logging.logback.LogbackNacosLogging.loadConfiguration(LogbackNacosLogging.java:44)
at at com.alibaba.nacos.client.utils.LogUtils.<clinit>(LogUtils.java:47)
at at com.alibaba.nacos.client.naming.utils.InitUtils$1.call(InitUtils.java:56)
at at com.alibaba.nacos.client.naming.utils.InitUtils$1.call(InitUtils.java:52)
at at com.alibaba.nacos.client.utils.TemplateUtils.stringEmptyAndThenExecute(TemplateUtils.java:43)
at at com.alibaba.nacos.client.naming.utils.InitUtils.initNamespaceForNaming(InitUtils.java:52)
at at com.alibaba.nacos.client.naming.NacosNamingService.init(NacosNamingService.java:88)
at at com.alibaba.nacos.client.naming.NacosNamingService.<init>(NacosNamingService.java:84)
at at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at at com.alibaba.nacos.api.naming.NamingFactory.createNamingService(NamingFactory.java:45)
at at com.alibaba.nacos.api.NacosFactory.createNamingService(NacosFactory.java:76)
at at com.alibaba.cloud.nacos.NacosDiscoveryProperties.namingServiceInstance(NacosDiscoveryProperties.java:486)
at at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.<init>(NacosServiceRegistry.java:47)
at at com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration.nacosServiceRegistry(NacosServiceRegistryAutoConfiguration.java:51)
at at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at at java.lang.reflect.Method.invoke(Method.java:498)
解决办法
经过了一系列的烧脑摸索,一无所获,后,在官方文档上找到了相关问题的阐述(不是解决办法),然后再结合以往经验,终于把问题给解决了。
原来,在基于Spring Boot框架项目开发过程中,集成了Alibaba Nacos的服务注册与发现和配置中心,
因为Spring Boot本身已经集成了日志框架Logback,而在Alibaba Nacos中又集成了一套Logback框架(我顶你个肺,Alibaba Nacos给我们开发者们留了这么大一个坑),
而Alibaba Nacos中的Logback加载要优先于项目自身的Logback框架,在一个项目中其context_name只能定义一次,
所以,在项目启动时,Alibaba Nacos的Logback先加载完成后,再加载项目本身的Logback时就出现了冲突,报错误日志 “ Failed to rename context [logback] as [nacos] java.lang.IllegalStateException: Context has been already given a name ” ,但是不影响使用,
虽然不影响使用,但是作为一个传统的老码畜,看着这个错误日志,实在不爽,所以博主把它个消灭了,
解决这个,只要禁用掉其中一个Logback就可以了,一般禁用外部框架夹带的Logback即可 ,在启动类中加入如下段代码即可:
System.setProperty("nacos.logging.default.config.enabled","false");
完整示例:
@EnableDiscoveryClient
@EnableFeignClients
@EnableSwagger2
@MapperScan({"com....*.mapper","com.....system.*.mapper","com.....*.mapper"})
@SpringBootApplication(scanBasePackages = {"com.....","com....."})
public class ApplicationName {
public static void main(String[] args) {
System.setProperty("nacos.logging.default.config.enabled","false");
SpringApplication.run(ApplicationName.class, args);
}
}
解决后就能成功启动了,如下图;
参考文献:
好了,关于 Spring Boot整合Nacos时遇到Failed to rename context [logback] as [nacos] 的解决办法 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。
作 者: | 华 仔 |
联系作者: | who.seek.me@java98k.vip |
来 源: | CSDN (Chinese Software Developer Network) |
原 文: | https://blog.csdn.net/Hello_World_QWP/article/details/115969198 |
版权声明: | 本文为博主原创文章,请在转载时务必注明博文出处! |
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)