Hazelcast嵌入式集群疑问:newHazelcastInstance无法共享Map,是否需用客户端?
Great question—let’s start by clearing up a key misunderstanding: you absolutely do NOT need to use HazelcastClient to share data between embedded Hazelcast nodes. In fact, the entire point of an embedded cluster is that instances created with Hazelcast.newHazelcastInstance() are full cluster members that automatically sync distributed data structures like Map.
Your issue where Map changes aren’t syncing between nodes almost certainly means your two instances aren’t actually forming a single cluster. Let’s break down the root causes and how to fix them:
Common Reasons Your Embedded Nodes Aren’t Syncing
1. Mismatched Cluster Names
By default, Hazelcast uses the cluster name dev. If one of your instances is configured with a different cluster name, it will join a separate cluster entirely.
To fix this, explicitly set the same cluster name for all instances:
Config config = new Config(); config.setClusterName("my-shared-cluster"); // Use the exact same name everywhere HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
2. Cluster Discovery Failure
Hazelcast uses multicast by default to find other nodes. This can fail if:
- Multicast is disabled on your network
- You’re running nodes in isolated environments (like Docker containers without proper network setup)
- Firewalls are blocking Hazelcast’s default ports (5701-5703)
First, verify if your nodes are in the same cluster. Add this code after creating each instance:
Cluster cluster = instance.getCluster(); System.out.println("Current cluster members: " + cluster.getMembers());
If the output only shows one member per instance, your nodes aren’t discovering each other. Switch to TCP/IP discovery for reliable node detection:
Config config = new Config(); JoinConfig joinConfig = config.getNetworkConfig().getJoin(); // Disable multicast joinConfig.getMulticastConfig().setEnabled(false); // Enable TCP/IP and list your node addresses joinConfig.getTcpIpConfig() .setEnabled(true) .addMember("127.0.0.1:5701") // Replace with your actual node IPs/ports .addMember("127.0.0.1:5702"); HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
3. Accidental Use of Local (Non-Distributed) Data Structures
Double-check that you’re using the distributed IMap and not a local map. The default instance.getMap("my-map") returns a distributed IMap, but if you’ve explicitly configured a local map (e.g., via custom providers), changes won’t sync. Stick to the standard IMap API for shared data.
When Should You Use HazelcastClient?
Clients are useful for scenarios where:
- Your application doesn’t need to act as a cluster member (e.g., a lightweight service that only reads/writes data without storing it)
- You want to isolate your application from cluster resource usage (members handle data storage/replication; clients just connect)
- You’re connecting to a remote Hazelcast cluster (not embedded within your app)
But for embedded clusters, using Hazelcast.newHazelcastInstance() is the correct approach—members are designed to share data seamlessly once they’re in the same cluster.
Final Verification Steps
- Confirm both instances use the exact same cluster name
- Check logs for messages like
Members {size:2}to confirm cluster formation - Test basic Map operations: put a value on one node, then get it on the other. If it works, your cluster is functioning correctly.
内容的提问来源于stack exchange,提问作者NishM

