SpringBoot與Hazelcast整合,實現雙11大促情景下購物車數據同步與自動故障轉移
作者:Java知識日歷
在節假日和促銷期間,系統需要處理大量的并發請求。現有的購物車系統往往存在性能瓶頸和數據一致性問題,所以,我們現在需要一個高效的解決方案來解決這些問題。
在節假日和促銷期間,系統需要處理大量的并發請求。現有的購物車系統往往存在性能瓶頸和數據一致性問題,所以,我們現在需要一個高效的解決方案來解決這些問題。
哪些公司使用了Hazelcast?
Uber
- 用途: Uber在其移動應用和后臺服務中使用Hazelcast來緩存頻繁訪問的數據。
- 優勢: Hazelcast的高效數據存儲和快速讀寫能力提高了系統的整體性能。
Adobe
- 用途: Adobe在其數字營銷平臺中使用Hazelcast來管理用戶會話和緩存。
- 優勢: Hazelcast的靈活性和可擴展性使其能夠適應不斷變化的業務需求。
eBay
- 用途: eBay使用Hazelcast來管理會話狀態和緩存,支持其全球范圍內的流量。
- 優勢: Hazelcast的水平擴展能力和數據一致性保障使其成為理想的解決方案。
PayPal
- 用途: PayPal使用Hazelcast來加速交易處理和提高支付系統的響應速度。
- 優勢: Hazelcast的強一致性和可靠性保證了金融交易的安全性和準確性。
Netflix
- 用途: Netflix使用Hazelcast來緩存推薦系統中的數據,提高推薦算法的響應速度和準確性。
- 優勢: Hazelcast的高性能和低延遲特性非常適合處理大規模的數據訪問需求。
- 用途: LinkedIn利用Hazelcast進行會話管理和緩存,提升用戶體驗。
- 優勢: Hazelcast的分布式特性和自動故障轉移功能確保了系統的高可用性。
Telenor
- 用途: Telenor(挪威最大的電信運營商之一)使用Hazelcast來優化其網絡基礎設施和服務。
- 優勢: Hazelcast的高可用性和數據同步功能提升了電信服務的質量和效率。
Bosch
- 用途: Bosch在其工業自動化系統中使用Hazelcast來管理和分析傳感器數據。
- 優勢: Hazelcast的實時數據處理能力幫助Bosch實現了高效的工業物聯網解決方案。
我們項目選擇Hazelcast的原因
并發處理
- 線程安全: 內置的并發控制機制,支持高并發環境下的數據一致性。
- 低延遲: 設計優化以減少延遲,提升整體系統性能。
分布式緩存
- 實時數據訪問: Hazelcast提供內存級別的數據存儲,確保快速的數據讀寫操作。
- 自動分片: 數據自動分布在多個節點上,支持水平擴展,輕松應對大規模數據。
強一致性模型
- 分布式鎖: 提供分布式鎖機制,確保數據操作的一致性。
- 復制和備份: 數據在多個節點之間進行復制和備份,防止數據丟失。
自動故障轉移
- 健康檢查: 集成Spring Boot Actuator進行健康檢查,自動檢測節點狀態。
- 數據遷移: 當某個節點宕機時,Hazelcast會自動將數據遷移到其他可用節點,確保服務連續性。
數據結構
- IMap: 類似于Java中的HashMap,支持高效的鍵值對存儲。
- IQueue: 支持分布式隊列,適用于消息傳遞場景。
- MultiMap: 支持一個鍵對應多個值的映射關系。
查詢和聚合
- SQL查詢: 支持類似SQL的查詢語法,方便復雜的數據檢索。
- 聚合函數: 提供內置的聚合函數,簡化數據分析任務。
開源免費
- 開源許可證: Hazelcast Community Edition完全免費,適合中小型企業和個人開發者。
- 企業版優勢: 企業版提供更多高級功能和商業支持,可根據需求選擇合適的版本。
實現思路
1. 數據同步
- 功能描述: 使用Hazelcast的分區表(Partitioned Map)技術,確保用戶在多個服務器節點間操作購物車數據時的一致性。
- 實現方式: Hazelcast會自動管理和同步數據,即使在多臺服務器之間也能保持數據的一致性。
2. 自動故障轉移
- 功能描述: 結合Spring Boot Actuator進行健康檢查,當某個節點發生故障時,自動將該節點上的會話數據遷移到其他可用節點,避免用戶因服務器宕機而丟失購物車信息。
- 實現方式: Spring Boot Actuator提供健康檢查端點,Hazelcast負責數據遷移。
代碼實操
<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") // 設置Hazelcast實例名稱
.addMapConfig(new com.hazelcast.config.MapConfig()
.setName("users")); // 添加一個名為"users"的分區表(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; // 商品數量
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;
}
}
數據訪問層
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<>();
}
}
服務層
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."); // 返回成功響應
}
// 獲取購物車中某個商品的信息
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."); // 返回成功響應
}
// 清空用戶的整個購物車
public ResponseEntity<?> clearCart(String userId) {
if (!userRepository.containsUser(userId)) {
return ResponseEntity.notFound().build(); // 如果用戶不存在,返回404 Not Found
}
userRepository.clearCart(userId);
return ResponseEntity.ok("Cart cleared."); // 返回成功響應
}
// 獲取用戶購物車中的所有商品
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); // 調用服務層的方法
}
// 獲取購物車中某個商品信息的API接口
@GetMapping("/get")
public ResponseEntity<?> getFromCart(@RequestParam String userId,
@RequestParam String itemId) {
return shoppingCartService.getItem(userId, itemId); // 調用服務層的方法
}
// 從購物車中移除某個商品的API接口
@DeleteMapping("/remove")
public ResponseEntity<?> removeFromCart(@RequestParam String userId,
@RequestParam String itemId) {
return shoppingCartService.removeItem(userId, itemId); // 調用服務層的方法
}
// 清空用戶整個購物車的API接口
@DeleteMapping("/clear")
public ResponseEntity<?> clearCart(@RequestParam String userId) {
return shoppingCartService.clearCart(userId); // 調用服務層的方法
}
// 獲取用戶購物車中所有商品的API接口
@GetMapping("/all")
public ResponseEntity<?> getAllItems(@RequestParam String userId) {
return shoppingCartService.getAllItems(userId); // 調用服務層的方法
}
}
測試
添加商品到購物車
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}
增加現有商品的數量
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."
責任編輯:武曉燕
來源:
Java知識日歷