seata-1.4.0安装及在springcloud中使用详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

seata-1.4.0安装及在springcloud中使用详解

橙子VC   2020-12-17 我要评论
这篇文章主要介绍了seata-1.4.0安装及在springcloud中使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

seata-1.4.0安装及使用

 1、简介

Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
详见官方文档:https://seata.io/zh-cn/docs/overview/what-is-seata.html
网上的多是0.9.0版本的安装方式,这里记录安装seata-1.4.0版本的方式,在win10环境下安装,centos7与此相同。

下载

需要下载seata-1.4.0.zip 和 seata-server-1.4.0.zip两个安装包
下载地址:https://github.com/seata/seata/releases

2.1、seata-server-1.4.0

安装配置

解压压缩包,修改conf内的配置文件

2.1.1、registry.conf修改:

我使用的nacos作为配置中心和注册中心,使用将配置文件改为nacos

registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
 type = "nacos"
 loadBalance = "RandomLoadBalance"
 loadBalanceVirtualNodes = 10

 nacos {
 application = "seata-server"
 serverAddr = "127.0.0.1:8848"
 group = "SEATA_GROUP"
 namespace = ""
 cluster = "default"
 username = "nacos"
 password = "nacos"
 }
}

config {
 # file、nacos 、apollo、zk、consul、etcd3
 type = "nacos"
 nacos {
 serverAddr = "127.0.0.1:8848"
 namespace = ""
 group = "SEATA_GROUP"
 username = "nacos"
 password = "nacos"
 }
}

2.1.2、file.conf修改:

修改数据库地址,注意mysql5/mysql8驱动不同

store {
 ## store mode: file、db、redis
 mode = "db"
	## 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.cj.jdbc.Driver"
	 url = "jdbc:mysql://127.0.0.1:3307/seata?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true"
	 user = "root"
	 password = "123456"
	 minConn = 5
	 maxConn = 100
	 globalTable = "global_table"
	 branchTable = "branch_table"
	 lockTable = "lock_table"
	 queryLimit = 100
	 maxWait = 5000
	 }
	}

2.1.3、创建数据库

	创建数据库seata,并建立下面三张表(branch_table, global_table, lock_table),创建undo_log表放到业务库中
-- the table to store GlobalSession data
drop table if exists `global_table`;
create table `global_table` (
 `xid` varchar(128) not null,
 `transaction_id` bigint,
 `status` tinyint not null,
 `application_id` varchar(32),
 `transaction_service_group` varchar(32),
 `transaction_name` varchar(128),
 `timeout` int,
 `begin_time` bigint,
 `application_data` varchar(2000),
 `gmt_create` datetime,
 `gmt_modified` datetime,
 primary key (`xid`),
 key `idx_gmt_modified_status` (`gmt_modified`, `status`),
 key `idx_transaction_id` (`transaction_id`)
);
 
-- the table to store BranchSession data
drop table if exists `branch_table`;
create table `branch_table` (
 `branch_id` bigint not null,
 `xid` varchar(128) not null,
 `transaction_id` bigint ,
 `resource_group_id` varchar(32),
 `resource_id` varchar(256) ,
 `lock_key` varchar(128) ,
 `branch_type` varchar(8) ,
 `status` tinyint,
 `client_id` varchar(64),
 `application_data` varchar(2000),
 `gmt_create` datetime,
 `gmt_modified` datetime,
 primary key (`branch_id`),
 key `idx_xid` (`xid`)
);
 
-- the table to store lock data
drop table if exists `lock_table`;
create table `lock_table` (
 `row_key` varchar(128) not null,
 `xid` varchar(96),
 `transaction_id` long ,
 `branch_id` long,
 `resource_id` varchar(256) ,
 `table_name` varchar(32) ,
 `pk` varchar(36) ,
 `gmt_create` datetime ,
 `gmt_modified` datetime,
 primary key(`row_key`)
);
 
-- the table to store seata xid data
-- 0.7.0+ add context
-- you must to init this sql for you business databese. the seata server not need it.
-- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库)
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
drop table `undo_log`;
CREATE TABLE `undo_log` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `branch_id` bigint(20) NOT NULL,
 `xid` varchar(100) NOT NULL,
 `context` varchar(128) NOT NULL,
 `rollback_info` longblob NOT NULL,
 `log_status` int(11) NOT NULL,
 `log_created` datetime NOT NULL,
 `log_modified` datetime NOT NULL,
 `ext` varchar(100) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2.1.4、启动

seata-1.4\bin目录下:双击 seata-server.bat
nacos中查看服务是否注册成功

在这里插入图片描述

2.2 seata-1.4.0.

配置

解压安装包,并修改配置

2.2.3 修改config.txt

路径:script\config-center
修改service.vgroupMapping和数据库地址
service.vgroupMapping.system-server-group=default 
service.vgroupMapping.product-server-group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://192.168.111.130:3307/seata?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

注:
service.vgroupMapping.system-server-group=default 
service.vgroupMapping.product-server-group=default
 #system-server-group、product-server-group 与项目模块中yml配置的seata.tx-service-group内容相同

在这里插入图片描述

2.2.3 启动nacos-config.sh,

将配置注入到nacos中

1、在seata-server-1.4.0\script\config-center\nacos目录下,右键 Git Bash Here

在这里插入图片描述

2、输入命令:
sh nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -u nacos -w nacos

在这里插入图片描述 

返回成功

2.2.3、nacos中查看配置

在nacos配置列表中找到config.txt配置的vgroupMapping,即说明注入成功
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201214150808987.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzY5ODMzNg==,size_16,color_FFFFFF,t_70

3、SpringCloud 配置

 3.1、pom添加依赖

<dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
 <exclusions>
  <exclusion>
  <groupId>io.seata</groupId>
  <artifactId>seata-spring-boot-starter</artifactId>
  </exclusion>
 </exclusions>
 </dependency>
 <dependency>
 <groupId>io.seata</groupId>
 <artifactId>seata-spring-boot-starter</artifactId>
 <version>1.4.0</version>
 </dependency>
注:seata-spring-boot-starter的版本与安装版本一致

3.2、修改yml配置文件

seata:
 enabled: true
 application-id: product-server
 tx-service-group: product-server-group
 enable-auto-data-source-proxy: true
 config:
 type: nacos
 nacos:
 namespace:
 server-addr: 127.0.0.1:8848
 group: SEATA_GROUP
 userName: "nacos"
 password: "nacos"
 registry:
 type: nacos
 nacos:
 application: seata-server
 server-addr: 127.0.0.1:8848
 namespace:
 userName: "nacos"
 password: "nacos"

3.3、代码上添加注解:

application启动类上添加:
@EnableAutoDataSourceProxy
service上添加:
@GlobalTransactional(rollbackFor = Exception.class)

3.4、启动服务:

控制台输出:

在这里插入图片描述

3.5、测试

调用方控制台打印

在这里插入图片描述

被调用方异常控制台打印:

在这里插入图片描述

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们