首页 > 技术文章 > Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

yiMro 2020-08-07 11:20 原文

  在使用Eureka时出现 Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat错误。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-07 11:00:39.609 ERROR 16772 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
    at com.yxw.springcloud.Eureka1Config.main(Eureka1Config.java:12) [classes/:na]
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

排查是不是yml文件的错误  

server:
  port: 8081

spring:
  application:
    name: server-euerka

# Eureka配置
eureka:
  instance:
    hostname: localhost #eureka服务端的实列名称
  client:
    register-with-eureka: false # 表示是否向eureka注册中心注册自己
    fetch-registry: false # 如果为false,表示自己为注册中心
    service-url: #监控页面
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在看一下是不是jdk的问题,相比较于jdk1.8以上的版本缺少了依赖包。

要使JAXBAPI在运行时可用,请指定以下命令行选项:
--add-modules java.xml.bind

快速和肮脏的解决方案:(仅JDK 9/10)
要使JAXBAPI在运行时可用,请指定以下命令行选项:
--add-modules java.xml.bind

但是我仍然需要这个来使用Java 8

如果您尝试指定--add-modules对于旧的JDK,它会崩溃,因为它是一个无法识别的选项。我建议两种选择之一:

通过检查JDK版本,可以在启动脚本(如果有)中有条件地应用参数$JAVA_HOME/release为JAVA_VERSION。
您可以添加-XX:+IgnoreUnrecognizedVMOptions让JVM默默地忽略不可识别的选项,而不是崩溃。但要小心!您使用的任何其他命令行args都将不再由JVM对您进行验证。此选项适用于Oracle/OpenJDK以及IBMJDK(截至JDK8sr4)
替代快速解决方案:(仅限于JDK 9/10)

注意,您可以通过指定--add-modules java.se.ee选择。这,这个,那,那个java.se.ee模块是一个聚合模块,它包括java.se.ee以及上面的JavaEEAPI模块。

正确的长期解决方案:(所有JDK版本)

上面列出的JavaEEAPI模块都有标记@Deprecated(forRemoval=true)因此,它们可能会在Java 11中被删除。所以--add-moduleJava 11中的方法将不再有效。

在Java 11和后续中,您需要做的是在类路径或模块路径上包含您自己的JavaEEAPI副本。例如,可以将JAX-BAPI添加为maven依赖项
View Code
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.0</version>
</dependency>

如果还是不行那应该就是版本不兼容了,spring cloud我用的Greenwich版本,spring boot用的是2.1.6,现在换成新一点的2.2.2就出问题了,那就用回低版本吧。

<!--            springcloud依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

<!--            spring-boot依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

spring-boot于spring-cloud对应版本:https://www.jianshu.com/p/29ee7dddcbae

推荐阅读