分布式事务

岁酱吖の 提交于 2020-01-18 06:57:15
  1. TCC  基于RM本地事务来实现
    https://github.com/liuyangming/ByteTCC
    
    源码分析:
    1.1.1. Spring Cloud
    <dependency>
    	<groupId>org.bytesoft</groupId>
    	<artifactId>bytetcc-supports-springcloud</artifactId>
    	<version>0.5.7</version>
    </dependency>
    1.1.2. dubbo
    <dependency>
    	<groupId>org.bytesoft</groupId>
    	<artifactId>bytetcc-supports-dubbo</artifactId>
    	<version>0.5.7</version>
    </dependency>
    1.2 Compose a business service
    @Service("accountService")
    @Compensable(
      interfaceClass = IAccountService.class 
    , confirmableKey = "accountServiceConfirm"
    , cancellableKey = "accountServiceCancel"
    )
    public class AccountServiceImpl implements IAccountService {
    
    	@Resource(name = "jdbcTemplate")
    	private JdbcTemplate jdbcTemplate;
    
    	@Transactional
    	public void increaseAmount(String accountId, double amount) throws ServiceException {
    	    this.jdbcTemplate.update("update tb_account set frozen = frozen + ? where acct_id = ?", amount, acctId);
    	}
    
    }
    1.3 Compose a confirm service
    @Service("accountServiceConfirm")
    public class AccountServiceConfirm implements IAccountService {
    
    	@Resource(name = "jdbcTemplate")
    	private JdbcTemplate jdbcTemplate;
    
    	@Transactional
    	public void increaseAmount(String accountId, double amount) throws ServiceException {
    	    this.jdbcTemplate.update("update tb_account set amount = amount + ?, frozen = frozen - ? where acct_id = ?", amount, amount, acctId);
    	}
    
    }
    1.4 Compose a cancel service
    @Service("accountServiceCancel")
    public class AccountServiceCancel implements IAccountService {
    
    	@Resource(name = "jdbcTemplate")
    	private JdbcTemplate jdbcTemplate;
    
    	@Transactional
    	public void increaseAmount(String accountId, double amount) throws ServiceException {
    	    this.jdbcTemplate.update("update tb_account set frozen = frozen - ? where acct_id = ?", amount, acctId);
    	}
    
    }
  2. sega
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!