解决一些Springboot启动报错问题

项目启动报错比调bug更加难受

概要:近日在学习Springcloud微服务。配置服务注册中心,一切妥妥当当,但还是报了一个让我崩溃的问题:org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
翻译一下这句话:启动内置tomcat失败。
具体报错信息如下:

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:155) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at com.ecjtu.demo.DemoApplication.main(DemoApplication.java:15) [classes/:?]
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:126) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.(TomcatWebServer.java:86) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:409) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:174) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	... 8 more
Caused by: java.lang.IllegalStateException: StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[] failed to start
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.rethrowDeferredStartupExceptions(TomcatWebServer.java:172) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:110) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.(TomcatWebServer.java:86) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:409) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:174) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
	... 8 more

出了错,当然得去看配置文件application.yml,pom.xml,程序入口类。这三个。我们一个一个来看。

  1. application.yml(application.properties)
    解决一些Springboot启动报错问题_第1张图片
    极其简单的配置,但是跑起来不是问题。
  2. 程序入口类
    解决一些Springboot启动报错问题_第2张图片
    当然也是没有问题的,就是加了一个@EnableEurekaServer注解,表示是一个服务注册中心
  3. pom.xml
    放在最后不是没道理的


    4.0.0
    com.ecjtu
    demo
    0.01-SNAPSHOT
    jar
    demo
    
    
    
        UTF-8
        Finchley.SR1
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
        
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        
        
            junit
            junit
            4.12
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    
    
    build>
        demo
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                         -server -Xmx2048m -Xms2048m -XX:MaxPermSize=512m
                         -Xdebug
                         -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9090
                    
                    com.lelian.Application
                    true
                
            
        
    

我是左看右看,就是解决不了,但是如果我把@EnableEurekaServer注解和关于eureka的配置都去掉,就完全是一个普通的Springboot项目,但是项目可以跑起来。我也是很奇怪啊!!!
最后想了一下是不是java版本的问题,网上许多教程都是在java8的环境下搭建的,但是我用的java版本是java10,这是一个你知道但是不知道他有什么特性的版本,然后百度了一下他的特性,从java9开始,模块化的概念使得JAXB默认没有加载;jaxb-api是存在jdk中的,只是默认没有加载而已,我们需要手动引入。要问这个jaxb-api是什么作用,大家可以自行百度,反正我引入了这个模块问题得到了解决。

 jaxb模块引用 - start
        
            com.sun.xml.bind
            jaxb-core
            2.2.11
        
        
            javax.xml.bind
            jaxb-api
        
        
            com.sun.xml.bind
            jaxb-impl
            2.2.11
        
        
            org.glassfish.jaxb
            jaxb-runtime
            2.2.10-b140310.1920
        
        
            javax.activation
            activation
            1.1.1
        
         jaxb模块引用 - end 

最终,项目跑起来了。
解决一些Springboot启动报错问题_第3张图片
以上是我遇到的问题和解决的办法,不知道对于大家有没用。若有不当之处还望各位指正。

你可能感兴趣的:(个人学习)