雙11購(gòu)物車黑科技!SpringBoot + Hazelcast 實(shí)現(xiàn)數(shù)據(jù)同步與故障轉(zhuǎn)移
在分布式系統(tǒng)架構(gòu)中,數(shù)據(jù)同步和故障轉(zhuǎn)移(Failover)是核心問題之一。Hazelcast 作為一種高性能的內(nèi)存數(shù)據(jù)網(wǎng)格(IMDG),可以幫助我們實(shí)現(xiàn)分布式數(shù)據(jù)共享、緩存以及容錯(cuò)機(jī)制,確保應(yīng)用高可用性。本文將介紹如何在 Spring Boot 應(yīng)用中集成 Hazelcast,實(shí)現(xiàn)數(shù)據(jù)同步和故障轉(zhuǎn)移,并提供完整的示例代碼。
Hazelcast 簡(jiǎn)介
Hazelcast 是一個(gè)開源的內(nèi)存計(jì)算平臺(tái),提供分布式數(shù)據(jù)存儲(chǔ)、緩存和流處理功能,支持 Java、.NET、Node.js 等多種語(yǔ)言。它的核心特性包括:
- 對(duì)等架構(gòu)無中心化節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)地位相同,避免單點(diǎn)故障。
- 自動(dòng)發(fā)現(xiàn)節(jié)點(diǎn)可以自動(dòng)發(fā)現(xiàn)并加入 Hazelcast 集群。
- 分區(qū)存儲(chǔ)數(shù)據(jù)在集群中的多個(gè)節(jié)點(diǎn)間進(jìn)行分片存儲(chǔ),提高性能和可靠性。
- 多種數(shù)據(jù)結(jié)構(gòu)支持 IMap、MultiMap、Queue、Set、List 等數(shù)據(jù)結(jié)構(gòu)。
- 持久化支持將數(shù)據(jù)持久化到磁盤,避免數(shù)據(jù)丟失。
官方網(wǎng)站:https://hazelcast.com/ 開源項(xiàng)目地址:https://github.com/hazelcast/hazelcast
引入 Hazelcast 依賴
在 pom.xml 文件中添加 Hazelcast 相關(guān)依賴:
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
Hazelcast 配置
在 com.icoderoad.config.HazelcastConfig 類中配置 Hazelcast:
package com.icoderoad.config;
import com.hazelcast.config.Config;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.JoinConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HazelcastConfig {
@Bean
public Config hazelcastConfig() {
Config config = new Config();
config.setInstanceName("hazelcast-instance");
NetworkConfig networkConfig = config.getNetworkConfig();
JoinConfig joinConfig = networkConfig.getJoin();
joinConfig.getMulticastConfig().setEnabled(true);
return config;
}
}
創(chuàng)建 Hazelcast 服務(wù)類
在 com.icoderoad.service.CacheService 中創(chuàng)建數(shù)據(jù)存取服務(wù):
package com.icoderoad.service;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class CacheService {
private final HazelcastInstance hazelcastInstance;
public CacheService(HazelcastInstance hazelcastInstance) {
this.hazelcastInstance = hazelcastInstance;
}
public void putValue(String key, String value) {
IMap<String, String> map = hazelcastInstance.getMap("distributed-cache");
map.put(key, value);
}
public String getValue(String key) {
IMap<String, String> map = hazelcastInstance.getMap("distributed-cache");
return map.get(key);
}
}
創(chuàng)建 REST 接口
在 com.icoderoad.controller.CacheController 類中創(chuàng)建 REST API:
package com.icoderoad.controller;
import com.icoderoad.service.CacheService;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/cache")
public class CacheController {
private final CacheService cacheService;
public CacheController(CacheService cacheService) {
this.cacheService = cacheService;
}
@PostMapping("/put")
public String put(@RequestParam String key, @RequestParam String value) {
cacheService.putValue(key, value);
return "Success";
}
@GetMapping("/get")
public String get(@RequestParam String key) {
return cacheService.getValue(key);
}
}
測(cè)試 Hazelcast 數(shù)據(jù)同步
運(yùn)行多個(gè) Spring Boot 實(shí)例,并在一個(gè)實(shí)例中存儲(chǔ)數(shù)據(jù):
curl -X POST "http://localhost:8080/cache/put?key=test&value=hello"
然后在另一個(gè)實(shí)例中獲取數(shù)據(jù):
curl -X GET "http://localhost:8080/cache/get?key=test"
你會(huì)發(fā)現(xiàn)數(shù)據(jù)可以在多個(gè)實(shí)例之間共享。
結(jié)論
本文介紹了 Hazelcast 的基本概念,并展示了如何在 Spring Boot 中集成 Hazelcast 進(jìn)行分布式緩存管理。Hazelcast 作為一款強(qiáng)大的緩存框架,提供了對(duì)等架構(gòu)、自動(dòng)擴(kuò)展和數(shù)據(jù)持久化等特性,適用于高并發(fā)場(chǎng)景。
相比于 Redis,Hazelcast 適用于對(duì) JVM 友好的分布式環(huán)境,支持更豐富的數(shù)據(jù)結(jié)構(gòu),并且無需額外的外部依賴。如果你的應(yīng)用需要高性能分布式緩存,不妨嘗試 Hazelcast。
你可以進(jìn)一步擴(kuò)展本示例,如:
- 結(jié)合 Spring Cache 進(jìn)行注解式緩存管理
- 配置 Hazelcast 集群,實(shí)現(xiàn)高可用性
- 結(jié)合持久化存儲(chǔ),確保數(shù)據(jù)可靠性