我有一个应用程序,它使用spring boot 2,并记录来自微米的指标。我希望定期记录jdbc (mysql)最小、最大和活动连接。我还想在我的数据源bean上使用@RefreshScope,以防止在spring admin中动态注入配置时出现hikari绑定异常。我发现当我在config class/datasource bean上使用@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();
}删除@ MeterRegistry允许JDBC自动向bean注册,但在配置更改时导致以下异常: org.springframework.boot.context.properties.ConfigurationPropertiesBindException:在创建名为“”dataSource“”的bean时出错:无法将属性绑定到“”HikariDataSource“”:prefix=spring.datasource、ignoreInvalidFields=false、ignoreUnknownFields=true;嵌套异常是org.springframework.boot.context.properties.bind.BindException:无法将“”spring.datasource“”下的属性绑定到javax.sql.DataSource“”
发布于 2018-10-20 00:46:28
添加javax.sql.DataSource作为额外的可刷新。application.yml文件示例:
spring:
cloud:
refresh.extra-refreshable:
- javax.sql.DataSource并从您的类中删除@RefreshScope。另一种解决方案是将DataSource转换为HikariDataSource。我使用第一种解决方案,因为DataSource的创建是由应用程序中的外部库完成的。
参考:https://github.com/spring-cloud/spring-cloud-commons/issues/318
https://stackoverflow.com/questions/52896477
复制相似问题