seata 分布式事务 -- seata-two工程完整代码

Posted Li&Fan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了seata 分布式事务 -- seata-two工程完整代码相关的知识,希望对你有一定的参考价值。

 

工程结构 ,启动类 ,数据库主键生成工具类(雪花算法) 跟 seata-one 一致

 

入口  controller:

 

package com..controller;

import com..service.Rm_Two_Interface;
import com..service.Rm_Two_Service;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Rm_Two_Controller {


    @Autowired
    private Rm_Two_Service rm_two_service;

    @Autowired
    private Rm_Two_Interface rm_two_interface;


    @RequestMapping("/two_at")
    public String rm2(){

        String result = rm_two_service.rm2();
//        System.out.println(1/0);
        if(result.equals("err")){
            return "err";
        }
        return "success_"+result;

    }


    @RequestMapping("/two_tcc")
    @GlobalTransactional(rollbackFor = Exception.class)
    public String oneTcc() throws InterruptedException {
        rm_two_interface.rm2(null);
        return "success";
    }





}

 

 

 

数据库实体类:

 

package com..entity;


public class TbltwoInfo {
   
    private String id;

   
    private String name;

    
    public String getId() {
        return id;
    }

   
    public void setId(String id) {
        this.id = id;
    }

   
    public String getName() {
        return name;
    }

   
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
}

 

 

mapper:

package com..mapper;

import com..entity.TbltwoInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component(value = "TbltwoInfoMapper")
public interface TbltwoInfoMapper {
    
    int deleteByPrimaryKey(String id);

    int insert(TbltwoInfo record);

    TbltwoInfo selectByPrimaryKey(String id);

    List<TbltwoInfo> selectAll();

    int updateByPrimaryKey(TbltwoInfo record);
}

 

AT 模式 实现类:

package com..service;

import com..entity.TbltwoInfo;
import com..mapper.TbltwoInfoMapper;
import com..sqlToJava.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class Rm_Two_Service {

    @Autowired
    TbltwoInfoMapper tbltwoInfoMapper;

    public String rm2(){

        long id = SnowFlake.nextId();
        TbltwoInfo tbltwoInfo = new TbltwoInfo();
        tbltwoInfo.setId(id+"");
        tbltwoInfo.setName("Rm_Two_"+id);

        int insert = tbltwoInfoMapper.insert(tbltwoInfo);


        if (insert==1){
            return id+"";
        }
        return "err";
    }
}

 

TCC模式 接口 / 实现类:

接口:

package
com.wondersgroup.service; import io.seata.rm.tcc.api.BusinessActionContext; import io.seata.rm.tcc.api.LocalTCC; import io.seata.rm.tcc.api.TwoPhaseBusinessAction; @LocalTCC public interface Rm_Two_Interface { @TwoPhaseBusinessAction(name = "rm2TccAction",commitMethod = "rm2Commit",rollbackMethod = "rm2Rollback") public String rm2(BusinessActionContext businessActionContext); public boolean rm2Commit(BusinessActionContext businessActionContext); public boolean rm2Rollback(BusinessActionContext businessActionContext); }

实现类:

package com..service;

import com..entity.TbltwoInfo;
import com..mapper.TbltwoInfoMapper;
import com..sqlToJava.SnowFlake;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class Rm_Two_InterfaceImpl implements Rm_Two_Interface{


@Autowired
TbltwoInfoMapper tbltwoInfoMapper;

private static ConcurrentMap<String,String> maps = new ConcurrentHashMap<>();

@Override
@Transactional
public String rm2(BusinessActionContext businessActionContext) {

long id = SnowFlake.nextId();
TbltwoInfo tbltwoInfo = new TbltwoInfo();
tbltwoInfo.setId(id+"");
tbltwoInfo.setName("Rm_Two_"+id);
maps.put("id",id+"");
int insert = tbltwoInfoMapper.insert(tbltwoInfo);

System.out.println("rm2 try...."+insert);

return null;
}

@Override
@Transactional
public boolean rm2Commit(BusinessActionContext businessActionContext) {

System.out.println("rm2 rm2Commit....");
return true;
}

@Override
@Transactional
public boolean rm2Rollback(BusinessActionContext businessActionContext) {

String id = maps.get("id");
int i = tbltwoInfoMapper.deleteByPrimaryKey(id);
System.out.println("rm2 rm2Rollback...."+i);
return true;
}
}
 

 

application.yml  :

server:
  port: 8070

#应用名称及验证账号
spring:
  application:
    name: seata-two


  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3307/seata-rm-two?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    dbcp2:
      initial-size: 5
      min-idle: 5
      max-total: 5
      max-wait-millis: 200
      validation-query: SELECT 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false

mybatis:
  mapper-locations:
    - classpath:mapper/*.xml

eureka:
  client:
    prefer-ip-address: true
    service-url:
      defaultZone: http://localhost:7900/eureka/

 

数据库链接 TbloneInfoMapper.xml:

(项目地址需要根据情况补充)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com..mapper.TbltwoInfoMapper">
  <resultMap id="BaseResultMap" type="com..entity.TbltwoInfo">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 17 11:20:53 CST 2021.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 17 11:20:53 CST 2021.
    -->
    delete from tbl_two
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com..entity.TbltwoInfo">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 17 11:20:53 CST 2021.
    -->
    insert into tbl_two (id, name)
    values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR})
  </insert>
  <update id="updateByPrimaryKey" parameterType="com..entity.TbltwoInfo">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 17 11:20:53 CST 2021.
    -->
    update tbl_two
    set name = #{name,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 17 11:20:53 CST 2021.
    -->
    select id, name
    from tbl_two
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <select id="selectAll" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 17 11:20:53 CST 2021.
    -->
    select id, name
    from tbl_two
  </select>
</mapper>

 

pom:

(<groupId>com.</groupId> 需要根据情况补充 )

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.</groupId>
    <artifactId>seata-two</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>seata-two</name>
    <description>seata-two</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- euekea 依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- mysql:MyBatis相关依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- 整合MyBatis java类依赖 -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.0</version>
            <type>maven-plugin</type>
        </dependency>

        <!-- mysql:mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- mysql:阿里巴巴数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>
        <!--  JSONObject  -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.60</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-seata</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
            <version>3.0.2</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

以上是关于seata 分布式事务 -- seata-two工程完整代码的主要内容,如果未能解决你的问题,请参考以下文章

Seata分布式事务

seata-分布式事务与seata

seata---分布式事务处理

Seata基础使用-分布式事务

Seata分布式事务Seata的使用及原理

Seata分布式事务Seata的使用及原理