1、maven依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.2.7.RELEASE</version> <exclusions> <exclusion> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>1.4.2</version> </dependency> <dependency> <groupId>com.esotericsoftware</groupId> <artifactId>kryo</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>de.javakaffee</groupId> <artifactId>kryo-serializers</artifactId> <version>0.42</version> </dependency> |
2、添加配置
spring.main.allow-bean-definition-overriding= true spring.application.name=order-service seata.enabled=true seata.application-id=stock-seata-example seata.tx-service-group=my_test_tx_group seata.client.rm-report-success-enable=true seata.client.rm-table-meta-check-enable=false seata.client.rm-report-retry-count=5 seata.client.rm-async-commit-buffer-limit=10000 seata.client.rm.lock.lock-retry-internal=10 seata.client.rm.lock.lock-retry-times=30 seata.client.rm.lock.lock-retry-policy-branch-rollback-on-conflict=true seata.client.tm-commit-retry-count=3 seata.client.tm-rollback-retry-count=3 seata.client.undo.undo-data-validation=true seata.client.undo.undo-log-serialization=jackson seata.client.undo.undo-log-table=undo_log seata.log.exception-rate=100 seata.client.support.spring.datasource-autoproxy=true seata.service.vgroup-mapping.my_test_tx_group=default seata.service.enable-degrade=false seata.service.disable-global-transaction=false 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.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.registry.type=eureka seata.registry.eureka.service-url=eureka的地址 seata.registry.eureka.application=default seata.registry.eureka.weight=1 seata.client.undo.logSerialization=kryo |
3、修改数据源
package cn.com.dragonpass.infra.account.config; import com.alibaba.druid.pool.DruidDataSource; import io.seata.rm.datasource.DataSourceProxy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DataSourceProxyConfig { @Autowired private DataSourceProperties dataSourceProperties; // @Bean // @ConfigurationProperties(prefix = "spring.datasource") // public DataSource dataSource() { // return ; // } @Bean public DataSourceProxy dataSourceProxy() { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl(dataSourceProperties.getUrl()); druidDataSource.setUsername(dataSourceProperties.getUsername()); druidDataSource.setPassword(dataSourceProperties.getPassword()); druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); druidDataSource.setInitialSize(0); druidDataSource.setMaxActive(180); druidDataSource.setMaxWait(60000); druidDataSource.setMinIdle(0); druidDataSource.setValidationQuery("Select 1 from DUAL"); druidDataSource.setTestOnBorrow(false); druidDataSource.setTestOnReturn(false); druidDataSource.setTestWhileIdle(true); druidDataSource.setTimeBetweenEvictionRunsMillis(60000); druidDataSource.setMinEvictableIdleTimeMillis(25200000); druidDataSource.setRemoveAbandoned(true); druidDataSource.setRemoveAbandonedTimeout(1800); druidDataSource.setLogAbandoned(true); return new DataSourceProxy(druidDataSource); } } |
4、创建undo_log表
mysql:
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 AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8
5、使用
在方法上添加@GlobalTransactional即可开启事务