高效的并發(fā)管理:房間預訂 API 的樂觀鎖和消息隊列
想象一下這樣一個場景:多名旅行者同時嘗試預訂熱門目的地的最后一個可用房間。如果沒有適當?shù)牟l(fā)控制機制,這種情況很快就會變成競爭狀態(tài),導致房間超額預訂和客戶沮喪。
我們將深入研究用于應對這些挑戰(zhàn)的兩種關鍵策略的復雜性:樂觀鎖定和消息隊列。
想象一下您正在使用一個在線酒店預訂平臺,類似于 Booking.com 或 Expedia 等知名平臺。以下是同步和異步流程如何發(fā)揮作用:
同步流程:
預訂房間(同步):
- 您訪問酒店預訂網站并選擇您的目的地、入住和退房日期以及其他偏好。
- 您點擊“立即預訂”按鈕即可預訂房間。
- 該網站使用基于 HTTP 的同步協(xié)議(如 REST 或 SOAP)將您的請求發(fā)送到酒店的預訂系統(tǒng)。
- 酒店的系統(tǒng)會立即同步處理您的請求。它檢查房間可用性,為您預訂房間,并生成預訂號碼。
- 預訂號碼將發(fā)送回您的瀏覽器,并在幾秒鐘內顯示在網站上。
- 您可以立即獲得預訂號碼,然后可以放心地繼續(xù)您的旅行計劃。
創(chuàng)建房間實體
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Room {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String roomType;
private boolean isAvailable;
// getters and setters
}
創(chuàng)建房間存儲庫
import org.springframework.data.jpa.repository.JpaRepository;
public interface RoomRepository extends JpaRepository<Room, Long> {
Room findByRoomType(String roomType);
}
創(chuàng)建客房預訂請求 DTO
import java.time.LocalDate;
public class RoomBookingRequest {
private String roomType;
private LocalDate checkInDate;
private LocalDate checkOutDate;
// getters and setters
}
創(chuàng)建客房預訂響應 DTO
public class RoomBookingResponse {
private String reservationNumber;
// getters and setters
}
創(chuàng)建客房服務
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class RoomService {
@Autowired
private RoomRepository roomRepository;
public RoomBookingResponse bookRoom(RoomBookingRequest bookingRequest) {
String roomType = bookingRequest.getRoomType();
LocalDate checkInDate = bookingRequest.getCheckInDate();
LocalDate checkOutDate = bookingRequest.getCheckOutDate();
Room room = roomRepository.findByRoomType(roomType);
if (room != null && room.isAvailable()) {
// Add validation to check availability based on check-in and check-out dates here.
// For simplicity, we'll assume the room is available.
room.setAvailable(false);
roomRepository.save(room);
// Generate a reservation number (you can implement your logic here).
String reservationNumber = generateReservationNumber();
return new RoomBookingResponse(reservationNumber);
} else {
throw new RoomNotAvailableException();
}
}
private String generateReservationNumber() {
// Generate a unique reservation number (you can implement your logic here).
return UUID.randomUUID().toString();
}
}
創(chuàng)建房間控制器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/rooms")
public class RoomController {
@Autowired
private RoomService roomService;
// Book a room
@PostMapping("/book")
public RoomBookingResponse bookRoom(@RequestBody RoomBookingRequest bookingRequest) {
return roomService.bookRoom(bookingRequest);
}
}
定義自定義異常
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class RoomNotAvailableException extends RuntimeException {
public RoomNotAvailableException() {
super("The requested room is not available.");
}
}
測試API
您可以使用 Postman 或 cURL 等工具來測試您的 API。要預訂房間,請http://localhost:8080/api/rooms/book使用包含房間類型、入住日期和退房日期的 JSON 正文發(fā)出 POST 請求:
{
"roomType" : "Standard" ,
"checkInDate" : "2023-10-01" ,
"checkOutDate" : "2023-10-05"
}
如果房間可用,API 將返回帶有預訂編號的 JSON 響應。您可以根據(jù)您的課堂需求自定義預訂邏輯和預訂號碼生成RoomService。
異步流程
當多個用戶同時調用Booking API時
當多個并發(fā)呼叫在系統(tǒng)中搜索同一房間時,可能存在潛在的缺點和挑戰(zhàn):
競爭條件:當多個請求嘗試同時預訂同一房間時,可能會出現(xiàn)競爭條件。如果處理不當,這可能會導致超額預訂,即系統(tǒng)允許的預訂數(shù)量超過了可用房間的數(shù)量。
如何解決并發(fā)問題?
樂觀鎖定是一種數(shù)據(jù)庫級技術,可防止多個用戶同時嘗試更新同一資源時發(fā)生數(shù)據(jù)沖突。
另一方面,消息隊列是異步通信工具,可確保請求的有序、可靠處理,使其成為分布式系統(tǒng)中處理并發(fā)請求的理想選擇。
方法一:實現(xiàn)消息隊列響應并發(fā)請求
消息隊列確保請求按照接收順序進行處理,從而防止競爭條件和超量預訂。
- 多個客戶端向端點發(fā)出 POST 請求/api/rooms/book以同時預訂酒店房間。
- 處理RoomController傳入的預訂請求。
- 該roomService.bookRoom方法接收預訂請求。
- 它使用該方法將預訂請求發(fā)送到名為“room-booking”的 RabbitMQ 消息隊列rabbitTemplate.convertAndSend。
- 它向客戶端返回初步響應,其中包含一條消息,表明預訂請求已發(fā)送,客戶端應等待確認。
- 預訂請求被放入“房間預訂”隊列中。消息隊列系統(tǒng)(在本例中為 RabbitMQ)確保每個預訂請求都按照收到的順序進行處理,以防止競爭情況。
- 監(jiān)聽RoomBookingMessageConsumer“房間預訂”隊列。
- processBookingRequest當預訂請求出隊時,將調用消費者的方法。在該方法中,您通常會實現(xiàn)以下邏輯:
- 根據(jù)請求的房型、入住日期和退房日期檢查客房供應情況。
- 如果房間可用,則生成預訂號碼。
- 更新數(shù)據(jù)庫中的房間可用性,將其標記為不可用,以防止重復預訂。
- 通過RabbitMQ向客戶端發(fā)送包含預約號的響應消息
8. 在 中RoomBookingMessageConsumer,處理預訂請求并生成預訂號碼后,您可以使用傳統(tǒng)的 HTTP 客戶端(例如RestTemplate、HttpClient)將確認響應直接發(fā)送到客戶端的回調 URL 端點(該端點在請求中發(fā)送)。
執(zhí)行:
創(chuàng)建客房預訂請求和響應 DTO
import java.time.LocalDate;
public class RoomBookingRequest {
private String roomType;
private LocalDate checkInDate;
private LocalDate checkOutDate;
private String clientCallbackUrl; // Added to specify the client's callback URL
// getters and setters
}
public class RoomBookingResponse {
private String reservationNumber;
// getters and setters
}
修改控制器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/rooms")
public class RoomController {
@Autowired
private RoomService roomService;
@PostMapping("/book")
public RoomBookingResponse bookRoom(@RequestBody RoomBookingRequest bookingRequest) {
return roomService.bookRoom(bookingRequest);
}
}
創(chuàng)建客房預訂服務(生產者)
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
@Service
public class RoomService {
@Autowired
private RoomRepository roomRepository;
@Autowired
private RabbitTemplate rabbitTemplate;
private RestTemplate restTemplate = new RestTemplate();
public RoomBookingResponse bookRoom(RoomBookingRequest bookingRequest) {
String roomType = bookingRequest.getRoomType();
// Send the booking request to the message queue
rabbitTemplate.convertAndSend("room-booking-exchange", "room-booking", bookingRequest);
return new RoomBookingResponse("Booking request sent. Please wait for confirmation.");
}
// This method sends the response to the client's callback URL
public void sendResponseToClient(RoomBookingResponse response, String clientCallbackUrl) {
ResponseEntity<Void> result = restTemplate.postForEntity(clientCallbackUrl, response, Void.class);
if (result.getStatusCode().is2xxSuccessful()) {
// Handle a successful response sent to the client
} else {
// Handle the case when the response to the client failed
}
}
}
創(chuàng)建消息消費者
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RoomBookingMessageConsumer {
@Autowired
private RoomService roomService;
@RabbitListener(queues = "room-booking-queue")
public void processBookingRequest(RoomBookingRequest bookingRequest) {
// Process the booking request
RoomBookingResponse response = processBookingLogic(bookingRequest);
// Send the confirmation response to the client's callback URL
roomService.sendResponseToClient(response, bookingRequest.getClientCallbackUrl());
}
private RoomBookingResponse processBookingLogic(RoomBookingRequest bookingRequest) {
// Implement your booking logic here, e.g., checking room availability and generating a reservation number
// Update room availability in the database
// Send a response message to confirm the booking or indicate unavailability
// For simplicity, we'll assume the room is available and generate a reservation number.
String reservationNumber = generateReservationNumber();
return new RoomBookingResponse(reservationNumber);
}
private String generateReservationNumber() {
// Generate a unique reservation number (you can implement your logic here).
return "RES-" + System.currentTimeMillis();
}
}
方法二:實現(xiàn)樂觀鎖來處理并發(fā)請求
您可以修改代碼以使用同步方法和 JPA 樂觀鎖定。
步驟1:修改Room實體:@Version向實體添加一個字段Room以啟用樂觀鎖定:
import javax.persistence.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@Entity
public class Room {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String roomType;
private boolean isAvailable;
@Version
private Long version;
// getters and setters
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@Service
public class RoomService {
@Autowired
private RoomRepository roomRepository;
private final ConcurrentHashMap<Long, Lock> roomLocks = new ConcurrentHashMap<>();
public RoomBookingResponse bookRoom(RoomBookingRequest bookingRequest) {
String roomType = bookingRequest.getRoomType();
LocalDate checkInDate = bookingRequest.getCheckInDate();
LocalDate checkOutDate = bookingRequest.getCheckOutDate();
Room room = roomRepository.findByRoomType(roomType);
if (room != null) {
Lock roomLock = roomLocks.computeIfAbsent(room.getId(), id -> new ReentrantLock());
roomLock.lock();
try {
if (room.isAvailable()) {
// Add validation to check availability based on check-in and check-out dates here.
// For simplicity, we'll assume the room is available.
room.setAvailable(false);
roomRepository.save(room);
// Generate a reservation number (you can implement your logic here).
String reservationNumber = generateReservationNumber();
return new RoomBookingResponse(reservationNumber);
}
} finally {
roomLock.unlock();
}
}
throw new RoomNotAvailableException();
}
private String generateReservationNumber() {
// Generate a unique reservation number (you can implement your logic here).
return UUID.randomUUID().toString();
}
}
詳細工作原理:
并發(fā)請求&ConcurrentHashMap:當同一房間收到多個并發(fā)預訂請求時,它們可能同時到達并可能導致競爭條件。的引入ConcurrentHashMap確保每個房間都有自己的鎖。這ConcurrentHashMap是一個線程安全的映射,可以由多個線程同時安全地訪問。
通過鎖定并發(fā)更新房間可用性:如果兩個線程同時嘗試預訂同一個房間,則只有其中一個線程會使用 成功獲取鎖roomLock.lock(),而另一個線程將暫時阻塞,直到第一個線程釋放鎖。
釋放鎖以供其他線程更新:一旦線程獲取了鎖并成功修改了房間的可用性,它就會使用 釋放鎖roomLock.unlock(),從而允許其他線程繼續(xù)預訂其他房間。
樂觀鎖防止數(shù)據(jù)庫級別的競爭條件:在代碼中,實體中的字段啟用數(shù)據(jù)庫級別的樂觀鎖。更新房間時,JPA 在允許更新之前會根據(jù)實體中的版本字段檢查數(shù)據(jù)庫中的版本字段。@VersionRoom
- 如果兩個事務同時嘗試更新同一個房間,根據(jù)版本號的比較,只有其中一個會成功,從而防止數(shù)據(jù)庫級別的數(shù)據(jù)沖突。
- 因此 2 個不同的事務無法同時更新數(shù)據(jù)庫中的一個房間