我正在为 clickhouse 构建一个千分尺插件。我想@AutoConfigureAfter
没有像我预期的那样工作,并告诉你原因。
我写了 ClickhouseMetricsExportAutoConfiguration 类,如下所示
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore({CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class})
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(ClickhouseMeterRegistry.class)
@ConditionalOnProperty(prefix = "management.metrics.export.clickhouse", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(ClickhouseProperties.class)
public class ClickhouseMetricsExportAutoConfiguration {
private final ClickhouseProperties properties;
public ClickhouseMetricsExportAutoConfiguration(
ClickhouseProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean
public ClickhouseConfig clickhouseConfig () {
return new ClickhousePropertiesConfigAdapter(this.properties);
}
@Bean
@ConditionalOnMissingBean
public ClickhouseMeterRegistry clickhouseMeterRegistry (ClickhouseConfig clickhouseConfig, Clock clock, ClickhouseProperties properties) {
return ClickhouseMeterRegistry.builder(clickhouseConfig)
.clock(clock)
.httpClient(
new HttpUrlConnectionSender(this.properties.getConnectTimeout(), this.properties.getReadTimeout())
).build();
}
}
该代码与运行良好的InfluxMetricsExportAutoConfiguration几乎相同。
当我运行代码时,我在调试日志中发现了这条消息。
# In 'Negative matches' section
ClickhouseMetricsExportAutoConfiguration:
Did not match:
- @ConditionalOnBean (types: io.micrometer.core.instrument.Clock; SearchStrategy: all) did not find any beans of type io.micrometer.core.instrument.Clock (OnBeanCondition)
Matched:
- @ConditionalOnClass found required class 'com.widerplanet.dpa.tazza.goni.supports.clickhouse.ClickhouseMeterRegistry' (OnClassCondition)
- @ConditionalOnProperty (management.metrics.export.clickhouse.enabled=true) matched (OnPropertyCondition)
Clock
所以总而言之,我当时没有豆子。但是它应该在注册时MetricsAutoConfiguration
注册。因为 ...
# In 'positive matches' section
MetricsAutoConfiguration#micrometerClock matched:
- @ConditionalOnMissingBean (types: io.micrometer.core.instrument.Clock; SearchStrategy: all) did not find any beans (OnBeanCondition)
因此,我猜ClickhouseMetricsExportConfiguration
bean 的构造比MetricsAutoConfiguation
即使有@AutoConfigureAfter(MetricsAutoConfiguration.class)
声明还要早。
我想知道如何解决这个问题。谢谢