天天看點

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

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

  腳本之家

你與百萬開發者在一起

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

本文經授權轉自公衆号 阿裡巴巴中間件(ID:Aliware_2018)

如若轉載請聯系原公衆号

- 正文開始 -

?

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.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>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

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

springboot actuator_穩定性專題 | 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 開源項目負責人。

- END -

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

更多精彩

在公衆号背景對話框輸入以下關鍵詞

檢視更多優質内容!

女朋友 | 大資料 | 運維 | 書單 | 算法

大資料 | JavaScript | Python | 黑客

AI | 人工智能 | 5G | 區塊鍊

機器學習 | 數學 | 送書

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

● 

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

 人人都欠微軟一個正版?

● 

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

 腳本之家粉絲福利,請檢視!

● 

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

 程式員怒打産品經理,這個需求做不了

● 緻敬經典:Linux/UNIX必讀書單推薦給你

● 一個故事講完CPU的工作原理

● 終于有人把 Nginx 說清楚了,圖文詳解!

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