当前位置:   article > 正文

分布式事务-seata AT模式-强一致性_seata 强一致性

seata 强一致性

1.seata原理

    Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC(类似2PC)、SAGA(长事务解决方案、基于状态机),为用户打造一站式的分布式解决方案。

    AT模式同tx-lcn类似,通过代理底层数据库连接,控制数据库事务的提交与回滚(必须依赖数据库);当然seate由于是由阿里维护的,同时支持多种注册中心,zookeeper、eureka等。

名词解释: TC 事务协调者、TM 事务管理器、RM 资源管理器、Microservices 微服务、DB 数据库

参考链接:

seate主项目:GitHub - seata/seata: Seata is an easy-to-use, high-performance, open source distributed transaction solution.

seate模板:seata-samples/springcloud-eureka-feign-mybatis-seata at master · seata/seata-samples · GitHub

seate快速使用:Quick Start

2.关键组件

  eureka、seate-0.8.0服务端、微服务1(hello)、微服务2(feign-consumer)

  spring boot:2.1.8.RELEASE

  spring cloud:Greenwich.SR2

  代码链接:GitHub - kickTec/springCloudDemo at transaction-seate

  csdn链接:transaction-seate.rar-Java文档类资源-CSDN下载

3.seate服务端参数

主要指定host,多个网卡自动绑定IP有时不对

  1. @Parameter(names = "--help", help = true)
  2. private boolean help;
  3. @Parameter(names = {"--host", "-h"}, description = "The ip to register to registry center.", order = 1)
  4. private String host;
  5. @Parameter(names = {"--port", "-p"}, description = "The port to listen.", order = 2)
  6. private int port = SERVER_DEFAULT_PORT;
  7. @Parameter(names = {"--storeMode", "-m"}, description = "log store mode : file, db", order = 3)
  8. private String storeMode;
  9. @Parameter(names = {"--serverNode", "-n"}, description = "server node id, such as 1, 2, 3.it will be generated according to the snowflake by default", order = 4)
  10. private Long serverNode;
  11. @Parameter(names = {"--seataEnv", "-e"}, description = "The name used for multi-configuration isolation.",
  12. order = 5)

windows启动seate服务器

seata-server.bat -h 192.168.0.18

4.微服务配置

maven配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.8.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.kenick</groupId>
  12. <artifactId>feign-consumer</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>feign-consumer</name>
  15. <description>feign-consumer</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <!--数据库-->
  29. <dependency>
  30. <groupId>org.mybatis.spring.boot</groupId>
  31. <artifactId>mybatis-spring-boot-starter</artifactId>
  32. <version>2.0.0</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>mysql</groupId>
  36. <artifactId>mysql-connector-java</artifactId>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <!-- pageHelper分页插件 -->
  40. <dependency>
  41. <groupId>com.github.pagehelper</groupId>
  42. <artifactId>pagehelper</artifactId>
  43. <version>4.0.0</version>
  44. </dependency>
  45. <!--druid-->
  46. <dependency>
  47. <groupId>com.alibaba</groupId>
  48. <artifactId>druid-spring-boot-starter</artifactId>
  49. <version>1.1.10</version>
  50. <exclusions>
  51. <exclusion>
  52. <artifactId>slf4j-api</artifactId>
  53. <groupId>org.slf4j</groupId>
  54. </exclusion>
  55. </exclusions>
  56. </dependency>
  57. <!--lombok-->
  58. <dependency>
  59. <groupId>org.projectlombok</groupId>
  60. <artifactId>lombok</artifactId>
  61. <version>1.18.8</version>
  62. </dependency>
  63. <!--seata-->
  64. <dependency>
  65. <groupId>com.alibaba.cloud</groupId>
  66. <artifactId>spring-cloud-alibaba-seata</artifactId>
  67. <version>2.1.0.RELEASE</version>
  68. <exclusions>
  69. <exclusion>
  70. <artifactId>seata-all</artifactId>
  71. <groupId>io.seata</groupId>
  72. </exclusion>
  73. </exclusions>
  74. </dependency>
  75. <dependency>
  76. <groupId>io.seata</groupId>
  77. <artifactId>seata-all</artifactId>
  78. <version>1.2.0</version>
  79. </dependency>
  80. <!--eureka client-->
  81. <dependency>
  82. <groupId>org.springframework.cloud</groupId>
  83. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  84. </dependency>
  85. <!--feign-->
  86. <dependency>
  87. <groupId>org.springframework.cloud</groupId>
  88. <artifactId>spring-cloud-starter-openfeign</artifactId>
  89. </dependency>
  90. </dependencies>
  91. <dependencyManagement>
  92. <dependencies>
  93. <dependency>
  94. <groupId>org.springframework.cloud</groupId>
  95. <artifactId>spring-cloud-dependencies</artifactId>
  96. <version>Greenwich.SR2</version>
  97. <type>pom</type>
  98. <scope>import</scope>
  99. </dependency>
  100. </dependencies>
  101. </dependencyManagement>
  102. <build>
  103. <plugins>
  104. <plugin>
  105. <groupId>org.springframework.boot</groupId>
  106. <artifactId>spring-boot-maven-plugin</artifactId>
  107. </plugin>
  108. </plugins>
  109. </build>
  110. </project>

seate特殊配置(seate服务器和微服务指定相同的tx-service-group):

spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group

file.conf文件中设置:

  1. #transaction service group mapping
  2. vgroupMapping.my_test_tx_group = "default"
  3. #only support when registry.type=file, please don't set multiple addresses
  4. default.grouplist = "192.168.0.18:8091"

register.conf中指定使用eureka:

  1. registry {
  2. # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  3. type = "eureka"
  4. nacos {
  5. serverAddr = "localhost"
  6. namespace = "public"
  7. cluster = "default"
  8. }
  9. eureka {
  10. serviceUrl = "http://192.168.0.105:8888/eureka"
  11. application = "default"
  12. weight = "1"
  13. }

由于seate AT模式是通过代理数据库连接方式实现的,在每个service服务中都需要进行如下配置:

  1. /**
  2. * 数据源代理
  3. */
  4. @Configuration
  5. public class DataSourceConfiguration {
  6. @Bean
  7. @ConfigurationProperties(prefix = "spring.datasource")
  8. public DataSource druidDataSource(){
  9. return new DruidDataSource();
  10. }
  11. @Primary
  12. @Bean("dataSource")
  13. public DataSourceProxy dataSource(DataSource druidDataSource){
  14. return new DataSourceProxy(druidDataSource);
  15. }
  16. @Bean
  17. public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy)throws Exception{
  18. SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
  19. sqlSessionFactoryBean.setDataSource(dataSourceProxy);
  20. sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
  21. .getResources("classpath:mybatis/mapping/*Mapper.xml"));
  22. sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
  23. return sqlSessionFactoryBean.getObject();
  24. }
  25. }

全局事务使用,在service上添加注释

  1. @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
  2. @Override
  3. public int saveUser(String userId, String name, int age) {
  4. logger.debug("本地开始保存用户!");
  5. User user = new User();
  6. user.setUserId(userId);
  7. user.setName(name);
  8. user.setAge(age);
  9. int ret = userMapper.insert(user);
  10. logger.debug("本地开始保存用户结果:" + ret);
  11. logger.debug("开始远程调用helloservice!");
  12. String remoteRet = helloService.addUser("remote_hello" + userId, name, age);
  13. logger.debug("远程调用helloservice结果:" + remoteRet);
  14. // 产生异常
  15. // int num = 1/0;
  16. return ret;
  17. }

其它的请参考项目代码配置。

5.业务流程

    调用feign-consumer工程的addUser接口,接口逻辑:先在本地保存用户,再调用hello服务的保存用户接口,相当于一次调用保存2了个用户信息。

6.依次启动eureka、seate服务器、微服务

正常流程

调用feign-consumer接口(192.168.0.18:4001/addUser),先在当前服务添加用户,再调用另一个微服务hello添加用户;2个用户信息添加成功;全局事务执行情况见下图。

feign-consumer日志

hello日志

异常流程:两次用户都保存成功后,后续feign-consumer 服务异常,最终两个工程都进行了回退。

feign-consumer日志

hello日志

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/858471
推荐阅读
相关标签
  

闽ICP备14008679号