网站首页 > 博客文章 正文
找不到配置?配置不对?配置被覆盖?
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=* 即可。
找不到配置?配置不对?配置被覆盖?
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=* 即可。
查看更多:https://yqh.aliyun.com/detail/6459?utm_content=g_1000105546
上云就看云栖号:更多云资讯,上云案例,最佳实践,产品入门,访问:https://yqh.aliyun.com/
猜你喜欢
- 2024-09-20 Spring cloud Ribbon 客户端负载均衡详解(二)负载均衡器
- 2024-09-20 springcloud(十三):注册中心 Consul 使用详解
- 2024-09-20 SpringCloud系列——11Spring Cloud 源码分析之Gateway网关
- 2024-09-20 FeignClient注解配置url属性实现指定服务方
- 2024-09-20 SpringCloud升级之路2020.0.x版-34.验证重试配置正确性(2)
- 2024-09-20 我放弃了okhttp、httpClient,选了这个神仙工具
- 2024-09-20 还没有秃头吗?你真的需要大牛来教你如何深入解析Ribbon源码了
- 2024-09-20 Spring GateWay : 网关的转发细节
- 2024-09-20 深入理解SpringCloud之Gateway,小白都能看懂的保姆级教学
- 2024-09-20 微服务架构进阶:Hystrix 如何解决灾难性雪崩及隔离问题
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)