自动配置故障排除

目录

官网文档

71.2 Troubleshoot auto-configuration

翻译

71.2 自动配置故障排除


官网文档

71.2 Troubleshoot auto-configuration

The Spring Boot auto-configuration tries its best to ‘do the right thing’, but sometimes things fail and it can be hard to tell why.

There is a really useful ConditionEvaluationReport available in any Spring Boot ApplicationContext. You will see it if you enable DEBUG logging output. If you use the spring-boot-actuator there is also an autoconfig endpoint that renders the report in JSON. Use that to debug the application and see what features have been added (and which not) by Spring Boot at runtime.

Many more questions can be answered by looking at the source code and the Javadoc. Some rules of thumb:

  • Look for classes called *AutoConfiguration and read their sources, in particular the @Conditional* annotations to find out what features they enable and when. Add --debug to the command line or a System property -Ddebug to get a log on the console of all the auto-configuration decisions that were made in your app. In a running Actuator app look at the autoconfig endpoint (‘/autoconfig’ or the JMX equivalent) for the same information.
  • Look for classes that are @ConfigurationProperties (e.g. ServerProperties) and read from there the available external configuration options. The@ConfigurationProperties has a name attribute which acts as a prefix to external properties, thus ServerProperties has prefix="server" and its configuration properties are server.portserver.address etc. In a running Actuator app look at the configprops endpoint.
  • Look for use of RelaxedPropertyResolver to pull configuration values explicitly out of the Environment. It often is used with a prefix.
  • Look for @Value annotations that bind directly to the Environment. This is less flexible than the RelaxedPropertyResolver approach, but does allow some relaxed binding, specifically for OS environment variables (so CAPITALS_AND_UNDERSCORES are synonyms for period.separated).
  • Look for @ConditionalOnExpression annotations that switch features on and off in response to SpEL expressions, normally evaluated with placeholders resolved from the Environment.

翻译

71.2 自动配置故障排除

springboot 自动配置尽最大努力做"正确的事情" ,但是有时候会出错,并且很难弄清楚原因。

ConditionEvaluationReport非常有用,在任何Spring Boot 上下文中都可以找到。如果把日志级别设置为DEBUG,那么在控制台可以看到ConditionEvaluationReport相关输出。如果使用 spring-boot-actuator的话,利用autoconfig endpoint (自动配置端点)("/autoconfig")可以把ConditionEvaluationReport渲染为JSON格式。使用debug日志级别,通过ConditionEvaluationReport可以看到springboot运行时包含的和没有包含的特性信息。

许多问题可以通过查看源代码和java doc来解决。一下经验法则:

  • 查找 *AutoConfiguration 类然后阅读源码,特别使用@Conditional*的类了解它们启用的功能以及何时启用。在命令行添加 --debug或者添加系统变量-Ddebug 以便在控制台查看应用程序中的自动配置信息。在运行有Actuator的应用程序中使用autoconfig 端点(‘/autoconfig’ 或者  JMX)也可以看到相同的信息。
  • 查找使用@ConfigurationProperties的类(例如ServerProperties)从中读取可用的外部配置选项。@configurationproperties有一个name属性,用作外部属性的前缀。因此,server properties的前缀为“server”,其配置属性为server.port、server.address等。在正在运行的执行器应用程序中,查看configprops端点。
  • 查找使用RelaxedPropertyResolver(将配置值显式拉出环境)的地方。它通常与前缀一起使用。
  • 查找直接绑定到环境的@value注释。这比RelaxedPropertyResolver方式灵活度低,但允许一些轻松的绑定,特别是对于OS环境变量(因此大写字母_和下划线是period.separated的同义词)。
  • 查找@ConditionalOnExpression注释,这些注释根据SPEL表达式打开和关闭功能,通常使用环境中解析的占位符进行计算。

 

 

猜你喜欢

转载自blog.csdn.net/yyqhwr/article/details/87939122