iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot整合seata的配置过程
  • 799
分享到

springboot整合seata的配置过程

2024-04-02 19:04:59 799人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

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

前言:

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

1.整合配置

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;

在这里插入图片描述

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 服务器

在这里插入图片描述

4.下载对应的服务器软件包

下载地址:seata下载地址,小编下载是seata-server-0.7.1,下载完成之后解压文件

5.修改配置文件

进入 conf文件夹,修改reGIStry.conf

在这里插入图片描述

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

在这里插入图片描述

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

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”

在这里插入图片描述

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

在这里插入图片描述

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

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

在这里插入图片描述
在这里插入图片描述

10.具体业务:

在 online-project服务的ProjectController中


 
    @PostMapping("/updateProjectById")
    public R updateProjectById(@RequestBody Project project){
        projectService.updateProjectById(project);
        return R.ok();
    }

在 CouponServiceImpl中



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

在online-coupon服务CouponController中


 
    @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中



    @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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springboot整合seata的配置过程

本文链接: https://www.lsjlt.com/news/133412.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作