seata分布式事务原理_分布式事务seata集成

1、下载安装seata服务

1.1、下载 地址:https://github.com/seata/seata/releases

b8f2771c6d70319f78693bcea30e6a8f.png

1.2、解压后修改 seataconf 目录下的 register.conf ,file.conf,数据库地址、zookeeper地址

配置 register.conf,我的注册中心是 zk,去掉多余的配置,只需要修改zk的地址即可;

dba6b68b3cb69b1ccbf36e8c2f3b04ca.png

配置file.conf,只需要修改数据库部分

552829c4a9ad92c98fe8605582ead4aa.png

1.3、在对应数据库添加 seata所需要的事务信息表

执行 seataconf 目录下的 db_store.sql 和 db_undo_log.sql 把表添加到数据库里。

1.4、启动seata服务

在这之前首先保证seata所在服务器能正常连接数据库,和zookeeper;

根据系统环境执行seatabin 目录下的 seata-server.bat 或者seata-server.sh 启动后默认访问端口为8091

2、项目集成seata

1、加依赖

 <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <version>0.9.0</version>
        </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.7.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.7.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.7.4</version>
            </dependency>

2、把seata配置文件(registry.conf,file.conf在git案例中有,可直接拷贝过来) 放到项目资源目录

registry.conf

registry {
  # file nacos
  type = "file"
​
  file {
    name = "file.conf"
  }
}
​
config {
  # file、nacos 、apollo、zk、consul
  type = "file"
​
  file {
    name = "file.conf"
  }
}

file.conf 只需修改seata服务提供地址,就是我们上面下载安装的seata,

transport {
  # tcp udt unix-domain-socket
  type = "TCP"
  #NIO NATIVE
  server = "NIO"
  #enable heartbeat
  heartbeat = true
  #thread factory for netty
  thread-factory {
    boss-thread-prefix = "NettyBoss"
    worker-thread-prefix = "NettyServerNIOWorker"
    server-executor-thread-prefix = "NettyServerBizHandler"
    share-boss-worker = false
    client-selector-thread-prefix = "NettyClientSelector"
    client-selector-thread-size = 1
    client-worker-thread-prefix = "NettyClientWorkerThread"
    # netty boss thread size,will not be used for UDT
    boss-thread-size = 1
    #auto default pin or 8
    worker-thread-size = 8
  }
  shutdown {
    # when destroy server, wait seconds
    wait = 3
  }
  serialization = "seata"
  compressor = "none"
}
​
service {
  #vgroup->rgroup
  vgroup_mapping.my_test_tx_group = "default"
  #only support single node
  default.grouplist = "127.0.0.1:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
  #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
}
​
client {
  async.commit.buffer.limit = 10000
  lock {
    retry.internal = 10
    retry.times = 30
  }
  report.retry.count = 5
  tm.commit.retry.count = 1
  tm.rollback.retry.count = 1
}
​
transaction {
  undo.data.validation = true
  undo.log.serialization = "jackson"
  undo.log.save.days = 7
  #schedule delete expired undo_log in milliseconds
  undo.log.delete.period = 86400000
  undo.log.table = "undo_log"
}
​
support {
  ## spring
  spring {
    # auto proxy the DataSource bean
    datasource.autoproxy = false
  }
}

3、配置maven扫描 .conf尾缀的文件

这里需要配置下,刚开始我没配置一直找不到配置文件,导致regist type 为null的错误。

 <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                    <include>**/*.conf</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

4、添加seata spring配置 DBConfig 可直接使用seata-samples 案例中的。

@Configuration
public class DBConfig {
​
    @Value("${spring.application.name}")
    private String applicationId;
​
    @Bean(initMethod = "init")
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }
​
    @Primary
    @Bean
    @ConditionalOnMissingBean
    public DataSourceProxy dataSource(DataSource dataSource)
    {
        return new DataSourceProxy(dataSource);
    }
​
​
    /**
     * 初始化分布式全局事务扫描
     *
     * @return
     */
    @Bean
    public GlobalTransactionScanner globalTransactionScanner() {
        return new GlobalTransactionScanner(applicationId.toLowerCase(),  "my_test_tx_group");
    }
​
}

5、在事务的入口方法添加 seata注解,被调用方不需要加注解

服务A代码

@Service
public class TestSeataServiceImpl  implements TestSeataService{
​
    @Autowired
    private TestSeataMapper testSeataMapper;
    @Reference
    private BaseSeataService baseSeataService;
​
    @Override
    @GlobalTransactional(timeoutMills = 300000, name = "my_test_tx_group")
    public Json testSeataTransAction(Integer status) {
        TestSeata testSeata=new TestSeata();
        testSeata.setName("张3");
        testSeataMapper.insert(testSeata);
        baseSeataService.insertSeata("李4");
        return Json.CODE_200;
    }
}

服务B代码

@Service
public class BaseSeataServiceImpl  implements BaseSeataService{
​
    @Autowired
    private TestSeataMapper testSeataMapper;
​
    @Override
    public void insertSeata(String name) {
        TestSeata testSeata=new TestSeata();
        testSeata.setName(name);
        testSeataMapper.insert(testSeata);
        throw  new RuntimeException();
    }
​
}

测试:调用服务A 发现服务A和服务B都对事务进行了回滚

ac54f1c7fe56f5941311b57673b03a7a.png

2d200fd8c949f746c6541b8532578a5e.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值