当前位置:   article > 正文

Logback于Nacos的Logback冲突导致的项目启动失败 ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CO_项目中的logback于nacos的logback冲突导致的项目启动失败 error in ch.q

项目中的logback于nacos的logback冲突导致的项目启动失败 error in ch.qos.logbac
            <div id="content_views" class="htmledit_views">
                <h2><a name="t0"></a>问题:&nbsp;</h2> 
  • 1
  • 2

环境 :  Nacos 2.2.1   ,   Spring-cloud   Hoxton.SR1 , com.alibaba.cloud.version  2.2.8 (留意这个版本)

        最近新搭建了一次虚拟机, 然后重新部署了Nacos , 同时也在一个新项目里引入了Nacos 的服务注册功能 , 以下是服务Pom版本管理配置


 
 
  1. <properties>
  2. <java.version>1.8 </java.version>
  3. <file_encoding>UTF-8 </file_encoding>
  4. <spring-cloud.version>Hoxton.SR1 </spring-cloud.version>
  5. <com.alibaba.cloud.version>2.2.8.RELEASE </com.alibaba.cloud.version>
  6. </properties>
  7. <dependencyManagement>
  8. <dependencies>
  9. <!--spring boot 2.2.2-->
  10. <dependency>
  11. <groupId>org.springframework.boot </groupId>
  12. <artifactId>spring-boot-dependencies </artifactId>
  13. <version>2.2.2.RELEASE </version>
  14. <type>pom </type>
  15. <scope>import </scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.cloud </groupId>
  19. <artifactId>spring-cloud-dependencies </artifactId>
  20. <version>${spring-cloud.version} </version>
  21. <type>pom </type>
  22. <scope>import </scope>
  23. </dependency>
  24. <!--spring cloud alibaba 2.2.8.RELEASE-->
  25. <dependency>
  26. <groupId>com.alibaba.cloud </groupId>
  27. <artifactId>spring-cloud-alibaba-dependencies </artifactId>
  28. <version>${com.alibaba.cloud.version} </version>
  29. <type>pom </type>
  30. <scope>import </scope>
  31. </dependency>
  32. </dependencies>
  33. </dependencyManagement>
  • 1

然后再实际使用模块pom文件中引入服务注册和配置管理 


 
 
  1. <dependencies>
  2. <!-- Nacos依赖-->
  3. <dependency>
  4. <groupId>com.alibaba.cloud </groupId>
  5. <artifactId>spring-cloud-starter-alibaba-nacos-discovery </artifactId>
  6. </dependency>
  7. <!-- 在没有引入一下pom时候服务注册是正常的 -->
  8. <dependency>
  9. <groupId>com.alibaba.cloud </groupId>
  10. <artifactId>spring-cloud-starter-alibaba-nacos-config </artifactId>
  11. </dependency>
  12. </dependencies>
  • 1

application.ymal 的配置如下 


 
 
  1. spring:
  2. profiles: dev
  3. cloud:
  4. nacos:
  5. username: nacos
  6. password: nacos
  7. discovery:
  8. server-addr: 192.168.30.128:8848
  9. namespace: 281222d0-6848-4017-8768-c81df219e12
  10. config:
  11. bootstrap:
  12. enabled: true
  13. server-addr: ${spring.cloud.nacos.discovery.server-addr}
  14. file-extension: yml
  15. namespace: 281222d0-6848-4017-8768-c81df219e12
  16. auto-refresh: true
  • 1

此时启动项目问题就显露了 , 在没有加上  spring-cloud-starter-alibaba-nacos-config 这个maven配置的时候服务是可以正常启动的 , 然后加了之后 服务根本就启动不起来 , 报错如下 


 
 
  1. 2023- 03- 31 10: 30: 06.710 ERROR 40924 --- [ main]o.s.boot.SpringApplication : 826 : Application run failed
  2. java.lang.IllegalStateException: Logback configuration error detected:
  3. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - 'File' option has the same value "C:\Users\huang\logs/nacos/config.log" as that given for appender [CONFIG_LOG_FILE] defined earlier.
  4. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
  5. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - For more information, please visit http: //logback.qos.ch/codes.html#earlier_fa_collision
  6. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - 'File' option has the same value "C:\Users\huang\logs/nacos/naming.log" as that given for appender [NAMING_LOG_FILE] defined earlier.
  7. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
  8. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - For more information, please visit http: //logback.qos.ch/codes.html#earlier_fa_collision
  9. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - 'File' option has the same value "C:\Users\huang\logs/nacos/remote.log" as that given for appender [REMOTE_LOG_FILE] defined earlier.
  10. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
  11. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - For more information, please visit http: //logback.qos.ch/codes.html#earlier_fa_collision
  12. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - 'File' option has the same value "C:\Users\huang\logs/nacos/config.log" as that given for appender [CONFIG_LOG_FILE] defined earlier.
  13. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
  14. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - For more information, please visit http: //logback.qos.ch/codes.html#earlier_fa_collision
  15. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - 'File' option has the same value "C:\Users\huang\logs/nacos/naming.log" as that given for appender [NAMING_LOG_FILE] defined earlier.
  16. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
  17. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - For more information, please visit http: //logback.qos.ch/codes.html#earlier_fa_collision
  18. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - 'File' option has the same value "C:\Users\huang\logs/nacos/remote.log" as that given for appender [REMOTE_LOG_FILE] defined earlier.
  19. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
  20. ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - For more information, please visit http: //logback.qos.ch/codes.html#earlier_fa_collision
  21. at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java: 169)
  22. at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java: 80)
  23. at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java: 60)
  24. at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java: 118)
  25. at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java: 313)
  26. at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java: 288)
  27. at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java: 246)
  28. at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java: 223)
  29. at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java: 172)
  30. at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java: 165)
  31. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java: 139)
  32. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java: 127)
  33. at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java: 76)
  34. at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java: 53)
  35. at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java: 345)
  36. at org.springframework.boot.SpringApplication.run(SpringApplication.java: 308)
  37. at org.springframework.boot.SpringApplication.run(SpringApplication.java: 1226)
  38. at org.springframework.boot.SpringApplication.run(SpringApplication.java: 1215)
  39. at com.ko.assy.db.ms.KoAssyDbApplication.main(KoAssyDbApplication.java: 29)
  • 1

看表象 , 是Logback 引起的问题 , 初步怀疑是可能因为冲突了 , 因为我本地也使用了 Logback 的配置 ,  Logbak内容入如下


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration scan="true" scanPeriod="10 seconds">
  3. <contextName>logback </contextName>
  4. <springProperty scope="context" name="logDir" source="log.dir" defaultValue=""/>
  5. <property name="log.path" value="${logDir}"/>
  6. <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
  7. <conversionRule conversionWord="wex"
  8. converterClass= "org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
  9. <conversionRule conversionWord="wEx"
  10. converterClass= "org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
  11. <!-- 彩色日志格式 -->
  12. <property name="CONSOLE_LOG_PATTERN"
  13. value= "${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint}%clr(%-40.40logger{39}:%4line){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
  14. <!--1. 输出到控制台-->
  15. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  16. <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
  17. <!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
  18. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  19. <level>DEBUG </level>
  20. </filter>
  21. <encoder>
  22. <Pattern>${CONSOLE_LOG_PATTERN} </Pattern>
  23. <!-- 设置字符集 -->
  24. <charset>UTF-8 </charset>
  25. </encoder>
  26. </appender>
  27. <appender name="ALL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  28. <file>${log.path}/web-all.log </file>
  29. <encoder>
  30. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%15.15thread]%-40.40logger{39}:%4line: %msg%n </pattern>
  31. <charset>UTF-8 </charset>
  32. </encoder>
  33. <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
  34. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  35. <!-- 日志归档 -->
  36. <fileNamePattern>${log.path}/web-all-%d{yyyy-MM-dd}.%i.log </fileNamePattern>
  37. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  38. <maxFileSize>100MB </maxFileSize>
  39. </timeBasedFileNamingAndTriggeringPolicy>
  40. <!--日志文档保留天数-->
  41. <maxHistory>15 </maxHistory>
  42. </rollingPolicy>
  43. </appender>
  44. <appender name="TRACE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  45. <file>${log.path}/web-trace.log </file>
  46. <encoder>
  47. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%15.15thread]%-40.40logger{39}:%4line: %msg%n </pattern>
  48. <charset>UTF-8 </charset>
  49. </encoder>
  50. <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
  51. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  52. <!-- 日志归档 -->
  53. <fileNamePattern>${log.path}/web-trace-%d{yyyy-MM-dd}.%i.log </fileNamePattern>
  54. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  55. <maxFileSize>100MB </maxFileSize>
  56. </timeBasedFileNamingAndTriggeringPolicy>
  57. <!--日志文档保留天数-->
  58. <maxHistory>15 </maxHistory>
  59. </rollingPolicy>
  60. </appender>
  61. <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  62. <file>${log.path}/web-warn.log </file>
  63. <encoder>
  64. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%15.15thread]%-40.40logger{39}:%4line: %msg%n </pattern>
  65. <charset>UTF-8 </charset>
  66. </encoder>
  67. <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
  68. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  69. <!-- 日志归档 -->
  70. <fileNamePattern>${log.path}/web-warn-%d{yyyy-MM-dd}.%i.log </fileNamePattern>
  71. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  72. <maxFileSize>100MB </maxFileSize>
  73. </timeBasedFileNamingAndTriggeringPolicy>
  74. <!--日志文档保留天数-->
  75. <maxHistory>15 </maxHistory>
  76. </rollingPolicy>
  77. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  78. <level>WARN </level>
  79. <onMatch>ACCEPT </onMatch>
  80. <onMismatch>DENY </onMismatch>
  81. </filter>
  82. </appender>
  83. <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  84. <file>${log.path}/web-error.log </file>
  85. <encoder>
  86. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%15.15thread]%-40.40logger{39}:%4line: %msg%n </pattern>
  87. <charset>UTF-8 </charset>
  88. </encoder>
  89. <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
  90. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  91. <!-- 日志归档 -->
  92. <fileNamePattern>${log.path}/web-error-%d{yyyy-MM-dd}.%i.log </fileNamePattern>
  93. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  94. <maxFileSize>100MB </maxFileSize>
  95. </timeBasedFileNamingAndTriggeringPolicy>
  96. <!--日志文档保留天数-->
  97. <maxHistory>15 </maxHistory>
  98. </rollingPolicy>
  99. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  100. <level>ERROR </level>
  101. <onMatch>ACCEPT </onMatch>
  102. <onMismatch>DENY </onMismatch>
  103. </filter>
  104. </appender>
  105. <!-- 忽略Nacos配置中心长轮询日志 -->
  106. <logger name="com.alibaba.nacos.client.config.impl" level="WARN"/>
  107. <!-- Mybatis SQL 输出 -->
  108. <logger name="org.mybatis" level="DEBUG">
  109. <appender-ref ref="CONSOLE"/>
  110. <appender-ref ref="ALL_FILE"/>
  111. </logger>
  112. <root level="info">
  113. <appender-ref ref="CONSOLE"/>
  114. <appender-ref ref="ALL_FILE"/>
  115. <appender-ref ref="WARN_FILE"/>
  116. <appender-ref ref="ERROR_FILE"/>
  117. </root>
  118. </configuration>
  • 1

问题排查解决

问题原因大概猜测是因为配置冲突引起的, 我就去看jar的内容 , 看看是nacos 的那个pom 再用logback , 最终在 nacos-config  -> nacos-client 2.1.0 中发现了Logback 的配置文件 

 罪魁祸首找到了 , 那么久来处理掉这个冲突 , 最直接最简洁的方式就是 , 直接在config 的pom 中屏蔽掉他的nacos-client 


 
 
  1. <dependency>
  2. <groupId>com.alibaba.cloud </groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-config </artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>com.alibaba.nacos </groupId>
  7. <artifactId>nacos-client </artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  • 1

事实证明 , 无效,,,,,,, ,    此路不通  ,,,  就换条路  ,   去搜百度,  也是资料少的要死 ,  索性直接去GitHub上找Nacos  仓库 找 issues , 抱着试一试的态度 , 万幸 , 找到了一个2022年6月份提交的一个bug

 这个Issues 说得就是 nacos-logback 和  项目中的logback冲突 ,   并且这位仁兄还放出了报错信息 , 看报错内容是不是眼熟 , 嗯哼 ?  拨云见"日"了 ,  继续往下看 , 可以发现这就是一个 Nacos的Bug , 然后给出的临时解决方案就是 , 吧项目中的Logback 的 scan 属性 , set  = false ; 

 然后再次重启项目  , 豁然 启动了 , 为什么这样就可以了呢 ??

        <configuration> 元素中的 scan 属性可以用来控制 logback 是否自动扫描配置文件的变化并重新加载配置。该属性的默认值是 true,表示 logback 会周期性地检查配置文件的变化,并在检测到变化时重新加载配置文件。

具体来说,scan 属性的原理是通过启动一个单独的线程来监视配置文件的变化,一旦检测到配置文件发生了变化,就会重新读取配置文件并更新 logback 的内部状态。这样可以避免在修改配置文件后需要手动重启应用程序的情况,从而提高开发效率。

需要注意的是,如果您的配置文件较大,或者您的应用程序的日志记录量很大,那么频繁地重新加载配置文件可能会带来一定的性能开销。因此,在生产环境中建议将 scan 属性设置为 false,并在必要时手动重启应用程序以更新配置文件。

至此 , 问题算是临时性解决了 , 但是总让人感觉不满意,  总感觉是治标不治本 ,  nacos 高版本是否会解决这个问题呢  ?? 抱着好奇的态度 , 去 Mvn 中央仓库看看Nacos 的pom 版本 现在到那个版本了. ps: 我现在的项目使用的是 2.2.8  , 截止目前最新的版本是 2022.0.0.0-RC-1 

 从这里看 , client 的版本也更新了 , 

 那么 , 试试呗,  升级项目中管理的  spring-cloud-alibaba-dependencies 版本  ,


 
 
  1. <spring-cloud.version>Hoxton.SR1 </spring-cloud.version>
  2. <!-- <com.alibaba.cloud.version>2.2.8.RELEASE</com.alibaba.cloud.version>-->
  3. <com.alibaba.cloud.version>2022.0.0.0-RC1 </com.alibaba.cloud.version>
  • 1

果不其然,   出问题了 ,,,,,,真  tm  X蛋  , 最新版本的nacos pom  居然是 jdk 17  ,  我以为我11 都够新的了 , ,,,,,,,,,  搞到这里 , 如果大家不想继续搞了的话,  就用前面的解决办法 ,  把scan 设置为 false , ,,,  什么治标不治本 , 先启动再说 ,,,,爱咋滴咋滴,,,,,,有时间再搞 ,   

挣扎10分钟 , 决定还是不让这篇文章烂尾 ,  决心要处理下 

重新打开 mvn 中央仓库 , 大丈夫能屈能伸 ,  退而求其次 , 选个 使用量多的 , 低一版的 , 2021.1

 好奇看了一眼 , 这个nacos-client到底咋回事 ,  点进去一看,  豁.... 好家活,,,  

 废话少说 , pom 再一次换版本 ,  


 
 
  1. <spring-cloud.version>Hoxton.SR1 </spring-cloud.version>
  2. <!-- nacos-client 的 logback 会和项目的logback 冲突 -->
  3. <!-- <com.alibaba.cloud.version>2.2.8.RELEASE</com.alibaba.cloud.version> -->
  4. <!-- ncom/alibaba/cloud/nacos/discovery/logging/NacosLoggingListener has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0 -->
  5. <!-- 最新包需要jdk17编译 -->
  6. <!-- <com.alibaba.cloud.version>2022.0.0.0-RC1</com.alibaba.cloud.version> -->
  7. <com.alibaba.cloud.version>2021.1 </com.alibaba.cloud.version>
  • 1

完美启动

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/813273
推荐阅读
相关标签
  

闽ICP备14008679号