Spring Cloud 启动错误 - java.lang.NoSuchMethodError

最近想把项目中的一个功能设计成一个microservice,降低系统件的耦合度,方便后期维护与扩展。之前没有接触过microservice,只是知道microservice最近两年比较火。作为技术人员总喜欢折腾新技术,所以决定上网找个quick starter,结果找到了这篇https://spring.io/blog/2015/07/14/microservices-with-spring。

按照文中的步骤,首先需要创建一个服务注册(service registration)项目。文中给出的guide很简洁,自己新建了一个spring boot project,copy了提供的POM配置,结果运行时却碰到了如下错误:

java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.([Ljava/lang/Object;)V
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:161)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:102)
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:68)
    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:74)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:317)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
    at demo.ms.robossliu.demo.DemoApplication.main(DemoApplication.java:14)

尤其是第一行信息,似乎提示一个莫名其妙的无此方法错误。Spring boot框架很多组件都是基于依赖注入(DI),有些错误在编译时可能无法被检测出来,只有在运行时才暴露出来。既然提示无此方法,那么肯定是某个组件(包)有问题:要么未被注入;要么版本版本不对。

也没有过多考虑,直接bing一下(虽然想用google,但确实慢),看了几篇stackoverflow上的文章,基本可以断定是依赖包之间存在的兼容性问题,导致bean未能正确被注入。自己检查了这份copy出来的POM文件:


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

   
       
           
           org.springframework.boot
           spring-boot-starter
       

       
           
           org.springframework.boot
           spring-boot-starter-web
       

       
           
           org.springframework.cloud
           spring-cloud-starter
       

       
           
           org.springframework.cloud
           spring-cloud-starter-eureka-server
       
   

  
   
       
           
               org.springframework.cloud
               spring-cloud-dependencies
               Edgware.SR3
               pom
               import
           
       
   

发现自己使用的是spring boot 2.0.0版本,spring cloud使用是Edgware.SR3版本,难道是两者冲突?对于Edgware.SR3是哪个版本的spring cloud以及依赖的spring boot版本,自己不是很清楚,毕竟之前没有玩过。打开了spring cloud官网,发现Edgware.SR3版本依赖于spring boot 1.5.x。如此看来是这两个组件的版本发生了冲突,所以解决方法也比较简单:要么降级spring boot版本,要么升级spring cloud版本。内心一直秉持着一种理念“新技术总是更好”,于是果断选择升级spring cloud至最新版本Finchley。修改POM文件之后,编译运行一切OK了。

修改之后的POM文件如下,供大家参考一下:



    4.0.0
    demo.ms.robossliu
    demo
    0.0.1-SNAPSHOT
    jar
    demo
    Demo project for Spring Boot
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
        
    
    
        UTF-8
        UTF-8
        1.8
    
    
        
            org.springframework.boot
            spring-boot-configuration-processor
            2.0.0.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
            1.4.4.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
            1.4.4.RELEASE
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.BUILD-SNAPSHOT
                pom
                import
            
        
    
    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/libs-snapshot
            
                true
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

你可能感兴趣的:(Spring Cloud 启动错误 - java.lang.NoSuchMethodError)