【无语】SpringCloud 构建 Eureka 客户端启动报错 Failed to start component

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 依赖,就不会发生依赖重复了。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值