加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Seata排坑指南

(2021-07-30 12:12:36)
标签:

springcloud

nacos

seata

分类: 文档
Seata 版本和官方文档、实例代码匹配实在是太乱了,直到加入社区群后,才找到正确打开方式。建议入门必看以下资料:
springcloud整合demo: https://gitee.com/itCjb/spring-cloud-alibaba-seata-demo (上述demo内附1.3整合文字版)

1.参考官网版本说明下载最新版seata-server-1.4.2和所需组件。

2.参考官方脚本在mysql上创建seata数据库

3.修改file.conf,使用db存储方式
## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
  ## rsa decryption public key
  publicKey = ""
  
  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://121.37.225.23:33060/seata?rewriteBatchedStatements=true"
    user = "changein"
    password = "Changein=2020"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}

4.修改registry.conf ,使seata注册到nacos
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
 
  nacos {
    application = "seata-server"
    serverAddr = "http://nacos.changein.cn"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "changein2021"
  }
}
 
config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"
 
  nacos {
    serverAddr = "http://nacos.changein.cn"
    namespace = ""
    group = "SEATA_GROUP"
    username = "nacos"
    password = "changein2021"
    dataId = "seataServer.properties"
  }
}

5.需要到github上下载、nacos-config.sh

修改config.txt使用db存储
service.vgroupMapping.my_test_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://121.37.225.23:33060/seata?useUnicode=true
store.db.user=changein
store.db.password=Changein=2020
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table

执行nacos-config.sh,将seata配置上传到nacos。

注意:

1.service.vgroupMapping.my_test_tx_group=default 这个为默认是事务组。后面springcloud配置文件会用到,需要一一对应。default为seata-server注册到nacos上seata-server的集群,可以在nacos上服务列表里的seata-server的详情页面看到。

2.以上都没指定nacos的名称空间,默认服务都会在public下。

6.启动seata-server,这时可以在nacos配置列表、服务列表上看到seata的配置和服务。

7.springcloud引入seata,并且我们需要单独引入1.4.2版本
<!--   seata    -->
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
    <exclusions>
        <exclusion>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <scope>runtime</scope>
    <exclusions>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>

8.增加seata配置
server:
  port: 8052
spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: cloud-sample
  profiles:
    active: dev
  cloud:
    nacos:
      server-addr: http://nacos.changein.cn
      username: "nacos"
      password: "changein2021"
      config:
        enabled: true
        file-extension: yaml
        refresh-enabled: true
        namespace: ${spring.profiles.active}
        shared-configs:
          - data-id: common.yaml
            refresh: true
          - data-id: redis.yaml
            refresh: true
      discovery:
        enabled: true
        namespace: ${spring.profiles.active}
        service: cloud-sample
 
seata:
  enabled: true
  application-id: mall-customer
  tx-service-group: my_test_tx_group
  enableAutoDataSourceProxy: false
  config:
    type: nacos
    nacos:
      namespace:
      serverAddr: "http://nacos.changein.cn"
      group: SEATA_GROUP
      username: "nacos"
      password: "changein2021"
  registry:
    type: nacos
    nacos:
      application: seata-server
      serverAddr: "http://nacos.changein.cn"
      group: SEATA_GROUP
      namespace:
      username: "nacos"
      password: "changein2021"

注意,启动应用时,如果报错nacos login失败,会发现其实读取的spring.cloud.nacos下的username、password

seata.tx-service-group=my_test_tx_group 这里和前面说的config.txt里是事务组必须一一对应。

9.在服务对应的数据库用户下按官方脚本建undo_log表

10.增加数据源代理
package cn.changein.sample.boot.config;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Autowired(required = true)
    private DataSourceProperties dataSourceProperties;

    @Bean(name = "dataSource") // 声明其为Bean实例
    @Primary // 在同样的DataSource中,首先使用被标注的DataSource
    public DataSource druidDataSource() {
        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 2 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);
        DataSourceProxy dsp = new DataSourceProxy(druidDataSource);
//        DataSourceProxy dsp = new DataSourceProxyXA(druidDataSource)
        return dsp;
    }
}

11.在需要全局事务的地方使用@GlobalTransactional就可以了。

seata1.4.2已经默认开启了数据源代理,AT模式不需要再写一个代理类了。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有