SpringCloud 构建 Eureka 客户端启动报错 Failed to start component [NonLoginAuthenticator[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]]
解决方案
解决方案很简单,调整依赖导入顺序即可。Web 依赖在 Eureka 客户端依赖之前。
<dependencies>
<!-- 注意 Web 启动器和 Eureka 客户端依赖的导入顺序,Web 依赖在 Eureka 客户端依赖之前。 -->
<!-- Web 组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
错误信息
错误信息提示 servlet-api 重复导入引起的。
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [NonLoginAuthenticator[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122) [na:1.8.0_191]
at java.util.concurrent.FutureTask.get(FutureTask.java:192) [na:1.8.0_191]
...
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1319)
The following method did not exist:
javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
The calling method's class, org.apache.catalina.authenticator.AuthenticatorBase, was loaded from the following location:
jar:file:/D:/00-863/98-UP/Jar/org/apache/tomcat/embed/tomcat-embed-core/9.0.71/tomcat-embed-core-9.0.71.jar!/org/apache/catalina/authenticator/AuthenticatorBase.class
The called method's class, javax.servlet.ServletContext, is available from the following locations:
jar:file:/D:/00-863/98-UP/Jar/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
jar:file:/D:/00-863/98-UP/Jar/org/apache/tomcat/embed/tomcat-embed-core/9.0.71/tomcat-embed-core-9.0.71.jar!/javax/servlet/ServletContext.class
The called method's class hierarchy was loaded from the following locations:
javax.servlet.ServletContext: file:/D:/00-863/98-UP/Jar/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar
Action:
Correct the classpath of your application so that it contains compatible versions of the classes org.apache.catalina.authenticator.AuthenticatorBase and javax.servlet.ServletContext
原因分析
导入 Web 启动器时已经导入了 Tomcat 容器,其中包含了
servlet-api
依赖。导入 Eureka 客户端依赖时,导入了eureka-core
依赖,其中也导入了一个servlet-api
依赖,所以两者产生了重复。
Eureka 客户端依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.1.2</version>
</dependency>
Eureka 客户端依赖部分源码:在导入eureka-core
时也导入了servlet-api
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-core</artifactId>
<version>1.10.17</version>
<scope>compile</scope>
<exclusions>
...
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
...
</exclusions>
</dependency>
那么问题来了,为什么跟换两个依赖的顺序就能正常启动项目了呢?从源码看,在导入
eureka-core
依赖时,使用标签<exclusion>
去掉了servlet-api
依赖,所以在容器中只剩下一个servlet-api
依赖,就不会发生依赖重复了。