基本概念
消息隊列三個場景:異步,削峰,解耦
異步:將整個流程進行異步發送,也就是說本來順序執行的程序化流程,異步后可以同時進行操作,互不影響,但保持最終結果一致性;
解耦:它允許不同的應用程序或服務通過發送和接收消息進行通信,而不是直接相互調用,通過消息中間件監控一方是否成功后其他系統依據需求直接訂閱即可
削峰:大量的請求放到隊列中,能消費多少依據服務器的處理能力,保證服務器穩定不掛機
消息中間件缺點:
1.系統復雜性:多個系統會考慮更多東西,重復消息,消息丟失,消息的順序
2.數據一致性:采用分布式事務,分為編程式事務和分布式事務
// 獲取事務管理器
PlatformTransactionManager txManager = new DataSourceTransactionManager(dataSource);
// 定義事務定義
DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
// 開啟事務
TransactionStatus txStatus = txManager.getTransaction(txDefinition);try {// 執行業務邏輯// ...// 提交事務txManager.commit(txStatus);
} catch (Exception e) {// 回滾事務txManager.rollback(txStatus);throw e;
}
@Service
@Transactional
public class OrderService {@PersistenceContextprivate EntityManager entityManager;@Transactional(rollbackFor = Exception.class)public void createOrder(Order order) {// 執行業務邏輯entityManager.persist(order);}
}
- 可用性
- 技術選型:金融公司目前使用的是RabbitMQ+Kafka的組合方式