栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

seata的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

seata的使用

seata的启动

官网:seata官网
seata下载地址

修改文件

修改seata的配置文件conf/file.conf
1、修改事务组名称 2、修改数据源

service {
  #vgroup->rgroup
  vgroup_mapping.fsp_tx_group = "default" #1、修改事务组名称为:fsp_tx_group,和客户端自定义的名称对应
  #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"
}

## transaction log store
store {
  ## store mode: file、db
  mode = "db" #2、修改此处将事务信息存储到数据库中

  ## database store
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    driver-class-name = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://localhost:3306/seat-server" #修改数据库连接地址
    user = "root" #修改数据库用户名
    password = "123456" #修改数据库密码
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}

创建seat-server 数据库导入conf中的/conf/db_store.sql

配置seata的nacos conf/registry.conf
**修改注册中心即可registry 1、2、 **

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos" #1、改为nacos

  nacos {
    serverAddr = "localhost:8848" #2、改为nacos的连接地址
    namespace = ""
    cluster = "default"
  }
}

先启动nacos
再启动seata

springboot使用seata

导入依赖


            com.alibaba.cloud
            spring-cloud-starter-alibaba-seata
            
                
                    seata-all
                    io.seata
                
            


            io.seata
            seata-all
            ${seata.version}

在springboot配置文件application.yml中配置

spring:
  cloud:
    alibaba:
      seata:
        tx-service-group: fsp_tx_group #自定义事务组名称需要与seata-server中的对应

在resources中增加file.conf

在resource中添加 registry.conf

启动类上

//剔除数据源自动装配
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableDiscoveryClient
@EnableFeignClients
public class SeataOrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SeataOrderServiceApplication.class, args);
    }
}

使用seata对数据源进行代理

@Configuration
public class DataSourceProxyConfig {
	//此处是mapper.xml的配置 自行百度查询
    @Value("${mybatis.mapperLocations}")
    private String mapperLocations;
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }
    @Bean
    public DataSourceProxy dataSourceProxy(DataSource dataSource) {
        return new DataSourceProxy(dataSource);
    }
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceProxy);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(mapperLocations));
        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }
}

@GlobalTransactional注解开启分布式事务 其他的事务记得开启事务 :启动类@EnableTransactionManagement 事务@Transactional

此处无需看 本人自己做的笔记

方法一这种方式,相对来说是比较复杂的,在seata-server-1.4.2以后的版本中,我们可以在registry.conf中配置配置中心时,就可以通过dataId来创建配置

registry.conf的nacos配置中心的dataId默认为seataServer.properties,我们在Nacos的控制台就可以直接创建一个这样的配置,其中的配置的内容就是config.txt的内容 修改事务组名称

seata:
  enabled: true
  application-id: ${spring.application.name}
  # 客户端和服务端在同一个事务组
  tx-service-group: fsp_tx_group
  # 自动数据源代理
  enable-auto-data-source-proxy: true
  # 数据源代理模式(分布式事务方案)
  data-source-proxy-mode: AT
  # 事务群组,配置项值为TC集群名,需要与服务端保持一致
  service:
    vgroup-mapping:
      fsp_tx_group: default
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/940031.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号