【问题标题】:Spring boot cache Hazelcast return empty list of cache names and no metrics will displaySpring Boot 缓存 Hazelcast 返回缓存名称的空列表,并且不会显示任何指标
【发布时间】:2020-03-25 21:54:42
【问题描述】:

我开始使用 Hazelcast 缓存,我想为它们公开指标,但我不知道该怎么做。

我的 java 配置

`@配置 公共类 HazelcastConfiguration {

@Bean
public Config config(){
    return new Config()
            .setInstanceName("hazelcast-instace")
            .addMapConfig(
                    new MapConfig()
                    .setName("testing")
                    .setMaxSizeConfig(new MaxSizeConfig(10, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE))
                    .setEvictionPolicy(EvictionPolicy.LRU)
                    .setTimeToLiveSeconds(1000)
                    .setStatisticsEnabled(true)
            );
}

}`

在启动应用程序期间,我只看到此日志

2019-11-30 19:56:01.579  INFO 13444 --- [           main] com.hazelcast.instance.AddressPicker     : [LOCAL] [dev] [3.12.4] Prefer IPv4 stack is true, prefer IPv6 addresses is false
2019-11-30 19:56:01.671  INFO 13444 --- [           main] com.hazelcast.instance.AddressPicker     : [LOCAL] [dev] [3.12.4] Picked [192.168.43.2]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
2019-11-30 19:56:01.694  INFO 13444 --- [           main] com.hazelcast.system                     : [192.168.43.2]:5701 [dev] [3.12.4] Hazelcast 3.12.4 (20191030 - eab1290) starting at [192.168.43.2]:5701
2019-11-30 19:56:01.695  INFO 13444 --- [           main] com.hazelcast.system                     : [192.168.43.2]:5701 [dev] [3.12.4] Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
2019-11-30 19:56:02.037  INFO 13444 --- [           main] c.h.s.i.o.impl.BackpressureRegulator     : [192.168.43.2]:5701 [dev] [3.12.4] Backpressure is disabled
2019-11-30 19:56:02.761  INFO 13444 --- [           main] com.hazelcast.instance.Node              : [192.168.43.2]:5701 [dev] [3.12.4] Creating MulticastJoiner
2019-11-30 19:56:02.998  INFO 13444 --- [           main] c.h.s.i.o.impl.OperationExecutorImpl     : [192.168.43.2]:5701 [dev] [3.12.4] Starting 4 partition threads and 3 generic threads (1 dedicated for priority tasks)
2019-11-30 19:56:02.999  INFO 13444 --- [           main] c.h.internal.diagnostics.Diagnostics     : [192.168.43.2]:5701 [dev] [3.12.4] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-11-30 19:56:03.007  INFO 13444 --- [           main] com.hazelcast.core.LifecycleService      : [192.168.43.2]:5701 [dev] [3.12.4] [192.168.43.2]:5701 is STARTING
2019-11-30 19:56:05.085  INFO 13444 --- [           main] c.h.internal.cluster.ClusterService      : [192.168.43.2]:5701 [dev] [3.12.4] 

Members {size:1, ver:1} [
    Member [192.168.43.2]:5701 - 6ed511ff-b20b-4875-9b39-2dc734d4a9aa this
]

2019-11-30 19:56:05.142  INFO 13444 --- [           main] com.hazelcast.core.LifecycleService      : [192.168.43.2]:5701 [dev] [3.12.4] [192.168.43.2]:5701 is STARTED
2019-11-30 19:56:05.295  INFO 13444 --- [e.HealthMonitor] c.h.internal.diagnostics.HealthMonitor   : [192.168.43.2]:5701 [dev] [3.12.4] processors=4, physical.memory.total=23,9G, physical.memory.free=11,8G, swap.space.total=27,2G, swap.space.free=10,3G, heap.memory.used=306,9M, heap.memory.free=357,1M, heap.memory.total=664,0M, heap.memory.max=5,3G, heap.memory.used/total=46,23%, heap.memory.used/max=5,63%, minor.gc.count=0, minor.gc.time=0ms, major.gc.count=0, major.gc.time=0ms, load.process=100,00%, load.system=100,00%, load.systemAverage=n/a thread.count=37, thread.peakCount=37, cluster.timeDiff=0, event.q.size=0, executor.q.async.size=0, executor.q.client.size=0, executor.q.client.query.size=0, executor.q.client.blocking.size=0, executor.q.query.size=0, executor.q.scheduled.size=0, executor.q.io.size=0, executor.q.system.size=0, executor.q.operations.size=0, executor.q.priorityOperation.size=0, operations.completed.count=1, executor.q.mapLoad.size=0, executor.q.mapLoadAllKeys.size=0, executor.q.cluster.size=0, executor.q.response.size=0, operations.running.count=0, operations.pending.invocations.percentage=0,00%, operations.pending.invocations.count=0, proxy.count=0, clientEndpoint.count=0, connection.active.count=0, client.connection.count=0, connection.count=0

在自动装配类 CacheManager 并在其上运行函数 getCacheNames 后,我看到一个空列表,我不知道为什么?

并在日志中首次添加到缓存中显示

2019-11-30 20:11:12.760  INFO 16464 --- [nio-8080-exec-6] c.h.i.p.impl.PartitionStateManager       : [192.168.43.2]:5701 [dev] [3.12.4] Initializing cluster partition table arrangement...

在 Metrics Config 文件中我有这个,并且不会显示任何指标:

    @Autowired
private CacheMetricsRegistrar cacheMetricsRegistrar;

有人知道为什么它不起作用吗?

【问题讨论】:

    标签: java spring-boot caching hazelcast metrics


    【解决方案1】:

    当您将第一个条目插入缓存时,缓存会被初始化。这就是为什么一开始在CacheManager 中看不到任何缓存的原因。插入第一个值后,您应该会在 cacheManager.caches 中看到 HazelcastCache。

    CacheMetricsRegistrar 也一样,我只是试了一下,在binderProviders 中看到了HazelcastCacheMeterBinderProvider。

    【讨论】:

      【解决方案2】:

      对于 Spring Boot 2.x,创建您的注册组件:

      @Component
      @AllArgsConstructor
      public class CacheMetricsRegistrator {
      
        private final CacheMetricsRegistrar cacheMetricsRegistrar;
        private final CacheManager cacheManager;
        private final Config cacheConfig;
          
        @PostConstruct
        public void register() {
          this.cacheConfig.getMapConfigs().keySet().forEach(
              cacheName -> this.cacheMetricsRegistrar.bindCacheToRegistry(
                 this.cacheManager.getCache(cacheName))
          );
        } 
      }
      

      为您的 Hazelcast 缓存配置:

      @EnableCaching
      @Configuration
      public class HazelcastConfig {
      
          private MapConfig mapPortfolioCache() {
              return new MapConfig()
                  .setName("my-entity-cache")
                  .setEvictionConfig(new EvictionConfig().setMaxSizePolicy(MaxSizePolicy.FREE_HEAP_SIZE).setSize(200))
                  .setTimeToLiveSeconds(60*15);
          }
      
          @Bean
          public Config hazelCastConfig() {
          
            Config config = new Config()
              .setInstanceName("my-application-hazelcast")
              .setNetworkConfig(new NetworkConfig().setJoin(new JoinConfig().setMulticastConfig(new MulticastConfig().setEnabled(false))))
              .addMapConfig(mapPortfolioCache());
          
            SubZero.useAsGlobalSerializer(config);
            return config;        
          }   
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-11
        • 2019-03-16
        • 2020-03-15
        • 2018-02-07
        相关资源
        最近更新 更多