• 基于Seata的分布式事务方案


    Seata中,有4种分布式事务实现方案

    XA、AT、TCC、Saga

    其中XA利用了数据库的分布式事务特性,AT相当于框架去控制事务回滚。TCC手写三个方法,saga手写两个方法。

    AT的性能和编写比较折中,是最常用的一种。TCC一些视频教程中介绍了单表单字段加中间表的方式存储过程数据,对于一次操作多个主子表数据的示例目前实现还是太繁琐了。

    根据官方实例和相关视频简单写了下代码。

    对于配置,能用默认的用默认,有示例配置的优先抄示例配置

    1.加依赖

    1. <!-- 版本对应关系
    2. Alibaba 2021.0.5 对应 Seata 1.6.1
    3. https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
    4. 这里依赖不是 spring-cloud-alibaba-seata
    5. -->
    6. <dependency>
    7. <groupId>com.alibaba.cloud</groupId>
    8. <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    9. <version>2021.0.5.0</version>
    10. </dependency>

    这里alibaba版本是2021.0.5所以spring-cloud-starter-alibaba-seata也用这个版本,注意版本对应

    2.写配置

    1. seata:
    2. registry:
    3. type: nacos
    4. nacos:
    5. application: seata-server
    6. server-addr: 127.0.0.1:8848
    7. group: SEATA_GROUP
    8. cluster: default
    9. #事务组
    10. tx-service-group: default_tx_group
    11. service:
    12. #事务组和集群名称映射
    13. vgroup-mapping:
    14. default_tx_group: default
    15. data-source-proxy-mode: AT

    注意,这里需要先配置好nacos和seata服务端,seata服务端早期是json配置的,新版的是yaml配置

    seata服务端配置application.yml

    1. # Copyright 1999-2019 Seata.io Group.
    2. #
    3. # Licensed under the Apache License, Version 2.0 (the "License");
    4. # you may not use this file except in compliance with the License.
    5. # You may obtain a copy of the License at
    6. #
    7. # http://www.apache.org/licenses/LICENSE-2.0
    8. #
    9. # Unless required by applicable law or agreed to in writing, software
    10. # distributed under the License is distributed on an "AS IS" BASIS,
    11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. # See the License for the specific language governing permissions and
    13. # limitations under the License.
    14. server:
    15. port: 7091
    16. spring:
    17. application:
    18. name: seata-server
    19. logging:
    20. config: classpath:logback-spring.xml
    21. file:
    22. path: ${log.home:${user.home}/logs/seata}
    23. extend:
    24. logstash-appender:
    25. destination: 127.0.0.1:4560
    26. kafka-appender:
    27. bootstrap-servers: 127.0.0.1:9092
    28. topic: logback_to_logstash
    29. console:
    30. user:
    31. username: seata
    32. password: seata
    33. seata:
    34. config:
    35. # support: nacos, consul, apollo, zk, etcd3
    36. type: file
    37. registry:
    38. # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    39. type: nacos
    40. nacos:
    41. application: seata-server
    42. server-addr: 127.0.0.1:8848
    43. group: SEATA_GROUP
    44. namespace:
    45. cluster: default
    46. username:
    47. password:
    48. context-path:
    49. store:
    50. # support: file 、 db 、 redis
    51. mode: file
    52. # server:
    53. # service-port: 8091 #If not configured, the default is '${server.port} + 1000'
    54. security:
    55. secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    56. tokenValidityInMilliseconds: 1800000
    57. ignore:
    58. urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login

    3.写代码

    将主调用的方法注解@Transactional改为@GlobalTransactional,其他微服务方法无需更改。

    完整代码见顶部

  • 相关阅读:
    CSS动效合集之实现气泡发散动画
    java程序员的十年
    SQLServer下载安装详细图解
    11.4-GPT4AllTools版本已开始对小部分GPT3.5用户内测推送
    U2Net 网络结构详解
    基于Pytorch的图卷积网络GCN实例应用及详解3.0
    LeetCode 每日一题 2022/11/7-2022/11/13
    PyQt5_pyqtgraph双均线组合工具
    可以从以下几点着手选择期货开户公司
    java计算机毕业设计国产精品动漫网站MyBatis+系统+LW文档+源码+调试部署
  • 原文地址:https://blog.csdn.net/gsls200808/article/details/133829136