Spring-boot 执行器不发布休眠指标

问题描述 投票:0回答:1

执行器和休眠指标存在问题。指标不是由 HibernateMetricsAutoConfiguration 生成的。我们有多个休眠连接,这些设置来自 application.properties (使用 beans 以编程方式配置)

在应用程序属性中:

spring.jpa.properties.hibernate.generate_statistics=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

在 HibernateSessionHandler 类中:

    public HibernateSessionHandler setConfiguration(ConnectionConfig config) {
        this.configuration = new Configuration();
        this.configuration.setProperty("hibernate.connection.driver_class", config.getDriverClass());
        this.configuration.setProperty("hibernate.connection.url", config.getHost());
        this.configuration.setProperty("hibernate.connection.username", config.getLogin());
        this.configuration.setProperty("hibernate.connection.password", config.getPassword());
        this.configuration.setProperty("hibernate.dialect", config.getDialect());
        this.configuration.setProperty("hibernate.generate_statistics", "true");
        return this;
    }

HibernateSessionHandler - 注释为@Service 并由最终连接bean 扩展。

我们坚持仅在必要时创建和打开连接的概念(我们的操作只是“选择”),这可能是问题......我不明白如何自动配置 HibernateMetrics 和 HibernateQueryMetrics 或如何在我时更新指标我自己创建 HibernateMetrics 和 HibernateQueryMetrics beans

请帮忙=)

Spring 启动版本 - 2.7.16 Hibernate 核心版本 - 5.6.15.Final Hibernate 测微计版本 - 6.3.1.Final

我希望在应用程序启动时自动注册 HibernateMetrics 和 HibernateQueryMetrics,但没有 Hibernate 指标。

我尝试在扩展 HibernateSessionHandler 类中创建返回 HibernateMetrics 的 bean 在这种情况下,hibernate metrics 会被创建但不会更新。 我尝试通过 setConfiguration(ConnectionConfig config) 方法设置 hibernate.generate_statistics=true (上面的示例)

我如何修复它
为了解决这个问题,我不得不稍微改变我们的应用程序。当我们开始开发应用程序时,我们同意为每个数据库请求创建一个 SessionFcrory 和一个 Session (我们只是读取多个数据库而不更新它们),但这是错误的方式。 首先,这不是最佳实践。 其次,它的工作速度相当慢。正如他们在几个 stackoverflow 查询中所说 - 创建 SessionFactory - 是一项非常昂贵的操作。

我是如何修复它的:

  1. 对于我们所有的连接,我必须在标记为 @Component 的 HibernateConncetion 类的实现中创建像这样的 问题 的 bean

  2. 删除所有@Scope(“原型”)并使所有组件成为单例

  3. 添加了c3p0和hibernate-c3p0的使用

  4. 为休眠启用“信息”日志记录级别

  5. 添加到属性:

     this.configuration.setProperty("hibernate.generate_statistics", "true");
     this.configuration.setProperty("hibernate.c3p0.min_size", "5");
     this.configuration.setProperty("hibernate.c3p0.max_size", "20");
     this.configuration.setProperty("hibernate.c3p0.max_statements", "200");
    

看起来运行良好。

但最后一个问题是 - 为什么 hibernate-c3p0-micrometer-actuator-spring 在应用程序启动时不自动创建指标(无需自行创建 MeterBinder beans)?

java spring spring-boot hibernate spring-boot-actuator
1个回答
0
投票

有同样的问题,只需添加

hibernate-micrometer
依赖即可解决。

© www.soinside.com 2019 - 2024. All rights reserved.