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

Posted 零

tags:

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

创建undo_log表,此表为seata框架使用:

CREATE TABLE `undo_log` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `branch_id` bigint(20) NOT NULL, `xid` varchar(100) NOT NULL,
 `context` varchar(128) NOT NULL, `rollback_info` longblob NOT NULL,
 `log_status` int(11) NOT NULL,
 `log_created` datetime NOT NULL,
 `log_modified` datetime NOT NULL,
 `ext` varchar(100) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2.启动TC(事务协调器)

下载seata服务器 下载地址: https://github.com/seata/seata/releases/download/v0.7.1/seata-server-0.7.1.zip 解压并启动 seata服务端解压路径: /bin/seata-server.bat

 如上图出现“Server started...”的字样则表示启动成功。

3.修改 registry.conf 配置文件(并添加到resources目录下)

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


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

4.修改nacos-config.txt文件(改为服务名)

 5.在seata/config目录下在运行

sh nacos-config.sh 127.0.0.1

打开nacos查看配置文件是否导入

6.创建项目

导入依赖

    <dependencies>
        <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>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <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>
    </dependencies>

order controller

@RestController
public class OrderController 

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

    @GetMapping("/save")
    @GlobalTransactional
    public String save()
        //订单
        this.orderService.save();

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

service

@Service
public class OrderService 
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save()
        this.jdbcTemplate.update("INSERT INTO `order`(`name`) VALUES ('张三')");
    

application

@SpringBootApplication
@EnableDiscoveryClient
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));
    

yml

  application:
    name: order
  cloud:
    alibaba:
      seata:
        tx-service-group: $spring.application.name

pay controller

@RestController
public class PayController 

    @Autowired
    private PayService orderService;

    @GetMapping("/save")
    public String save()
        //订单
        this.orderService.save();
        return "success";
    

service

@Service
public class PayService 
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save()
        this.jdbcTemplate.update("INSERT INTO `pay`(`name`) VALUES ('张三')");
    

在两个服务中注入数据源

@Configuration
public class DataSourceProxyConfig 

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() 
        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);
        return sqlSessionFactoryBean.getObject();
    

以上是关于SpringCloud Alibaba Seata处理分布式事务及示例Demo的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud Alibaba 使用Seata解决分布式事物

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

SpringCloud Alibaba Docker 安装 Seata Server集群

SpringCloud - Spring Cloud Alibaba 之 Seata分布式事务服务;TCC事务模式机制(二十三)

SpringCloud Alibaba Seata TCC 模式讲解与使用

SpringCloud - Spring Cloud Alibaba 之 Seata分布式事务服务;集成Nacos配置中心(十九)