seata分布式事务配置示例

Posted 零

tags:

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

确定版本对应
| SpringBoot | 2.2.5.RELEASE |
| SpringCloud | Hoxton.SR3 |
| SpringCloudAlibaba | 2.2.1.RELEASE |

1.新建两个工程order/pay

修改pom

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

2.两个数据库 order、pay

3.分别编写两个服务的 application.yml

server:
  port: 8010
spring:
  application:
    name: order
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/order
server:
  port: 8020
spring:
  application:
    name: pay
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/pay

4.分别编写service

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class OrderService 
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save()
        this.jdbcTemplate.update("insert into orders(username) values ('张三')");
    

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class PayService 
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save()
        this.jdbcTemplate.update("insert into pay(username) values ('张三')");
    

5.控制器 Order 通过 RestTemplate 调用 Pay 的服务

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController 

    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/save")
    public String save()
        //订单
        this.orderService.save();
        int i = 10/0;
        //支付
        this.restTemplate.getForObject("http://localhost:8020/save",String.class);
        return "success";
    

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayController 
    @Autowired
    private PayService payService;

    @GetMapping("/save")
    public String save()
        this.payService.save();
        return "success";
    

6.启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class OrderApplication 

    public static void main(String[] args) 
        SpringApplication.run(OrderApplication.class, args);
    

    @Bean
    public RestTemplate restTemplate()
        return new RestTemplate();
    

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PayApplication 

    public static void main(String[] args) 
        SpringApplication.run(PayApplication.class, args);
    


2.7.5 seata解决方案

1.解压配置

分别修改nacos-config.txt和registry.conf

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IDiW8MXS-1649763177578)(SpringCloudAlibaba.assets/image-20210824070251054.png)]

  • regisry.conf

    注意serverAddr地址如果是linux中,不在本机,需要写uri

    registry 
      type = "nacos"
      nacos 
        serverAddr = "localhost"
        namespace = "public"
        cluster = "default"
      
    
    
    config 
      type = "nacos"
      nacos 
        serverAddr = "localhost"
        namespace = "public"
        cluster = "default"
      
    
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hEMrmFlt-1649763177579)(SpringCloudAlibaba.assets/image-20210824111223864.png)]

  • nacos-config.txt

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D3OshqMN-1649763177580)(SpringCloudAlibaba.assets/image-20210824070627941.png)]

2.启动Nacos

运行 nacos-config.sh 将 Seata 配置导入 Nacos

注意:windows中不能直接运行sh文件,可用git启动

# 进入conf目录,git hash中执行:
sh nacos-config.sh 127.0.0.1

3.启动 Seata Server ( JDK 8 以上环境无法启动)

# 进入bin目录 
seata-server.bat -p 8090 -m file

4.初始化数据库

两个数据库中执行此sql

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9uFeQM2W-1649763177580)(SpringCloudAlibaba.assets/image-20210824071336319.png)]

5.两个工程的pom中添加seata依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

6.给 JDBCTemplate 添加代理数据源

这里用的是JDBC,如果使用的是MyBatis 或 MP处理不一样

import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.client.RestTemplate;

import javax.sql.DataSource;

@SpringBootApplication
public class OrderApplication 

    public static void main(String[] args) 
        SpringApplication.run(OrderApplication.class, args);
    

    @Bean
    public RestTemplate restTemplate()
        return new RestTemplate();
    

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource)
        return new JdbcTemplate(new DataSourceProxy(dataSource));
    

import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@SpringBootApplication
public class PayApplication 

    public static void main(String[] args) 
        SpringApplication.run(PayApplication.class, args);
    

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource)
        return new JdbcTemplate(new DataSourceProxy(dataSource));
    


7.将registry.conf 复制到两个工程的 resources 下

8.给两个工程添加 bootstrap.yml 读取 Nacos 配置

tx-service-group 需要和 Nacos 配置中的名称一致

spring:
  application:
    name: order
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        namespace: public
        group: SEATA_GROUP
    alibaba:
      seata:
        tx-service-group: $spring.application.name
spring:
  application:
    name: pay
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        namespace: public
        group: SEATA_GROUP
    alibaba:
      seata:
        tx-service-group: $spring.application.name

9.在 Order 调用 Pay 处添加注解 @GlobalTransactional

import com.southwind.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController 

    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/save")
    @GlobalTransactional
    public String save()
        //订单
        this.orderService.save();
        int i = 10/0;
        //支付
        this.restTemplate.getForObject("http://localhost:8020/save",String.class);
        return "success";
    

以上是关于seata分布式事务配置示例的主要内容,如果未能解决你的问题,请参考以下文章

Seata TCC 分布式事务

分布式事务:SpringBoot+Dubbo+Seata+Nacos 实现案例

SpringCloud Alibaba Seata处理分布式事务及示例Demo

分布式事务-seata

Spring Cloud Alibaba Seata 分布式事务使用快速入门,Nacos做Seata的注册中心和配置中心

Spring Cloud Alibaba Seata 分布式事务使用快速入门,Nacos做Seata的注册中心和配置中心