1. 程式人生 > 其它 >SpringCloud+MyBatisPlus微服務啟動系列異常解決方法

SpringCloud+MyBatisPlus微服務啟動系列異常解決方法

技術標籤:SpringCloudspring bootspring cloud

最近,使用Spring Cloud 2020.0.0-SNAPSHOT整合SpringBoot2.4.0、MyBatis Plus3.4.1搭建微服務,JDK版本是13.0.2。
啟動微服務應用時,出現一系列異常,重點羅列如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (
_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.0) _ _ |_ _ _|_. ___ _ | _ | | |\/|_)(_| | |_\ |_)||_|_\ / | 3.4.1 ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '
xxxController': Unsatisfied dependency expressed through field 'xxxFeignService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.edee.xxx.xxx.feign.xxxFeignService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No Feign Client for
loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?

看到最後一句“Did you forget to include spring-cloud-starter-loadbalancer?”,立馬想到服務依賴是否缺少?於是,嘗試新增如下依賴:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>

重新啟動服務,上面的異常不見了。但又出現了新的:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-16 11:08:07.394 ERROR 32748 --- [           main] o.s.boot.SpringApplication               : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'growthChangeHistoryController': Unsatisfied dependency expressed through field 'growthChangeHistoryService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'growthChangeHistoryService': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.edee.xxx.xxx.dao.GrowthChangeHistoryDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

這個很讓人頭疼,百思不得其解!
懷著絕望的心情查閱網上各種資料,說法也是五花八門,一一嘗試,均不奏效。
但有些文章提到了版本相容問題,比如就有文章專門指出SpringBoot雖然已經發布了2.4.0版本,但是,不建議大家馬上升級,因為版本相容問題較多。
接下來嘗試把SpringBoot降級為2.1.13.RELEASE,重啟,又異常:

Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file ;
nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 57

根據此異常,查到關鍵一篇部落格:https://www.cnblogs.com/INnoVationv2/p/12349571.html。裡面提到:jdk13暫時不被支援,降級至jdk11,可正常執行。
無奈,只能大換血,將JDK降為11.0.9。重啟,依然異常:

"C:\Program Files\Java\jdk-11.0.9\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar=53833:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\AppData\Local\Temp\classpath336559354.jar com.edee.xxx.ware.xxxApplication
錯誤: 找不到或無法載入主類 com.edee.xxx.ware.xxxApplication
原因: java.lang.ClassNotFoundException: com.edee.xxx.ware.xxxApplication

幾近崩潰下,又嘗試降低Spring Cloud版本,由2020.0.0-SNAPSHOT降為Greenwich.SR6。這次,終於成功了!

最終版本組合:
Spring Cloud:Greenwich.SR6
SpringBoot:2.1.13.RELEASE
MyBatis Plus:3.4.1
JDK:11.0.9
但願該組合能夠穩如泰山啊!
在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述