当前位置:   article > 正文

第十二章 Seata快速入门

seata快速入门

第十二章 Seata快速入门



安装配置步骤

下载地址:https://github.com/seata/seata/releases
我下载的是Seata的1.5.2版本进行演示。

  1. 将下载的安装包解压到指定目录
  2. 修改conf目录下的application.yml配置文件

主要修改:

  • 配置中心修改为Nacos
  • Nacos注册信息
server:
  port: 7091

# 服务名
spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${user.home}/logs/seata
  extend:
    logstash-appender:
      destination: 127.0.0.1:4560
    kafka-appender:
      bootstrap-servers: 127.0.0.1:9092
      topic: logback_to_logstash

console:
  user:
    username: seata
    password: seata

seata:
  config:
    # 设置nacos作为配置中心,namespace可以为默认的public。
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: 
      group: SEATA_GROUP
      username: nacos
      password: nacos

  registry:
    # 设置注册服务的信息
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: 
      cluster: default
      username: nacos
      password: nacos
      
#  server:
#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  1. 在nacos配置中心中添加配置和修改配置

seata1.5.2的文件夹中为我们提供了一个git文件,可以直接帮我们添加配置到nacos,我们在seata\script\config-center\nacos路径下可以找到该文件,确保nacos已启动,直接点击运行即可。

在这里插入图片描述
运行完毕后可以在nacos中看见相关配置项
在这里插入图片描述

  1. 项目中application.yml的seata配置
seata:
  # 设置分组,1.5以后版本默认事务分组的由my_test_tx_group 修改为 default_tx_group。
  # 这里我选择新建一个my_tx_group分组进行测试
  # 事务分组:seata的资源逻辑,可以按微服务的需要,在应用程序(客户端)对自行定义事务分组,每组取一个名字。
  tx-service-group: my_tx_group
  enabled: true
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group : SEATA_GROUP
      username: nacos
      password: nacos
  service:
  # 配置TC集群名clusterName
    vgroup-mapping:
      my_tx_group: default
  # 配置真实TC服务地址127.0.0.1:8091    
    grouplist:
      default: 127.0.0.1:8091
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  1. 在nacos配置中心添加分组配置

在这搜索service.vgroupMapping,可以看到默认分组的配置
在这里插入图片描述

默认TC集群名为default

在这里插入图片描述

我们新建一个 service.vgroupMapping.my_tx_group的配置

在这里插入图片描述

案例测试

相关代码地址:https://gitee.com/xin-77/spring-cloud-alibaba.git

启动order模块

成功启动后我们可以在这看到seata的相关信息

在这里插入图片描述
自己新建的事务分组
在这里插入图片描述

启动其它两个模块,成功注册到nacos。

在这里插入图片描述

事务成功测试

访问http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100测试成功
在这里插入图片描述
查看后台日志

在这里插入图片描述

事务失败测试

在account模块添加异常方法
在这里插入图片描述

访问http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100测试

在这里插入图片描述

事务失败,查看后台日志
在这里插入图片描述


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

闽ICP备14008679号