是工作在应用层的中间件。主要优点是性能较好,且不长时间占用连接资源,它以高效并且对业务0侵入的方式解决微服 务场景下面临的分布式事务问题,它目前提供AT模式(即2PC)及TCC模式的分布式事务解决方案。
seata 采用的是大量运用在数据库软件的 Write Ahead Log 思想,即把事务的信息以事务日志的方式记录下来。这种处理方式,实际上是对传统两阶段提交的一种改进和优化。主要有几个关键点:
在进行本地提交的前提是,seata 会解析 SQL,获取数据库表的元数据,根绝 SQL 类型,选择性地生成数据的前置镜像和后置镜像, 保存在 undolog 表中,并且要求与保存 undolog 与业务 SQL 在同一个本地事务内。
因为每个分支事务的本地事务都已经被提交,所以如果全局事务能够顺利进行到“提交“这一阶段,那么意味着所有事务分支的本地事 务都已经被提交了,数据的一致性已经得到了保证。
这个时候全局事务的提交就变得十分轻量级,就是把 undo_log 对应的记录删掉即可,即使是当时删除失败了,也已经不会影响全局事 务的最终结果,这次删不了,那就待会再删,程序删不了,没事,顶多人工删。
-
#服务名称
-
dubbo.application.name=service
-
#注册中心地址
-
dubbo.registry.address=127.0.0.1:2181
-
#注册中心类型
-
dubbo.registry.protocol=zookeeper
-
#版本号
-
dubbo.application.version=3
-
# Dubbo Protocol
-
#协议名称
-
dubbo.protocol.name=dubbo
-
#服务暴露端口
-
dubbo.protocol.port=20880
-
-
-
-
-
seata.enabled=true
-
seata.application-id=biz-service
-
seata.tx-service-group=my_test_tx_group
-
seata.client.rm.async-commit-buffer-limit=1000
-
seata.client.rm.report-retry-count=5
-
seata.client.rm.table-meta-check-enable=false
-
seata.client.rm.report-success-enable=false
-
seata.client.rm.lock.retry-interval=10
-
seata.client.rm.lock.retry-times=30
-
seata.client.rm.lock.retry-policy-branch-rollback-on-conflict=true
-
seata.client.tm.commit-retry-count=5
-
seata.client.tm.rollback-retry-count=5
-
seata.client.undo.data-validation=true
-
seata.client.undo.log-serialization=jackson
-
seata.client.undo.log-table=undo_log
-
seata.client.log.exceptionRate=100
-
seata.service.vgroup-mapping.my_test_tx_group=default
-
seata.service.grouplist.default=127.0.0.1:8091
-
seata.transport.shutdown.wait=3
-
seata.transport.thread-factory.boss-thread-prefix=NettyBoss
-
seata.transport.thread-factory.worker-thread-prefix=NettyServerNIOWorker
-
seata.transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandler
-
seata.transport.thread-factory.share-boss-worker=false
-
seata.transport.thread-factory.client-selector-thread-prefix=NettyClientSelector
-
seata.transport.thread-factory.client-selector-thread-size=1
-
seata.transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThread
-
seata.transport.thread-factory.worker-thread-size=default
-
seata.transport.thread-factory.boss-thread-size=1
-
seata.transport.type=TCP
-
seata.transport.server=NIO
-
seata.transport.heartbeat=true
-
seata.transport.serialization=seata
-
seata.transport.compressor=none
-
seata.transport.enable-client-batch-send-request=true
-
seata.config.type=file
-
seata.registry.type=file
-
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;
-
@Configuration
-
public class SeataConfiguration {
-
@Bean
-
-
@ConfigurationProperties(prefix = "spring.datasource")
-
-
public DataSource druidDataSource() {
-
-
DruidDataSource druidDataSource = new DruidDataSource();
-
-
return druidDataSource;
-
-
}
-
-
@Bean
-
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
-
-
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
-
-
factoryBean.setDataSource(dataSource);
-
-
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
-
-
.getResources("classpath:/mapping/.xml"));
-
-
return factoryBean.getObject();
-
-
}
-
}
-
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;
-
package cn.enjoy.mt.order;
-
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
-
import org.mybatis.spring.annotation.MapperScan;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
-
import org.springframework.cache.annotation.EnableCaching;
-
import org.springframework.context.annotation.PropertySource;
-
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
-
@EnableDubbo
-
@PropertySource("classpath:mt_db.properties")
-
@MapperScan("cn.enjoy.mt.dao")
-
@EnableCaching
-
public class OrderServiceApp {
-
public static void main(String[] args) {
-
SpringApplication.run(OrderServiceApp.class,args);
-
}
-
}