问题:
环境 : Nacos 2.2.1 , Spring-cloud Hoxton.SR1 , com.alibaba.cloud.version 2.2.8 (留意这个版本)
最近新搭建了一次虚拟机, 然后重新部署了Nacos , 同时也在一个新项目里引入了Nacos 的服务注册功能 , 以下是服务Pom版本管理配置
<properties>
<java.version>1.8</java.version>
<file_encoding>UTF-8</file_encoding>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
<com.alibaba.cloud.version>2.2.8.RELEASE</com.alibaba.cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.2.8.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${com.alibaba.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后再实际使用模块pom文件中引入服务注册和配置管理
<dependencies>
<!-- Nacos依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 在没有引入一下pom时候服务注册是正常的 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
application.ymal 的配置如下
spring:
profiles: dev
cloud:
nacos:
username: nacos
password: nacos
discovery:
server-addr: 192.168.30.128:8848
namespace: 281222d0-6848-4017-8768-c81df219e12
config:
bootstrap:
enabled: true
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml
namespace: 281222d0-6848-4017-8768-c81df219e12
auto-refresh: true
此时启动项目问题就显露了 , 在没有加上 spring-cloud-starter-alibaba-nacos-config 这个maven配置的时候服务是可以正常启动的 , 然后加了之后 服务根本就启动不起来 , 报错如下
2023-03-31 10:30:06.710 ERROR 40924 --- [ main]o.s.boot.SpringApplication : 826 : Application run failed
java.lang.IllegalStateException: Logback configuration error detected:
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.
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
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
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.
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
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
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.
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
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
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.
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
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
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.
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
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
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.
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.
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
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:169)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:80)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:118)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:313)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:288)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:246)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:223)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.ko.assy.db.ms.KoAssyDbApplication.main(KoAssyDbApplication.java:29)
看表象 , 是Logback 引起的问题 , 初步怀疑是可能因为冲突了 , 因为我本地也使用了 Logback 的配置 , Logbak内容入如下
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<contextName>logback</contextName>
<springProperty scope="context" name="logDir" source="log.dir" defaultValue=""/>
<property name="log.path" value="${logDir}"/>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
<conversionRule conversionWord="wex"
converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
<conversionRule conversionWord="wEx"
converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN"
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}}"/>
<!--1. 输出到控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<!-- 设置字符集 -->
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="ALL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/web-all.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%15.15thread]%-40.40logger{39}:%4line: %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/web-all-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文档保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<appender name="TRACE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/web-trace.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%15.15thread]%-40.40logger{39}:%4line: %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/web-trace-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文档保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/web-warn.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%15.15thread]%-40.40logger{39}:%4line: %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/web-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文档保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/web-error.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%15.15thread]%-40.40logger{39}:%4line: %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/web-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文档保留天数-->
<maxHistory>15</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 忽略Nacos配置中心长轮询日志 -->
<logger name="com.alibaba.nacos.client.config.impl" level="WARN"/>
<!-- Mybatis SQL 输出 -->
<logger name="org.mybatis" level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ALL_FILE"/>
</logger>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ALL_FILE"/>
<appender-ref ref="WARN_FILE"/>
<appender-ref ref="ERROR_FILE"/>
</root>
</configuration>
问题排查解决
问题原因大概猜测是因为配置冲突引起的, 我就去看jar的内容 , 看看是nacos 的那个pom 再用logback , 最终在 nacos-config -> nacos-client 2.1.0 中发现了Logback 的配置文件
罪魁祸首找到了 , 那么久来处理掉这个冲突 , 最直接最简洁的方式就是 , 直接在config 的pom 中屏蔽掉他的nacos-client
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
事实证明 , 无效,,,,,,, , 此路不通 ,,, 就换条路 , 去搜百度, 也是资料少的要死 , 索性直接去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 版本 ,
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
<!-- <com.alibaba.cloud.version>2.2.8.RELEASE</com.alibaba.cloud.version>-->
<com.alibaba.cloud.version>2022.0.0.0-RC1</com.alibaba.cloud.version>
果不其然, 出问题了 ,,,,,,真 tm X蛋 , 最新版本的nacos pom 居然是 jdk 17 , 我以为我11 都够新的了 , ,,,,,,,,, 搞到这里 , 如果大家不想继续搞了的话, 就用前面的解决办法 , 把scan 设置为 false , ,,, 什么治标不治本 , 先启动再说 ,,,,爱咋滴咋滴,,,,,,有时间再搞 ,
挣扎10分钟 , 决定还是不让这篇文章烂尾 , 决心要处理下
重新打开 mvn 中央仓库 , 大丈夫能屈能伸 , 退而求其次 , 选个 使用量多的 , 低一版的 , 2021.1
好奇看了一眼 , 这个nacos-client到底咋回事 , 点进去一看, 豁.... 好家活,,,
废话少说 , pom 再一次换版本 , 文章来源:https://www.toymoban.com/news/detail-456750.html
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
<!-- nacos-client 的 logback 会和项目的logback 冲突 -->
<!-- <com.alibaba.cloud.version>2.2.8.RELEASE</com.alibaba.cloud.version> -->
<!-- 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 -->
<!-- 最新包需要jdk17编译 -->
<!-- <com.alibaba.cloud.version>2022.0.0.0-RC1</com.alibaba.cloud.version> -->
<com.alibaba.cloud.version>2021.1</com.alibaba.cloud.version>
完美启动文章来源地址https://www.toymoban.com/news/detail-456750.html
到了这里,关于浅谈因为项目中的Logback于Nacos的Logback冲突导致的项目启动失败 ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CO的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!