目录
前言
seata是阿里的分布式事务解决方案。seata 版本从1.0开始与之前的版本配置与使用有了较大区别。本文使用的版本是1.4
特别注意
除了下载seata-server以外,版本大于1.0的,还需要注意这个:https://github.com/seata/seata/tree/1.4.0/script。一些额外的配置文件都在这里了,根据自己的seata版本切换对应版本的分支即可。 目录结构
1 2 3 |
client # seata客户端所需的配置与文件都在这里 config-center # 把seata配置导入到注册的服务中心所需的工具。 server # seata服务端所需的配置与文件都在这里 |
服务端
下载
下载地址:https://github.com/seata/seata/releases 下载并解压
1 2 3 4 |
# 下载 wget https://github.com/seata/seata/releases/download/v1.4.2/seata-server-1.4.2.tar.gz # 解压 tar -xzvf seata-server-1.4.2.tar.gz |
目录结构
1 2 3 4 5 6 7 8 9 10 |
├── bin │ ├── seata-server.bat # windows seata服务启动脚本 │ └── seata-server.sh # linux seata服务启动脚本 ├── conf │ ├── file.conf # 存储配置 │ ├── file.conf.example │ └── registry.conf # 配置文件 ├── lib ├── LICENSE └── logs |
配置
registry.conf
registry.conf只有registry 和 config 两个大项配置。 registry :把seata服务注册到哪里。默认是file,大多数选择的是nacos 。选了那种类型,就配置对应的类型即可。 config :把seata服务的配置放到哪里。默认是file。
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" # 选择nacos nacos { application = "seata-server" # 服务名称 serverAddr = "127.0.0.1:8848" # nacos地址 group = "SEATA_GROUP" # 服务分组名称 namespace = "" # 服务空间 cluster = "default" # 不需要改 # 注册服务和读取配置时的用户名和密码, # 如果你开启了注册服务和读取配置的认证,nacos默认是不开启的。 # (不是8848端口那个页面登录的用户名和密码) username = "" # nacos用户名 password = "" # nacos密码 } eureka { serviceUrl = "http://localhost:8761/eureka" application = "default" weight = "1" } redis { serverAddr = "localhost:6379" db = 0 password = "" cluster = "default" timeout = 0 } zk { cluster = "default" serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } consul { cluster = "default" serverAddr = "127.0.0.1:8500" aclToken = "" } etcd3 { cluster = "default" serverAddr = "http://localhost:2379" } sofa { serverAddr = "127.0.0.1:9603" application = "default" region = "DEFAULT_ZONE" datacenter = "DefaultDataCenter" cluster = "default" group = "SEATA_GROUP" addressWaitTime = "3000" } file { name = "file.conf" } } config { # file、nacos 、apollo、zk、consul、etcd3 type = "nacos" # 选择nacos nacos { serverAddr = "127.0.0.1:8848" # nacos地址 # 先去nacos创建配置的命名空间 namespace = "21493e24-cb74-43c1-b958-f7d2d0f6e76c" group = "SEATA_GROUP" username = "" password = "" dataId = "seataServer.properties" } consul { serverAddr = "127.0.0.1:8500" aclToken = "" } apollo { appId = "seata-server" ## apolloConfigService will cover apolloMeta apolloMeta = "http://192.168.1.204:8801" apolloConfigService = "http://192.168.1.204:8080" namespace = "application" apolloAccesskeySecret = "" cluster = "seata" } zk { serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" nodePath = "/seata/seata.properties" } etcd3 { serverAddr = "http://localhost:2379" } file { name = "file.conf" } } |
file.conf
这个是配置存储介质的。目前暂时支持file、db、redis。默认是file,如果选择db的话需要在数据库中导入三个表(https://github.com/seata/seata/tree/1.4.0/script/server/db,根据自己的数据库选择导入,注意分支版本要和seata的版本一致)。 Server端存储模式(store.mode)现有file、db、redis三种(后续将引入raft,mongodb),file模式无需改动,直接启动即可,下面专门讲下db和redis启动步骤。 注: file模式为单机模式,全局事务会话信息内存中读写并持久化本地文件root.data,性能较高; db模式为高可用模式,全局事务会话信息通过db共享,相应性能差些; redis模式Seata-Server 1.3及以上版本支持,性能较高,存在事务信息丢失风险,请提前配置合适当前场景的redis持久化配置.
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 57 58 59 60 61 62 63 64 |
store { ## store mode: file、db、redis mode = "db" # 选择db,修改db配置 ## rsa decryption public key publicKey = "" ## file store property file { ## store location dir dir = "sessionStore" # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions maxBranchSessionSize = 16384 # globe session size , if exceeded throws exceptions maxGlobalSessionSize = 512 # file buffer size , if exceeded allocate new buffer fileWriteBufferCacheSize = 16384 # when recover batch read size sessionReloadReadSize = 100 # async, sync flushDiskMode = async } ## database store property db { ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc. datasource = "druid" ## mysql/oracle/postgresql/h2/oceanbase etc. dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param url = "jdbc:mysql://192.168.43.133:3306/seata?rewriteBatchedStatements=true" user = "root" password = "123456" minConn = 5 maxConn = 100 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 } ## redis store property redis { ## redis mode: single、sentinel mode = "single" ## single mode property single { host = "127.0.0.1" port = "6379" } ## sentinel mode property sentinel { masterName = "" ## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381" sentinelHosts = "" } password = "" database = "0" minConn = 1 maxConn = 10 maxTotal = 100 queryLimit = 100 } } |
将配置导入到nacos(config配置为nacos的看这里;file的,跳过这里)
需要的文件:https://github.com/seata/seata/tree/1.4.0/script/config-center,注意分支版本要和seata的版本一致。 下载并根据自己的需求修改config.txt config.txt就是seata各种详细的配置,执行 nacos-config.sh 即可将这些配置导入到nacos,这样就不需要将file.conf和registry.conf放到我们的项目中了,需要什么配置就直接从nacos中读取。
执行导入
1 |
bash nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -t 3a2aea46-07c6-4e21-9a1e-8946cde9e2b3 -u nacos -w nacos |
命令解析:
-h -p 指定nacos的端口地址;-g 指定配置的分组,注意,是配置的分组;-t 指定命名空间id; -u -w指定nacos的用户名和密码,同样,这里开启了nacos注册和配置认证的才需要指定。
得到输出
1 2 3 4 5 6 7 8 9 10 11 |
set nacosAddr=127.0.0.1:8848 set group=SEATA_GROUP Set transport.type=TCP successfully Set transport.server=NIO successfully . . . ========================================================================= Complete initialization parameters, total-count:80 , failure-count:0 ========================================================================= Init nacos config finished, please start seata-server. |
最后直接启动seata
1 |
./seata/bin/seata-server.sh -h 127.0.0.1 |
启动的时候一定要指定 ip,否则会引起can not connect to services-server
客户端
引入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>seata版本</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.2.1.RELEASE</version> <exclusions> <exclusion> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> </exclusion> <exclusion> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> </exclusion> </exclusions> </dependency> |
application.yml配置seata
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 |
#seata配置 seata: enabled: true application-id: wemall-order #你的当前服务的application name #这里的名字与服务端file.conf中vgroup_mapping.my_test_tx_group = "default"相同 tx-service-group: my_test_tx_group enable-auto-data-source-proxy: true # use-jdk-proxy: false service: #这里的名字与服务端file.conf中vgroup_mapping.my_test_tx_group = "default"相同 vgroup-mapping: my_test_tx_group: default #这里的名字与服务端file.conf中default.grouplist = "127.0.0.1:8091"相同 grouplist: default: 192.168.43.133:8091 # disable-global-transaction: false config: type: nacos nacos: namespace: 21493e24-cb74-43c1-b958-f7d2d0f6e76c #这里的地址就是你的nacos的地址,可以更换为线上 serverAddr: 192.168.43.133:8848 #这里的名字就是registry.conf中 nacos的group名字 group: SEATA_GROUP registry: type: nacos nacos: application: seata-server #这里的地址就是你的nacos的地址,可以更换为线上 server-addr: 192.168.43.133:8848 |
在事务的主入口(即第一个事务)通过 @GlobalTransactional 开启全局异常,示例代码:
1 2 3 4 5 6 |
@GlobalTransactional @GetMapping({"create"}) public String create(String name,Integer age) { ... return "创建成功"; } |
其余事务@Transactional
参考
部署指南(官方文档)
分布式事务 SEATA-1.4.1 AT模式 配合NACOS 应用
Seata 1.4.0 + nacos配置和使用,超详细