You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hazelcast客户端-服务端架构同时支持IMap与ICache的问题

Hazelcast多客户端兼容问题解决方案

问题场景

部署了同时支持IMap和ICache的Hazelcast Server,其中一个客户端在限流场景下用ICache存储数据,另一个客户端需用IMap存储。但当其中一个客户端使用ICache后,另一个客户端尝试以IMap启动时抛出异常:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-04-24T06:34:12.099+0000 ERROR o.s.b.SpringApplication:reportFailure:821 -  -  | - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration]: Constructor threw exception; nested exception is com.hazelcast.client.impl.spi.impl.ClientServiceNotFoundException: No factory registered for service: hz:impl:cacheService
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:315) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:296) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.25.jar!/:5.3.25]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.25.jar!/:5.3.25]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.25.jar!/:5.3.25]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.9.jar!/:2.7.9]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.9.jar!/:2.7.9]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.9.jar!/:2.7.9]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.9.jar!/:2.7.9]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.7.9.jar!/:2.7.9]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.7.9.jar!/:2.7.9]

已尝试切换Hazelcast版本,问题未解决。

问题原因

异常核心是Spring Boot Actuator的CacheMetricsRegistrarConfiguration自动配置类尝试初始化缓存指标,但仅使用IMap的客户端没有配置Hazelcast Cache服务,导致找不到对应的服务工厂。即使服务端支持ICache,客户端如果不需要使用ICache,也会因为依赖或自动配置触发这个问题。

解决方案

1. 排除不必要的自动配置

在仅使用IMap的客户端项目中,禁用CacheMetrics相关的自动配置:

  • 方式一:启动类注解排除
    在Spring Boot启动类上添加:
@SpringBootApplication(exclude = CacheMetricsRegistrarConfiguration.class)
  • 方式二:配置文件排除
    在application.properties中添加:
spring.autoconfigure.exclude=org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration

或者在application.yml中添加:

spring:
  autoconfigure:
    exclude: org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration

2. 清理客户端依赖

如果仅使用IMap的客户端项目中引入了Hazelcast Cache相关依赖(比如hazelcast-spring中包含的cache模块),可以排除掉Cache相关的依赖:

<!-- Maven示例,排除cache模块 -->
<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast-spring</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-cache</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 检查客户端配置

确保仅使用IMap的客户端没有配置任何Hazelcast Cache相关的Bean(比如HazelcastCacheManager),避免Spring容器自动识别并初始化缓存服务。

4. 确认服务端配置

检查Hazelcast Server的配置文件(如hazelcast.xml),确保同时启用了IMap和ICache服务,示例配置:

<hazelcast>
    <!-- IMap配置 -->
    <map name="*">
        <backup-count>1</backup-count>
    </map>
    <!-- ICache配置 -->
    <cache name="*">
        <key-type class-name="java.lang.String"/>
        <value-type class-name="java.lang.Object"/>
    </cache>
</hazelcast>

内容的提问来源于stack exchange,提问作者naveen nallusamy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 00:47:57