当前位置:   article > 正文

java 分布式事务seata的使用_seata java

seata java

首先创建一个seata的springboot模块并引入seata的起步依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4

模块的目录结构如下

在这里插入图片描述

seata.yaml中的内容为:

seata:
  tx-service-group: seata-toutiao
  service:
    vgroup-mapping: # 事务组与cluster的映射关系
      seata-toutiao: DEFAULT
    grouplist:
      DEFAULT: 192.168.211.136:8091
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

spring.factories中的内容为:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jjw.core.seata.SeataHeimaAutoConfiguration
org.springframework.boot.env.EnvironmentPostProcessor=\
com.jjw.core.seata.MyEnvironmentPostProcessor
  • 1
  • 2
  • 3
  • 4

MyEnvironmentPostProcessor中的内容为:

package com.jjw.core.seata;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.List;

/**
 * 自定义环境处理,在运行SpringApplication之前加载任意配置文件到Environment环境中
 */

public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    //Properties对象
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        //自定义配置文件
        String[] profiles = {
                "seata.yaml"
        };
        //循环添加
        for (String profile : profiles) {
            //从classpath路径下面查找文件
            Resource resource = new ClassPathResource(profile);
            //加载成PropertySource对象,并添加到Environment环境中
            environment.getPropertySources().addFirst(loadProfiles(resource));
        }
    }

    //加载单个配置文件
    private PropertySource<?> loadProfiles(Resource resource) {
        if (!resource.exists()) {
            throw new IllegalArgumentException("资源" + resource + "不存在");
        }
        try {
            YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
            List<PropertySource<?>> resources = yamlPropertySourceLoader.load(resource.getFilename(), resource);
            return resources.get(0);
        } catch (IOException ex) {
            throw new IllegalStateException("加载配置文件失败" + resource, ex);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

SeataHeimaAutoConfiguration中的内容为:

package com.jjw.core.seata;

import io.seata.rm.datasource.DataSourceProxy;
import io.seata.spring.boot.autoconfigure.properties.SeataProperties;
import io.seata.spring.boot.autoconfigure.properties.client.ServiceProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * --加载文件properties
 * https://www.cnblogs.com/huanzi-qch/p/11122107.html
 * --加载yaml
 * https://blog.csdn.net/baidu_28523317/article/details/108701391
 *
 * --顺序
 * https://blog.csdn.net/f641385712/article/details/105596178
 *
 * --可能存在的问题
 * https://segmentfault.com/q/1010000040364236
 */
@Configuration
@ConditionalOnClass(DataSourceProxy.class)
public class SeataHeimaAutoConfiguration {

    //仅仅用作测试
    @Bean
    public Map<String,String> seatax(SeataProperties seataProperties,
                                     ServiceProperties serviceProperties){
        String txServiceGroup = seataProperties.getTxServiceGroup();
        System.out.println(txServiceGroup);
        System.out.println(serviceProperties.getGrouplist());
        return new HashMap<>();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

在需要用到分布式事务的微服务中添加依赖(用到几个微服务就给相应的微服务都添加分布式事务依赖)、且对这几个微服务所在的库都添加一个undolog表:

-- auto-generated definition
create table undo_log
(
    id            bigint auto_increment
        primary key,
    branch_id     bigint       not null,
    xid           varchar(100) not null,
    context       varchar(128) not null,
    rollback_info longblob     not null,
    log_status    int          not null,
    log_created   datetime     not null,
    log_modified  datetime     not null,
    ext           varchar(100) null,
    constraint ux_undo_log
        unique (xid, branch_id)
)
    charset = utf8;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在最开始使用分布式事务的那个微服务上的方法上添加事务注解

@GlobalTransactional // 全局事务
@Transactional // 本地事务
@Override
public void pass(Integer id) {
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/1018673
推荐阅读
相关标签
  

闽ICP备14008679号