Ehcache 3和Spring Boot Admin统计数据

4

我正在尝试将一个目前使用Ehcache 2的Spring Boot项目迁移到最新的Ehcache 3.7。

除了缺失Spring Boot Admin缓存统计信息之外,一切似乎都很好。

这是以前的Ehcache 2配置:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="ehcache.xsd"
     updateCheck="false"
     monitoring="on"
     dynamicConfig="true"
     statistics="true">

<cache name="asset"
       maxEntriesLocalHeap="5"
       timeToIdleSeconds="600"
       timeToLiveSeconds="3600"
       memoryStoreEvictionPolicy="LRU"/>

</ehcache>

新的Ehcache 3配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<config
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns='http://www.ehcache.org/v3'
    xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
    xsi:schemaLocation="
    http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.6.xsd
    http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.6.xsd">

<service>
    <jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>

<cache alias="asset">
    <resources>
        <heap unit="entries">5</heap>
    </resources>
    <expiry>
        <ttl unit="hours">1</ttl>
    </expiry>
    <jsr107:mbeans enable-management="true" enable-statistics="true"/>
</cache>

</config>

POM依赖项(仅与缓存管理相关的依赖项):

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jcache</artifactId>
        <version>5.4.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.7.0</version>
    </dependency>

Spring配置:

spring:
  cache:
    ehcache:
      config: classpath:ehcache.xml
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL8Dialect
        hbm2ddl:
          auto: none
        cache:
          use_second_level_cache: true
          region:
            factory_class: jcache
        javax:
          cache:
            provider: org.ehcache.jsr107.EhcacheCachingProvider
            missing_cache_strategy: fail

我曾经使用Ehcache 2获得这种统计数据:

enter image description here

但是在Ehcache 3中,SBA Insights/Details页面和Data/Caches页面上都没有显示任何统计信息。

在Ehcache 2中,它非常易于使用,但在Ehcache 3中似乎并非如此。

有人有提示吗?

谢谢!

1个回答

4
解决了!愚蠢的错误...
在Spring配置中,应该写成:
spring:
  cache:
    ehcache:
      config: classpath:ehcache.xml

我必须在Ehcache 3中使用这个:

spring:
  cache:
    jcache:
      config: classpath:ehcache.xml

定义一个bean:

@Bean
public HibernatePropertiesCustomizer hibernateSecondLevelCacheCustomizer(
        JCacheCacheManager cacheManager) {
    return (properties) -> properties.put(ConfigSettings.CACHE_MANAGER,
            cacheManager.getCacheManager());

}

如此描述:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-hibernate-second-level-caching。现在,缓存可通过SBA在操作器和JMX中清晰地报告 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接