• spring cloud alibaba 集成seata


    1.启动服务端

            1.下载 seata-server-1.4.2

            2.创建数据库

             

    1. DROP DATABASE IF EXISTS `ry-seata`;
    2. CREATE DATABASE `ry-seata` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    3. SET NAMES utf8mb4;
    4. SET FOREIGN_KEY_CHECKS = 0;
    5. USE `ry-seata`;
    6. -- -------------------------------- The script used when storeMode is 'db' --------------------------------
    7. -- the table to store GlobalSession data
    8. CREATE TABLE IF NOT EXISTS `global_table`
    9. (
    10. `xid` VARCHAR(128) NOT NULL,
    11. `transaction_id` BIGINT,
    12. `status` TINYINT NOT NULL,
    13. `application_id` VARCHAR(32),
    14. `transaction_service_group` VARCHAR(32),
    15. `transaction_name` VARCHAR(128),
    16. `timeout` INT,
    17. `begin_time` BIGINT,
    18. `application_data` VARCHAR(2000),
    19. `gmt_create` DATETIME,
    20. `gmt_modified` DATETIME,
    21. PRIMARY KEY (`xid`),
    22. KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
    23. KEY `idx_transaction_id` (`transaction_id`)
    24. ) ENGINE = InnoDB
    25. DEFAULT CHARSET = utf8mb4;
    26. -- the table to store BranchSession data
    27. CREATE TABLE IF NOT EXISTS `branch_table`
    28. (
    29. `branch_id` BIGINT NOT NULL,
    30. `xid` VARCHAR(128) NOT NULL,
    31. `transaction_id` BIGINT,
    32. `resource_group_id` VARCHAR(32),
    33. `resource_id` VARCHAR(256),
    34. `branch_type` VARCHAR(8),
    35. `status` TINYINT,
    36. `client_id` VARCHAR(64),
    37. `application_data` VARCHAR(2000),
    38. `gmt_create` DATETIME(6),
    39. `gmt_modified` DATETIME(6),
    40. PRIMARY KEY (`branch_id`),
    41. KEY `idx_xid` (`xid`)
    42. ) ENGINE = InnoDB
    43. DEFAULT CHARSET = utf8mb4;
    44. -- the table to store lock data
    45. CREATE TABLE IF NOT EXISTS `lock_table`
    46. (
    47. `row_key` VARCHAR(128) NOT NULL,
    48. `xid` VARCHAR(96),
    49. `transaction_id` BIGINT,
    50. `branch_id` BIGINT NOT NULL,
    51. `resource_id` VARCHAR(256),
    52. `table_name` VARCHAR(32),
    53. `pk` VARCHAR(36),
    54. `gmt_create` DATETIME,
    55. `gmt_modified` DATETIME,
    56. PRIMARY KEY (`row_key`),
    57. KEY `idx_branch_id` (`branch_id`)
    58. ) ENGINE = InnoDB
    59. DEFAULT CHARSET = utf8mb4;

            3.填写配置

            seata-server-1.4.2/conf/registry.conf

    1. registry {
    2. # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
    3. type = "nacos"
    4. nacos {
    5. application = "seata-server"
    6. serverAddr = "127.0.0.1:8848"
    7. group = "DEFAULT_GROUP"
    8. namespace = ""
    9. cluster = "DEFAULT"
    10. username = "nacos"
    11. password = "nacos"
    12. }
    13. eureka {
    14. serviceUrl = "http://localhost:8761/eureka"
    15. application = "default"
    16. weight = "1"
    17. }
    18. redis {
    19. serverAddr = "localhost:6379"
    20. db = 0
    21. password = ""
    22. cluster = "default"
    23. timeout = 0
    24. }
    25. zk {
    26. cluster = "default"
    27. serverAddr = "127.0.0.1:2181"
    28. sessionTimeout = 6000
    29. connectTimeout = 2000
    30. username = ""
    31. password = ""
    32. }
    33. consul {
    34. cluster = "default"
    35. serverAddr = "127.0.0.1:8500"
    36. aclToken = ""
    37. }
    38. etcd3 {
    39. cluster = "default"
    40. serverAddr = "http://localhost:2379"
    41. }
    42. sofa {
    43. serverAddr = "127.0.0.1:9603"
    44. application = "default"
    45. region = "DEFAULT_ZONE"
    46. datacenter = "DefaultDataCenter"
    47. cluster = "default"
    48. group = "SEATA_GROUP"
    49. addressWaitTime = "3000"
    50. }
    51. file {
    52. name = "file.conf"
    53. }
    54. }
    55. config {
    56. # file、nacos 、apollo、zk、consul、etcd3
    57. type = "nacos"
    58. nacos {
    59. serverAddr = "127.0.0.1:8848"
    60. namespace = ""
    61. group = "SEATA_GROUP"
    62. username = "nacos"
    63. password = "nacos"
    64. dataId = "seataServer.properties"
    65. }
    66. consul {
    67. serverAddr = "127.0.0.1:8500"
    68. aclToken = ""
    69. }
    70. apollo {
    71. appId = "seata-server"
    72. ## apolloConfigService will cover apolloMeta
    73. apolloMeta = "http://192.168.1.204:8801"
    74. apolloConfigService = "http://192.168.1.204:8080"
    75. namespace = "application"
    76. apolloAccesskeySecret = ""
    77. cluster = "seata"
    78. }
    79. zk {
    80. serverAddr = "127.0.0.1:2181"
    81. sessionTimeout = 6000
    82. connectTimeout = 2000
    83. username = ""
    84. password = ""
    85. nodePath = "/seata/seata.properties"
    86. }
    87. etcd3 {
    88. serverAddr = "http://localhost:2379"
    89. }
    90. file {
    91. name = "file.conf"
    92. }
    93. }

            4.nacos中seata的配置文件seataServer.properties内容

            

    1. store.mode=db
    2. store.db.datasource=druid
    3. store.db.dbType=mysql
    4. store.db.driverClassName=com.mysql.jdbc.Driver
    5. store.db.url=jdbc:mysql://127.0.0.1:3306/ry-seata?rewriteBatchedStatements=true
    6. store.db.user=root
    7. store.db.password=123456
    8. store.db.minConn=5
    9. store.db.maxConn=30
    10. store.db.globalTable=global_table
    11. store.db.branchTable=branch_table
    12. store.db.queryLimit=100
    13. store.db.lockTable=lock_table
    14. store.db.maxWait=5000

            5.运行服务端

            seata-server.bat -h 192.168.1.5

            启动成功

    2.配置微服务项目

            1.引入依赖

            

    1. <!-- SpringBoot Seata -->
    2. <dependency>
    3. <groupId>com.alibaba.cloud</groupId>
    4. <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    5. <version>2.2.5.RELEASE</version>
    6. <exclusions>
    7. <exclusion>
    8. <groupId>io.seata</groupId>
    9. <artifactId>seata-spring-boot-starter</artifactId>
    10. </exclusion>
    11. </exclusions>
    12. </dependency>
    13. <dependency>
    14. <groupId>io.seata</groupId>
    15. <artifactId>seata-spring-boot-starter</artifactId>
    16. <version>1.4.2</version>
    17. </dependency>

            2.修改微服务的配置文件

            

    1. # seata配置
    2. seata:
    3. # 默认关闭,如需启用spring.datasource.dynami.seata需要同时开启
    4. enabled: true
    5. # Seata 应用编号,默认为 ${spring.application.name}
    6. application-id: seata-server
    7. # Seata 事务组编号,用于 TC 集群名
    8. tx-service-group: default_tx_group
    9. # 关闭自动代理
    10. enable-auto-data-source-proxy: false
    11. # 服务配置项
    12. service:
    13. # 虚拟组和分组的映射
    14. vgroup-mapping:
    15. #映射集群名称
    16. default_tx_group: DEFAULT
    17. registry:
    18. type: nacos
    19. nacos:
    20. application: seata-server
    21. server-addr: 127.0.0.1:8848
    22. namespace:
    23. username: nacos
    24. password: nacos
    25. group: DEFAULT_GROUP
    26. data-source-proxy-mode: XA

               2.微服务链接的数据库需要建表

                

    1. -- for AT mode you must to init this sql for you business database. the seata server not need it.
    2. CREATE TABLE IF NOT EXISTS `undo_log`
    3. (
    4. `branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
    5. `xid` VARCHAR(100) NOT NULL COMMENT 'global transaction id',
    6. `context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    7. `rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
    8. `log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
    9. `log_created` DATETIME(6) NOT NULL COMMENT 'create datetime',
    10. `log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime',
    11. UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
    12. ) ENGINE = InnoDB
    13. AUTO_INCREMENT = 1
    14. DEFAULT CHARSET = utf8mb4 COMMENT ='AT transaction mode undo table';

              3.启动微服务

                    链接成功

            4.测试 @GlobalTransactional 注解

            

    1. /**
    2. * 删除物业公司
    3. */
    4. @RequiresPermissions("business:propertyCompany:remove")
    5. @Log(title = "物业公司", businessType = BusinessType.DELETE)
    6. @DeleteMapping("/{ids}")
    7. @GlobalTransactional
    8. public AjaxResult remove(@PathVariable Long[] ids)
    9. {
    10. //return toAjax(bPropertyCompanyService.deleteBPropertyCompanyByIds(ids));
    11. List<Long> longs = Arrays.asList(ids);
    12. for(Long id:longs){
    13. BPropertyCompany byId = bPropertyCompanyService.getById(id);
    14. ClosePropertyCompanyOrCommunityParam param=new ClosePropertyCompanyOrCommunityParam();
    15. param.setId(String.valueOf(byId.getId()));
    16. param.setType(0);
    17. param.setPhone(byId.getPhone());
    18. R<Map> rmap= remoteUserService.closePropertyCompanyOrCommunity(param, SecurityConstants.INNER);
    19. if (rmap.getCode() == 500){
    20. throw new RuntimeException(rmap.getMsg());
    21. }
    22. }
    23. boolean b = bPropertyCompanyService.removeByIds(Arrays.asList(ids));
    24. return toAjax(b);
    25. }

    需要注意的是:这两处必须一致

  • 相关阅读:
    SQL SERVER中存储过程的使用场景
    【scikit-learn基础】--『回归模型评估』之损失分析
    springboot+vue实现Minio文件存储
    Linux/shell命令
    自学软件测试1个半月上岸拿5个offer
    解决ubuntu网卡失效-更换内核版本
    B+树结构与索引<二> _ 索引页示例
    数字孪生3d车间、虚拟车间、数字化三维车间的案例比较
    Android垂直跑马灯
    ​P1190 [NOIP2010 普及组] 接水问题 【贪心】​
  • 原文地址:https://blog.csdn.net/qq_31683775/article/details/133929097