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):

<code><plugin>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-maven-plugin/<artifactid>
/<plugin>
/<code>

自動化配置類沒有被加載

條件註解是 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:

<code>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/<code>

定義的 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=* 即可。

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

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):

<code><plugin>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-maven-plugin/<artifactid>
/<plugin>
/<code>

自動化配置類沒有被加載

條件註解是 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:

<code>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/<code>

定義的 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=* 即可。

查看更多:https://yqh.aliyun.com/detail/6459?utm_content=g_1000105546

上雲就看雲棲號:更多雲資訊,上雲案例,最佳實踐,產品入門,訪問:https://yqh.aliyun.com/


分享到:


相關文章: