java 如何在 Spring Boot 应用程序中配置 HikariCP 和 Dropwizard/Coda-Hale 指标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28615203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I configure HikariCP and Dropwizard/Coda-Hale metrics in Spring Boot application
提问by Kevin M
Reading the instructionson the HikariCP wiki about how to enable the Dropwizard metrics, it says to just configure a MetricsRegistry
instance in HikariConfig
or HikariDatasource
.
阅读说明书关于如何启用Dropwizard度量HikariCP维基,它说,只是配置MetricsRegistry
的实例HikariConfig
或HikariDatasource
。
Problem is, in Spring Boot, all the configuration is handled by autoconfiguration so I'm not manually configuring the HikariCP pool at all.
问题是,在 Spring Boot 中,所有配置都是由自动配置处理的,所以我根本没有手动配置 HikariCP 池。
Any instructions on how to do this? Do I have to completely override the autoconfiguration by defining my own bean and setting all the settings in a @Configuration
file?
有关如何执行此操作的任何说明?我是否必须通过定义我自己的 bean 并在@Configuration
文件中设置所有设置来完全覆盖自动配置?
回答by Kevin M
So I was able to figure this out by manually configuring HikariCP in a java configuration file. That allowed me to get a reference to the Spring Boot MetricRegistry, which I could then set in HikariConfig. Here's my configuration class:
所以我能够通过在 java 配置文件中手动配置 HikariCP 来解决这个问题。这使我能够获得对 Spring Boot MetricRegistry 的引用,然后我可以在 HikariConfig 中设置它。这是我的配置类:
@Configuration
public class DatasourceConfiguration {
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.url}")
private String dataSourceUrl;
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.connectionTestQuery}")
private String connectionTestQuery;
@Autowired
private MetricRegistry metricRegistry;
@Bean
public DataSource primaryDataSource() {
Properties dsProps = new Properties();
dsProps.setProperty("url", dataSourceUrl);
dsProps.setProperty("user", user);
dsProps.setProperty("password", password);
Properties configProps = new Properties();
configProps.setProperty("connectionTestQuery", connectionTestQuery);
configProps.setProperty("driverClassName", driverClassName);
configProps.setProperty("jdbcUrl", dataSourceUrl);
HikariConfig hc = new HikariConfig(configProps);
hc.setDataSourceProperties(dsProps);
hc.setMetricRegistry(metricRegistry);
return new HikariDataSource(hc);
}
}
回答by Svante
Or let Spring Boot configure your data source, @Autowire the DataSource and MetricRegistry in your @Configuration/@SpringBootApplication class and wire them together in a @PostConstruct:
或者让 Spring Boot 配置您的数据源,@Autowire 在您的 @Configuration/@SpringBootApplication 类中的 DataSource 和 MetricRegistry 并将它们连接在一起@PostConstruct:
@Autowired
private DataSource dataSource;
@Autowired
private MetricRegistry metricRegistry;
@PostConstruct
public void setUpHikariWithMetrics() {
if(dataSource instanceof HikariDataSource) {
((HikariDataSource) dataSource).setMetricRegistry(metricRegistry);
}
}