课堂笔记20230822

亮子 2023-08-22 06:39:31 10345 0 0 0

分布式事务使用的场景

  • 当跨域多个数据库时
  • 当跨域多个微服务时

分布式事务的实现方案

  • 2PC
  • 3PC

seata的实现方案

  • AT(默认)
  • TCC
  • Saga
  • XA

图片alt

SpringBoot项目集成Seata

1、在数据库中建表

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=3 DEFAULT CHARSET=utf8;

2、添加依赖

        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2021.1</version>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

3、修改配置文件

seata:
  enabled: true
  application-id: server-shop-order
  registry:
    type: file
  config:
    type: file
  service:
    grouplist:
      default: 127.0.0.1:8091
    vgroup-mapping:
      server-seata-money: default
  tx-service-group: server-seata-money
  client:
    undo:
      log-table: undo_log

图片alt

4、添加注解

  • order微服务的接口
    @PostMapping(value = "/swap")
    @GlobalTransactional
    public ResultVo swap() {
        iSwapBeijingService.swapMoney(1, -100);
        iSwapShanghaiService.swapMoney(1, 100);
        return ResultVo.SUCCESS();
    }
  • user微服务的接口
    @PostMapping(value = "/swapMoney")
    @GlobalTransactional
    public ResultVo swapMoney(@RequestParam("userId") Integer userId, @RequestParam("money") Integer money) {
        return tbMoneyBjService.swapMoney(userId, money);
    }
  • product微服务的接口
    @PostMapping(value = "/swapMoney")
    @GlobalTransactional
    public ResultVo swapMoney(@RequestParam("userId") Integer userId, @RequestParam("money") Integer money) {
        return tbMoneyShService.swapMoney(userId, money);
    }

OSS文件的断点续传

  • 修改工具类的的参数

这个工具类在一下目录:
专高5考试附件 6.7\专高5考试附件\专高5月考最新附件1.0\专高5 附件2\微服务\utils\OSSFileUtil.java

    String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
    String accessKeyId = "LTAI4FtqoX9aXnwt5WRPVtJ9";
    String accessKeySecret = "BjxfqbWKvpusWM328UGzwYT8PQYlm5";
    String bucket = "shenmazong";
    String ossUrl = "https://images.shenmazong.com/";
  • 普通上传
    @PostMapping(value = "/uploadFile")
    public ResultVo uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String url = ossFileUtil.uploadFile(file);

        return ResultVo.SUCCESS(url);
    }
  • 断点上传
    @PostMapping(value = "/upload")
    public ResultVo upload(@RequestParam("file") MultipartFile file) throws IOException {
        String url = ossFileUtil.uploadFileByBreakingPoint(file);

        return ResultVo.SUCCESS(url);
    }
  • 注意事项

在controller里面,需要注入上传的工具类

    @Autowired
    OSSFileUtil ossFileUtil;

FastDFS文件的上传