You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

关于Hazelcast集群配置的验证问询:单一集群是否可同时承载Hibernate二级缓存、应用级缓存与发布/订阅消息

Hazelcast集群配置验证:单一集群是否可同时承载Hibernate二级缓存、应用级缓存与发布/订阅消息

是的,你的当前配置思路完全正确,用同一个Hazelcast集群同时处理Hibernate二级缓存、应用级缓存和发布/订阅消息是完全可行的,下面我来结合你的配置和代码逐一验证说明:

一、集群基础配置的有效性

你的双节点集群配置符合生产环境要求,能够正常组成单一集群:

  • 统一的集群名称access-cache确保两个节点能加入同一集群
  • 禁用了多播,启用TCP-IP直连模式,明确配置了两个节点的IP和端口,避免了多播的网络不确定性
  • 心跳超时和操作超时参数配置合理,能应对短时间的网络波动
  • 注意:public-address配置需要每个节点单独设置:节点1的配置里填ip1:5701,节点2的配置里填ip2:5701,不能写成ip1/ip2:5701,否则会导致集群发现问题

修正后的单节点示例配置(以节点1为例):

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hazelcast.com/schema/config
           https://www.hazelcast.com/schema/config/hazelcast-config-5.5.xsd">
    <cluster-name>access-cache</cluster-name>
    <network>
        <public-address>ip1:5701</public-address>
        <interfaces enabled="false"/>
        <port auto-increment="false">5701</port>
        <join>
            <multicast enabled="false"/>
            <tcp-ip enabled="true">
                <member>ip1:5701</member>
                <member>ip2:5701</member>
            </tcp-ip>
        </join>
    </network>
    <properties>
        <property name="hazelcast.max.no.heartbeat.seconds">30</property>
        <property name="hazelcast.operation.call.timeout.millis">60000</property>
    </properties>
    <!-- 其余Map、Topic配置保持不变 -->
</hazelcast>

二、三个业务场景的配置支持

你的配置已经为三个场景分别提供了对应的集群资源,且都归属同一集群:

1. Hibernate二级缓存支持

你定义了通配符hibernate.*的Map配置,完全适配Hibernate的二级缓存需求:

  • backup-count=1确保缓存数据在两个节点间同步,避免单节点故障导致缓存失效
  • time-to-live-secondsmax-idle-seconds配置能自动清理过期缓存,避免内存溢出

2. 应用级缓存支持

你为每个业务缓存定义了独立的cache_*前缀Map,每个Map都配置了备份数和TTL,能直接和Spring的@Cacheable@CacheEvict注解配合使用,实现跨节点的应用级缓存同步。

3. 发布/订阅消息支持

你配置了可靠主题distributed-events并绑定了RingBuffer,代码中通过同一个hazelcastPubSub实例获取主题,实现双节点间的消息同步:

  • 可靠主题的BLOCK过载策略能避免消息丢失
  • 消息监听代码会将分布式消息转为本地应用事件,实现跨节点的websocket通知同步

对应的Java代码配置也确保了使用同一集群实例:

@Configuration
public class HazelcastPubSubConfig {

    /**
     * Creates a Hazelcast instance for pub/sub communication.
     *
     * @return the configured {@link HazelcastInstance}
     * @throws IOException if {@code hazelcast.xml} cannot be read
     */
    @Bean(name = "hazelcastPubSub")
    public HazelcastInstance hazelcastPubSubInstance() throws IOException {
        InputStream is = new ClassPathResource("hazelcast.xml").getInputStream();
        Config config = new XmlConfigBuilder(is).build();
        return Hazelcast.newHazelcastInstance(config);
    }
}
@Component
public class DistributedEventListener {

    private final ObjectMapper mapper;

    /**
     * Constructs a DistributedEventListener.
     *
     * @param hz          the Hazelcast instance for subscribing to topics
     * @param multicaster the application event multicaster for local event broadcasting
     * @param mapper      the ObjectMapper for JSON serialization/deserialization
     */
    public DistributedEventListener(@Qualifier("hazelcastPubSub") HazelcastInstance hz,
                                    @Qualifier("applicationEventMulticaster") ApplicationEventMulticaster multicaster,
                                    ObjectMapper mapper) {
        this.mapper = mapper;

        ITopic<String> topic = hz.getReliableTopic("distributed-events");
        topic.addMessageListener(msg -> {
            try {
                String json = msg.getMessageObject();
                StreamNotification<?> event =
                        mapper.readValue(json, StreamNotification.class);

                multicaster.multicastEvent(new PayloadApplicationEvent<>(this, event));
            } catch (IOException e) {
                throw new RuntimeException("Error deserializing event", e);
            }
        });
    }
}
@Service
public class DistributedEventPublisher {

    private final ITopic<String> topic;
    private final ObjectMapper mapper;

    /**
     * Constructs a HazelcastPubSubConfig.
     *
     * @param hz     the Hazelcast instance for publishing to topics
     * @param mapper the ObjectMapper for JSON serialization
     */
    public DistributedEventPublisher(@Qualifier("hazelcastPubSub") HazelcastInstance hz, ObjectMapper mapper) {
        this.topic = hz.getReliableTopic("distributed-events");
        this.mapper = mapper;
    }

    /**
     * Publish a StreamNotification event to the distributed topic.
     *
     * @param event the event to publish
     * @throws RuntimeException if serialization fails
     */
    public void publish(StreamNotification<?> event) {
        try {
            String json = mapper.writeValueAsString(event);
            topic.publish(json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Error serializing event: ", e);
        }
    }
}

三、最后一个小优化点

请确保每个节点的public-address配置为自身节点的IP地址(不要同时写两个IP),这样能保证集群节点间的心跳和通信正常,避免出现集群分裂或节点无法加入的问题。

总结来说,你的方案完全可行,单一Hazelcast集群完全可以同时承载这三类业务负载,只需要修正public-address的节点专属配置即可稳定运行。

火山引擎 最新活动