当 JDBC 位于数据源 Bean 上时,@RefreshScope未在 Spring 引导 2 上注册指标



我有一个使用 Spring boot 2 并记录千分尺指标的应用程序。我想定期记录jdbc(mysql(最小,最大和活动连接。我还想在我的数据源 bean 上使用 @RefreshScope 来防止从 spring 管理员动态注入配置时的 hikari 绑定异常。我发现当我在配置类/数据源be上使用@RefreshScope时,JDBC 不会向 MeterRegistry 注册自己。

是否有可能让 JDBC 向 MeterRegistry 注册@RefreshScope?

有没有办法在我的 bean 定义中向 MeterRegistry 编程注册 JDBC?

@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@RefreshScope
public class DbConfig {
@Primary
@Bean(name = "dataSource")
@RefreshScope
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

删除@RefreshScope允许 JDBC 自动向 MeterRegistry 注册,但在配置更改时会导致以下异常: org.springframework.boot.context.properties.ConfigurationPropertiesBindException:创建名为"dataSource"的 bean 时出错:无法将属性绑定到"HikariDataSource":前缀=spring.datasource,忽略无效字段=false,忽略未知字段=true;嵌套异常是 org.springframework.boot.context.properties.bind.BindException:无法将"spring.datasource"下的属性绑定到 javax.sql.DataSource

添加javax.sql.DataSource作为额外的刷新对象。application.yml文件示例:

spring:
cloud:
refresh.extra-refreshable:
- javax.sql.DataSource

并从课程中删除@RefreshScope。 其他解决方案是将数据源转换为 HikariDataSource。 我使用第一个解决方案,因为数据源的创建是由应用程序中的外部库完成的。

参考: https://github.com/spring-cloud/spring-cloud-commons/issues/318

最新更新