SpringBoot與Hazelcast整合,實現(xiàn)雙11大促情景下購物車數(shù)據(jù)同步與自動故障轉(zhuǎn)移
作者:Java知識日歷
在節(jié)假日和促銷期間,系統(tǒng)需要處理大量的并發(fā)請求。現(xiàn)有的購物車系統(tǒng)往往存在性能瓶頸和數(shù)據(jù)一致性問題,所以,我們現(xiàn)在需要一個高效的解決方案來解決這些問題。
在節(jié)假日和促銷期間,系統(tǒng)需要處理大量的并發(fā)請求。現(xiàn)有的購物車系統(tǒng)往往存在性能瓶頸和數(shù)據(jù)一致性問題,所以,我們現(xiàn)在需要一個高效的解決方案來解決這些問題。
哪些公司使用了Hazelcast?
Uber
- 用途: Uber在其移動應(yīng)用和后臺服務(wù)中使用Hazelcast來緩存頻繁訪問的數(shù)據(jù)。
- 優(yōu)勢: Hazelcast的高效數(shù)據(jù)存儲和快速讀寫能力提高了系統(tǒng)的整體性能。
Adobe
- 用途: Adobe在其數(shù)字營銷平臺中使用Hazelcast來管理用戶會話和緩存。
- 優(yōu)勢: Hazelcast的靈活性和可擴展性使其能夠適應(yīng)不斷變化的業(yè)務(wù)需求。
eBay
- 用途: eBay使用Hazelcast來管理會話狀態(tài)和緩存,支持其全球范圍內(nèi)的流量。
- 優(yōu)勢: Hazelcast的水平擴展能力和數(shù)據(jù)一致性保障使其成為理想的解決方案。
PayPal
- 用途: PayPal使用Hazelcast來加速交易處理和提高支付系統(tǒng)的響應(yīng)速度。
- 優(yōu)勢: Hazelcast的強一致性和可靠性保證了金融交易的安全性和準(zhǔn)確性。
Netflix
- 用途: Netflix使用Hazelcast來緩存推薦系統(tǒng)中的數(shù)據(jù),提高推薦算法的響應(yīng)速度和準(zhǔn)確性。
- 優(yōu)勢: Hazelcast的高性能和低延遲特性非常適合處理大規(guī)模的數(shù)據(jù)訪問需求。
- 用途: LinkedIn利用Hazelcast進行會話管理和緩存,提升用戶體驗。
- 優(yōu)勢: Hazelcast的分布式特性和自動故障轉(zhuǎn)移功能確保了系統(tǒng)的高可用性。
Telenor
- 用途: Telenor(挪威最大的電信運營商之一)使用Hazelcast來優(yōu)化其網(wǎng)絡(luò)基礎(chǔ)設(shè)施和服務(wù)。
- 優(yōu)勢: Hazelcast的高可用性和數(shù)據(jù)同步功能提升了電信服務(wù)的質(zhì)量和效率。
Bosch
- 用途: Bosch在其工業(yè)自動化系統(tǒng)中使用Hazelcast來管理和分析傳感器數(shù)據(jù)。
- 優(yōu)勢: Hazelcast的實時數(shù)據(jù)處理能力幫助Bosch實現(xiàn)了高效的工業(yè)物聯(lián)網(wǎng)解決方案。
我們項目選擇Hazelcast的原因
并發(fā)處理
- 線程安全: 內(nèi)置的并發(fā)控制機制,支持高并發(fā)環(huán)境下的數(shù)據(jù)一致性。
- 低延遲: 設(shè)計優(yōu)化以減少延遲,提升整體系統(tǒng)性能。
分布式緩存
- 實時數(shù)據(jù)訪問: Hazelcast提供內(nèi)存級別的數(shù)據(jù)存儲,確??焖俚臄?shù)據(jù)讀寫操作。
- 自動分片: 數(shù)據(jù)自動分布在多個節(jié)點上,支持水平擴展,輕松應(yīng)對大規(guī)模數(shù)據(jù)。
強一致性模型
- 分布式鎖: 提供分布式鎖機制,確保數(shù)據(jù)操作的一致性。
- 復(fù)制和備份: 數(shù)據(jù)在多個節(jié)點之間進行復(fù)制和備份,防止數(shù)據(jù)丟失。
自動故障轉(zhuǎn)移
- 健康檢查: 集成Spring Boot Actuator進行健康檢查,自動檢測節(jié)點狀態(tài)。
- 數(shù)據(jù)遷移: 當(dāng)某個節(jié)點宕機時,Hazelcast會自動將數(shù)據(jù)遷移到其他可用節(jié)點,確保服務(wù)連續(xù)性。
數(shù)據(jù)結(jié)構(gòu)
- IMap: 類似于Java中的HashMap,支持高效的鍵值對存儲。
- IQueue: 支持分布式隊列,適用于消息傳遞場景。
- MultiMap: 支持一個鍵對應(yīng)多個值的映射關(guān)系。
查詢和聚合
- SQL查詢: 支持類似SQL的查詢語法,方便復(fù)雜的數(shù)據(jù)檢索。
- 聚合函數(shù): 提供內(nèi)置的聚合函數(shù),簡化數(shù)據(jù)分析任務(wù)。
開源免費
- 開源許可證: Hazelcast Community Edition完全免費,適合中小型企業(yè)和個人開發(fā)者。
- 企業(yè)版優(yōu)勢: 企業(yè)版提供更多高級功能和商業(yè)支持,可根據(jù)需求選擇合適的版本。
實現(xiàn)思路
1. 數(shù)據(jù)同步
- 功能描述: 使用Hazelcast的分區(qū)表(Partitioned Map)技術(shù),確保用戶在多個服務(wù)器節(jié)點間操作購物車數(shù)據(jù)時的一致性。
- 實現(xiàn)方式: Hazelcast會自動管理和同步數(shù)據(jù),即使在多臺服務(wù)器之間也能保持數(shù)據(jù)的一致性。
2. 自動故障轉(zhuǎn)移
- 功能描述: 結(jié)合Spring Boot Actuator進行健康檢查,當(dāng)某個節(jié)點發(fā)生故障時,自動將該節(jié)點上的會話數(shù)據(jù)遷移到其他可用節(jié)點,避免用戶因服務(wù)器宕機而丟失購物車信息。
- 實現(xiàn)方式: Spring Boot Actuator提供健康檢查端點,Hazelcast負責(zé)數(shù)據(jù)遷移。
代碼實操
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Hazelcast Integration for Spring Boot -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring-boot-starter</artifactId>
<version>5.3.0</version>
</dependency>
<!-- Spring Boot Actuator for Health Checks -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
健康檢查配置 (application.properties)
配置啟用Actuator的健康檢查功能
management.endpoints.web.exposure.include=*
啟動類
package com.example.shoppingcart;
import com.hazelcast.config.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
publicclass ShoppingCartApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingCartApplication.class, args);
}
// 配置Hazelcast實例
@Bean
public Config hazelCastConfig() {
returnnew Config()
.setInstanceName("shopping-cart-instance") // 設(shè)置Hazelcast實例名稱
.addMapConfig(new com.hazelcast.config.MapConfig()
.setName("users")); // 添加一個名為"users"的分區(qū)表(IMap)
}
}
User 類
package com.example.shoppingcart.model;
import java.util.HashMap;
import java.util.Map;
publicclass User {
private String userId; // 用戶ID
private Map<String, CartItem> cartItems; // 購物車中的商品,鍵為商品ID,值為CartItem對象
public User(String userId) {
this.userId = userId;
this.cartItems = new HashMap<>();
}
public String getUserId() {
return userId;
}
public Map<String, CartItem> getCartItems() {
return cartItems;
}
}
CartItem 類
package com.example.shoppingcart.model;
publicclass CartItem {
private String itemId; // 商品ID
privateint quantity; // 商品數(shù)量
public CartItem(String itemId, int quantity) {
this.itemId = itemId;
this.quantity = quantity;
}
public String getItemId() {
return itemId;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
數(shù)據(jù)訪問層
package com.example.shoppingcart.repository;
import com.example.shoppingcart.model.CartItem;
import com.example.shoppingcart.model.User;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
publicclass UserRepository {
privatefinal IMap<String, User> users; // 使用Hazelcast的IMap存儲用戶信息
@Autowired
public UserRepository(HazelcastInstance hazelcastInstance) {
this.users = hazelcastInstance.getMap("users"); // 獲取名為"users"的IMap實例
}
public User getUser(String userId) {
return users.get(userId); // 從IMap中獲取用戶信息
}
public void saveUser(User user) {
users.put(user.getUserId(), user); // 將用戶信息保存到IMap中
}
public boolean containsUser(String userId) {
return users.containsKey(userId); // 檢查是否存在指定用戶
}
public boolean containsItem(String userId, String itemId) {
User user = getUser(userId);
return user != null && user.getCartItems().containsKey(itemId); // 檢查用戶是否包含指定商品
}
public void addItemToCart(String userId, CartItem item) {
User user = getUser(userId);
if (user == null) {
user = new User(userId);
}
user.getCartItems().put(item.getItemId(), item);
saveUser(user);
}
public CartItem getItemFromCart(String userId, String itemId) {
User user = getUser(userId);
if (user != null) {
return user.getCartItems().get(itemId);
}
returnnull;
}
public void removeItemFromCart(String userId, String itemId) {
User user = getUser(userId);
if (user != null && user.getCartItems().containsKey(itemId)) {
user.getCartItems().remove(itemId);
saveUser(user);
}
}
public void clearCart(String userId) {
User user = getUser(userId);
if (user != null) {
user.getCartItems().clear();
saveUser(user);
}
}
public Map<String, CartItem> getAllItemsInCart(String userId) {
User user = getUser(userId);
if (user != null) {
return user.getCartItems();
}
returnnew HashMap<>();
}
}
服務(wù)層
package com.example.shoppingcart.service;
import com.example.shoppingcart.model.CartItem;
import com.example.shoppingcart.model.User;
import com.example.shoppingcart.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
@Service
class ShoppingCartService {
privatefinal UserRepository userRepository;
@Autowired
public ShoppingCartService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@PostConstruct
public void init() {
}
// 添加商品到購物車的方法
public ResponseEntity<?> addItem(String userId, String itemId, int quantity) {
if (userRepository.containsItem(userId, itemId)) {
CartItem existingItem = userRepository.getItemFromCart(userId, itemId);
existingItem.setQuantity(existingItem.getQuantity() + quantity);
userRepository.saveUser(getUserWithUpdatedItem(userId, existingItem));
} else {
userRepository.addItemToCart(userId, new CartItem(itemId, quantity));
}
return ResponseEntity.ok("Item added to cart."); // 返回成功響應(yīng)
}
// 獲取購物車中某個商品的信息
public ResponseEntity<?> getItem(String userId, String itemId) {
CartItem item = userRepository.getItemFromCart(userId, itemId);
if (item == null) {
return ResponseEntity.notFound().build(); // 如果用戶或商品不存在,返回404 Not Found
}
return ResponseEntity.ok(item); // 返回商品信息
}
// 從購物車中移除某個商品
public ResponseEntity<?> removeItem(String userId, String itemId) {
if (!userRepository.containsItem(userId, itemId)) {
return ResponseEntity.notFound().build(); // 如果用戶或商品不存在,返回404 Not Found
}
userRepository.removeItemFromCart(userId, itemId);
return ResponseEntity.ok("Item removed from cart."); // 返回成功響應(yīng)
}
// 清空用戶的整個購物車
public ResponseEntity<?> clearCart(String userId) {
if (!userRepository.containsUser(userId)) {
return ResponseEntity.notFound().build(); // 如果用戶不存在,返回404 Not Found
}
userRepository.clearCart(userId);
return ResponseEntity.ok("Cart cleared."); // 返回成功響應(yīng)
}
// 獲取用戶購物車中的所有商品
public ResponseEntity<?> getAllItems(String userId) {
Map<String, CartItem> items = userRepository.getAllItemsInCart(userId);
if (items.isEmpty()) {
return ResponseEntity.notFound().build(); // 如果用戶不存在,返回404 Not Found
}
return ResponseEntity.ok(items); // 返回購物車中的所有商品
}
// 輔助方法:更新用戶信息并返回
private User getUserWithUpdatedItem(String userId, CartItem updatedItem) {
User user = userRepository.getUser(userId);
if (user != null) {
user.getCartItems().put(updatedItem.getItemId(), updatedItem);
}
return user;
}
}
控制器層
package com.example.shoppingcart.controller;
import com.example.shoppingcart.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/cart")
publicclass ShoppingCartController {
privatefinal ShoppingCartService shoppingCartService;
@Autowired
public ShoppingCartController(ShoppingCartService shoppingCartService) {
this.shoppingCartService = shoppingCartService;
}
// 添加商品到購物車的API接口
@PostMapping("/add")
public ResponseEntity<?> addToCart(@RequestParam String userId,
@RequestParam String itemId,
@RequestParamint quantity) {
return shoppingCartService.addItem(userId, itemId, quantity); // 調(diào)用服務(wù)層的方法
}
// 獲取購物車中某個商品信息的API接口
@GetMapping("/get")
public ResponseEntity<?> getFromCart(@RequestParam String userId,
@RequestParam String itemId) {
return shoppingCartService.getItem(userId, itemId); // 調(diào)用服務(wù)層的方法
}
// 從購物車中移除某個商品的API接口
@DeleteMapping("/remove")
public ResponseEntity<?> removeFromCart(@RequestParam String userId,
@RequestParam String itemId) {
return shoppingCartService.removeItem(userId, itemId); // 調(diào)用服務(wù)層的方法
}
// 清空用戶整個購物車的API接口
@DeleteMapping("/clear")
public ResponseEntity<?> clearCart(@RequestParam String userId) {
return shoppingCartService.clearCart(userId); // 調(diào)用服務(wù)層的方法
}
// 獲取用戶購物車中所有商品的API接口
@GetMapping("/all")
public ResponseEntity<?> getAllItems(@RequestParam String userId) {
return shoppingCartService.getAllItems(userId); // 調(diào)用服務(wù)層的方法
}
}
測試
添加商品到購物車
curl -X POST "http://localhost:8080/cart/add?userId=user1&itemId=item1&quantity=3"
Response:
"Item added to cart."
獲取購物車中某個商品的信息
curl "http://localhost:8080/cart/get?userId=user1&itemId=item1"
Response:
{"itemId":"item1","quantity":3}
增加現(xiàn)有商品的數(shù)量
curl -X POST "http://localhost:8080/cart/add?userId=user1&itemId=item1&quantity=2"
Response:
"Item added to cart."
從購物車中移除某個商品
curl -X DELETE "http://localhost:8080/cart/remove?userId=user1&itemId=item1"
Response:
"Item removed from cart."
清空用戶的整個購物車
curl -X DELETE "http://localhost:8080/cart/clear?userId=user1"
Response:
"Cart cleared."
責(zé)任編輯:武曉燕
來源:
Java知識日歷