天天看點

穩定性專題 | Spring Boot 常見錯誤及解決方法

導讀

『StabilityGuide』是阿裡多位阿裡技術工程師共同發起的穩定性領域的知識庫開源項目,涵蓋性能壓測、故障演練、JVM、應用容器、服務架構、流量排程、監控、診斷等多個技術領域,以更結構化的方式來打造穩定性領域的知識庫,歡迎您的加入。

✅ @GitHub :

https://github.com/StabilityMan/StabilityGuide

✅ @釘釘群:

穩定性專題 | Spring Boot 常見錯誤及解決方法

Spring Boot 作為 Java 生态中最流行的開發架構,意味着被數以萬計的開發者所使用。下面根據我們自身遇到的問題,加上使用者提供的一些回報,來大緻梳理下 Spring Boot 的常見錯誤及解決方法。

找不到配置?配置不對?配置被覆寫?

Spring Boot 配置加載過程解析:

1、Spring Boot 配置的加載有着約定俗成的步驟: 從 resources 目錄下加載 application.properties/application.yml ; 再根據裡面的 spring.profiles.active 來加載不同 profile 的配置檔案 application-dev.properties/application-dev.yml (比如加載 profile 為 dev 的配置檔案)。

2、Spring Boot 所有的配置來源會被構造成 PropertySource,比如 -D 參數, -- 參數, 系統參數, 配置檔案配置等等。這些 PropertySource 最終會被添加到 List 中,擷取配置的時候會周遊這個 List ,直到第一次擷取對應 key 的配置,是以會存在優先級的問題。具體配置的優先級參考:

https://stackoverflow.com/a/45822571

配置覆寫案例:

Nacos 服務注冊的 IP 可以通過 spring.cloud.nacos.discovery.ip 設定,當我們打成 JAR 包之後,如需修改注冊 IP,可以通過 -Dspring.cloud.nacos.discovery.ip=xxx(-D 參數配置的優先級比配置檔案要高)。

配置問題排查:

進入

http://host:port/actuator/env

這個 endpoint 檢視具體的配置項屬于哪個 PropertySource。

Jar 包啟動不了

執行 Spring Boot 建構的 jar 包後,傳回 "my.jar中沒有主清單屬性" 錯誤。

錯誤分析: Spring Boot 的正常 jar 包運作方是通過 spring-boot-loader 這個子產品裡的 JarLauncher 完成的,該類内部提供了一套運作的規範。

解決方案: 在 pom 裡加上 spring-boot-maven-plugin 的 maven 插件配置(該插件會在 jar 裡加入 spring-boot-loader 的代碼,并在 MANIFEST.MF 中的 Main-Class 裡寫入 JarLauncher):

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
           

自動化配置類沒有被加載

條件注解是 Spring Boot 的核心特性之一,第三方的 starter 或我們自定義的 starter 内部都會加載一些 AutoConfiguration,有時候會存在一些 AutoConfiguration 沒有被加載的情況。導緻出現 NoSuchBeanDefinitionException, UnsatisfiedDependencyException 等異常

排查步驟(三種方式):

1、把 spring 的日志級别調到 debug 級别:

logging.level.org.springframework: debug。

2、從 ApplicationContext 中擷取 ConditionEvaluationReport,得到内部的 ConditionEvaluationReport.ConditionAndOutcomes 類中的輸出資訊。

3、進入

http://host:port/actuator/conditions

這個 endpoint 檢視條件注解的 match 情況。

這是日志列印的不滿足條件的 AutoConfiguratoin:

Unconditional classes:
----------------------

    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

    org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration

    org.springframework.boot.actuate.autoconfigure.info.InfoContributorAutoConfiguration

    org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration

    org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration

    org.springframework.cloud.client.CommonsClientAutoConfiguration

    org.springframework.cloud.commons.httpclient.HttpClientConfiguration

    org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration

    org.springframework.cloud.loadbalancer.config.BlockingLoadBalancerClientAutoConfiguration           

定義的 Component 沒有被掃描到

@SpringBootApplication 注解内部也會使用 @ComponentScan 注解用于掃描 Component 。預設情況下會掃描 @SpringBootApplication 注解修飾的入口類的包以及它下面的子包中所有的 Component 。

@ComponentScan:

https://github.com/StabilityMan/StabilityGuide/blob/master/ComponentScan

這是推薦的包結構中項目的結構:

穩定性專題 | Spring Boot 常見錯誤及解決方法
  • exclude 包下的類不會被掃描到,card 包下的類會被掃描到。
  • Actuator Endpoint 通路不了
  • 通路 Actuator,出現 404 錯誤。

解決方案:

1、Spring Boot 2.x 版本對 Actuator 做了大量的修改,其中通路的路徑從

http://host:port/endpointid

變成了

http://host:port/actuator/endpointid

確定通路的路徑正确。

2、Endpoint 有 Security 要求,

在配置裡加上 management.endpoints.web.exposure.include=* 即可。

本文作者:方劍,花名洛夜,GitHub ID @fangjian0423,開源愛好者,阿裡巴巴進階開發工程師,阿裡雲産品 EDAS 開發,Spring Cloud Alibaba 開源項目負責人。