五分钟带你了解 Seata 分布式事务,java 基础菜鸟教程 txt
如果全局事务的任何一个事务分支失败了,那么全局事务就进入“回滚“流程,回滚时依据先前保存好数据镜像,将原来的数据回放回去。
如果全局回放成功,那么数据的一致性也就得到了保证,如果回放不成功,那么事务就进入异常。应对异常,可能需要重试,可能需要人工介入。
2.Seata 在 dubbo 中的使用
2.1.新增 mvn 依赖
在需要增加分布式事务的模块加上相关依赖
<dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>1.1.0</version></dependency>
2.2.修改 properties 文件
#服务名称dubbo.application.name=service#注册中心地址dubbo.registry.address=127.0.0.1:2181#注册中心类型dubbo.registry.protocol=zookeeper#版本号dubbo.application.version=3# Dubbo Protocol#协议名称`dubbo.pro
tocol.name=dubbo`
#服务暴露端口dubbo.protocol.port=20880seata.enabled=trueseata.application-id=biz-serviceseata.tx-service-group=my_test_tx_groupseata.client.rm.async-commit-buffer-limit=1000seata.client.rm.report-retry-count=5seata.client.rm.table-meta-check-enable=falseseata.client.rm.report-success-enable=falseseata.client.rm.lock.retry-interval=10seata.client.rm.lock.retry-times=30seata.client.rm.lock.retry-policy-branch-rollback-on-conflict=trueseata.client.tm.commit-retry-count=5seata.client.tm.rollback-retry-count=5seata.client.undo.data-validation=trueseata.client.undo.log-serialization=jacksonseata.client.undo.log-table=undo_logseata.client.log.exceptionRate=100seata.service.vgroup-mapping.my_test_tx_group=defaultseata.service.grouplist.default=127.0.0.1:8091seata.transport.shutdown.wait=3seata.transport.thread-factory.boss-thread-prefix=NettyBossseata.transport.thread-factory.worker-thread-prefix=NettyServerNIOWorkerseata.transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandlerseata.transport.thread-factory.share-boss-worker=falseseata.transport.thread-factory.client-selector-thread-prefix=NettyClientSelectorseata.transport.thread-factory.client-selector-thread-size=1seata.transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThreadseata.transport.thread-factory.worker-thread-size=defaultseata.transport.thread-factory.boss-thread-size=1seata.transport.type=TCPseata.transport.server=NIOseata.transport.heartbeat=trueseata.transport.serialization=seataseata.transport.compressor=noneseata.transport.enable-client-batch-send-request=trueseata.config.type=fileseata.registry.type=file
2.3.新增配置类
package cn.enjoy.mt.order.config;import com.alibaba.druid.pool.DruidDataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configurationpublic class SeataConfiguration {@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSource druidDataSource() {DruidDataSource druidDataSource = new DruidDataSource();return druidDataSource;}@Beanpublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();factoryBean.setDataSource(dataSource);factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapping/.xml"));return factoryBean.getObject();}}
2.4.新增 undo_log 日志表
CREATE TABLE undo_log (id bigint(20) NOT NULL AUTO_INCREMENT,branch_id bigint(20) NOT NULL,xid varchar(100) NOT NULL,context varchar(128) NOT NULL,rollback_info longblob NOT NULL,log_status int(11) NOT NULL,log_created datetime NOT NULL,log_modified datetime NOT NULL,ext varchar(100) DEFAULT NULL,PRIMARY KEY (id),UNIQUE KEY ux_undo_log (xid,branch_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.5.SeataProperties
package cn.enjoy.mt.order.config;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties("spring.cloud.alibaba.seata")public class SeataProperties {











评论