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

阿里云云棲號 發佈 2020-01-07T08:47:22+00:00

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

導讀

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

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

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

  • 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=* 即可。

作者:方劍,花名洛夜

本文為阿里雲原創內容,未經允許不得轉載。

關鍵字: