Golang 分布式事务
Contents
Golang 分布式事务
使用两阶段提交(2PC)协议实现分布式事务
1. 数据库设置
创建两个数据库
|
|
2. 实现2PC协议
-
coordinator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
type Coordinator struct{} // ExecuteDistributedTransaction runs the distributed transaction using 2PC func (c *Coordinator) ExecuteDistributedTransaction(ctx context.Context, prepareFun func(db *gorm.DB) error, participants []*Participant) error { // Phase 1: Prepare var preparedTransactions []*gorm.DB for _, p := range participants { tx := p.db.Begin() err := p.Prepare(ctx, tx, prepareFun) if err != nil { return err } preparedTransactions = append(preparedTransactions, tx) } // Phase 2: Commit for i, p := range participants { err := p.Commit(ctx, preparedTransactions[i]) if err != nil { // Rollback transactions if commit fails for _, tx := range preparedTransactions { _ = p.Rollback(ctx, tx) } return err } } return nil }
-
participant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
type Participant struct { db *gorm.DB } // Prepare prepares the transaction in the participant func (p *Participant) Prepare(ctx context.Context, tx *gorm.DB, prepareFun func(db *gorm.DB) error) error { return prepareFun(tx) } // Commit commits the transaction in the participant func (p *Participant) Commit(ctx context.Context, tx *gorm.DB) error { err := tx.Commit().Error if err != nil { return err } return nil } // Rollback rolls back the transaction in the participant func (p *Participant) Rollback(ctx context.Context, tx *gorm.DB) error { err := tx.Rollback().Error if err != nil { return err } return nil }
3. 测试
|
|