服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot 整合 seata的配置过程

springboot 整合 seata的配置过程

2021-12-01 13:29秋叶清风 Java教程

本文给大家介绍springboot 整合 seata的配置过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

前言:

小编引入的图片和文字描述都是来自于尚硅谷的视频讲解,在此感谢尚硅谷的老师,同时也结合 seata文档官方文档进行整合
项目地址(gitee): https://gitee.com/qinenqi/online
springboot整合 seata

整合配置

online-project 这个服务调用 online-coupon这个服务

在 这两个被整合的服务对用的数据库中分别 创建 UNDO_LOG 表

-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
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;

springboot 整合 seata的配置过程

2. 引入依赖

<!-- seata   -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        </dependency>

小编这儿已经引入了 阿里的相关组件,请根据自己的实际情况进行处理

 <!--        服务注册/发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--        配置中心来做配置管理-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

引入依赖后,查看自己的 seata-all-0.7.1,要根据这个版本下载相应的seata 服务器

springboot 整合 seata的配置过程

4.下载对应的服务器软件包,下载地址:seata下载地址,小编下载是seata-server-0.7.1,下载完成之后解压文件

5.修改配置文件,进入 conf文件夹,修改registry.conf

springboot 整合 seata的配置过程

在注册中, 小编配置的是nacos, 把type = “file” 改成 type = “nacos”,

springboot 整合 seata的配置过程

在配置信息中,小编用的是默认的文件方式

6.在online-coupon、online-project 新建 MySeataConfig

import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;

@Configuration
public class MySeataConfig {

    @Autowired
    DataSourceProperties dataSourceProperties;

    @Bean
    public DataSource dataSource(DataSourceProperties dataSourceProperties){
        HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        if (StringUtils.hasText(dataSourceProperties.getName())) {
            dataSource.setPoolName(dataSourceProperties.getName());
        }
        return new DataSourceProxy(dataSource);
    }
}

7.分别引入配置文件(file.conf、registry.conf)并修改 vgroup_mapping.my_test_tx_group = “default”
把这两个配置文件从conf文件夹下复制到项目的resources目录下,分别修改file.conf,把vgroup_mapping.my_test_tx_group = "default"分别修改成vgroup_mapping.online-coupon-fescar-service-group = "default"和 vgroup_mapping.online-project-fescar-service-group = “default”

springboot 整合 seata的配置过程

8.启动nacos 和 seata 服务(startup.cmd、seata-server.bat)

springboot 整合 seata的配置过程

服务启动以后,访问 http://127.0.0.1:8848/nacos/, 可以看到 seata的服务

9.给分布式大事务的入口标注@GlobalTransactional、每一个远程的小事务用 @Transactional

springboot 整合 seata的配置过程
springboot 整合 seata的配置过程

10.具体业务:

在 online-project服务的ProjectController中

 /**
     *  根据 id 更新数据
     * @param project
     * @return
     */
    @PostMapping("/updateProjectById")
    public R updateProjectById(@RequestBody Project project){
        projectService.updateProjectById(project);
        return R.ok();
    }

在 CouponServiceImpl中

/**
     *  从 商品哪儿调用  用来测试 seata
     */
    @Transactional
    public void  testSeata(){
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setId(4L);
        couponEntity.setCouponName("从 商品哪儿调用  用来测试 seata02");
        couponMapper.updateById(couponEntity);
//        int number = 2/0;
    }

在online-coupon服务CouponController中

 /**
     *  从 商品哪儿调用  用来测试 seata
     * @return
     */
    @RequestMapping("/testSeata")
    public R testSeata(){
        couponService.testSeata();
        return R.ok();
    }

新建 CouponFeignService

import com.example.onlinecommon.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("online-coupon")
public interface CouponFeignService {

    @RequestMapping("/coupon/couponController/testSeata")
    R testSeata();

}

在 CouponServiceImpl中

/**
     *  从 商品哪儿调用  用来测试 seata
     */
    @Transactional
    public void  testSeata(){
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setId(4L);
        couponEntity.setCouponName("从 商品哪儿调用  用来测试 seata02");
        couponMapper.updateById(couponEntity);
//        int number = 2/0;
    }

两个服务之间的调用使用的 openforeign,经过小编的测试,两个微服务实现了分布式事务的一致性

到此这篇关于springboot 整合 seata的文章就介绍到这了,更多相关springboot 整合 seata内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_41695138/article/details/119831809

延伸 · 阅读

精彩推荐