Dew provides abstracted interfaces for distributed capabilities, supporting Redis, Hazelcast, and RabbitMQ.
Supported Features by Implementation
|=
| Feature | Redis | Hazelcast | Rabbit | MQTT
| Distributed Cache | * | | |
| Distributed Map | * | * | |
| Distributed Lock | * | * | |
| MQ | * | * | * | * (pub-sub only)
| Leader Election | * | | |
|=
Enabling Cluster Support
- Add
boot-starter. - Add a specific SPI dependency (e.g.,
cluster-spi-redis). - Configure the implementation in
application.yml under dew.cluster.
Distributed Cache
Access via Dew.cluster.cache.
Dew.cluster.cache.set("key", "value", 1);
String val = $.json.toJson(Dew.cluster.cache.get("key"));
Distributed Map
Create named instances of distributed maps.
ClusterMap<TestMapObj> mapObj = Dew.cluster.map.instance("test_obj_map", TestMapObj.class);
mapObj.put("test", new TestMapObj());
Distributed Lock
Use tryLock or the simplified tryLockWithFun to ensure automatic unlocking.
ClusterLock lock = Dew.cluster.lock.instance("test_lock");
// Automatically unlocks after 1s if not manually released
if (lock.tryLock(0, 1000)) {
try {
// business logic
} finally {
lock.unLock();
}
}
// Simplified version
lock.tryLockWithFun(0, 1000, () -> {
// business logic (auto-unlocks)
});
Message Queue (MQ)
Supports pub-sub and req-resp patterns.
// Pub-Sub
Dew.cluster.mq.subscribe("topic", msg -> logger.info(msg.getBody()));
Dew.cluster.mq.publish("topic", "payload");
// Req-Resp
Dew.cluster.mq.response("topic", msg -> logger.info(msg.getBody()));
Dew.cluster.mq.request("topic", "request_data");
Note: For pub-sub, the topic must exist; subscribe will create it automatically.
Leader Election
ClusterElection election = Dew.cluster.election.instance("election_name");
if (election.isLeader()) {
// perform leader tasks
}
// Distributed Lock Example
ClusterLock lock = Dew.cluster.lock.instance("my_lock");
lock.tryLockWithFun(0, 5000, () -> {
// Critical section
});