下载地址:https://github.com/seata/seata/releases,下载 seata-server-1.4.2.tar.gz
安装包。
创建安装目录:
mkdir /opt/cloud -p
将下载的seata-server-1.4.2.tar.gz
拷贝到/opt/cloud
目录下,并且解压得到seata
目录。
tar -axvf seata-server-1.4.2.tar.gz
Seata-Server端存储模式(store.mode)现有file、db、redis三种,file模式无需改动,直接启动即可。
注: file模式为单机模式,全局事务会话信息内存中读写并持久化本地文件root.data,性能较高;
如果是seata分布式集群时推荐使用Redis或者DB模式,在此配置Mysql进行储存
,步骤如下:
1、建立数据库:seata
2、建表,参考语句:https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql
3、导入mysql的jar包:
默认情况下seata-server
没有导入mysql的驱动包,需要手动加入jar
导入到lib
目录下,比如将mysql-connector-java-8.0.18.jar
拷贝到/opt/cloud/seata/seata-server-1.4.2/lib
目录下。
4、修改file.conf:
修改file.conf中的mode为db
,并且指定在db{}
中指定mysql的配置信息,注意修改内容如下:
store {
## store mode: file、db、redis
mode = "db"
.....
db{
datasource = "druid"
dbType = "mysql"
driverClassName = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://111.229.160.175:3316/seata?useUnicode=true&serverTimezone=Asia/Shanghai"
user = "root"
password = "123456"
.....
.....
}
.....
}
配置registry.conf
文件:
vim /opt/cloud/seata/seata-server-1.4.2/conf/registry.conf
核心内容如下:
registry {
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "111.229.160.175:8848"
group = "SEATA_GROUP"
namespace = "13485083-0e72-47f7-af56-18b16a79ab56" //命名空间
cluster = "default" // 配置cluster名称
username = "nacos"
password = "nacos"
}
....
....
}
....
....
config {
type = "nacos"
nacos {
serverAddr ="111.229.160.175:8848"
namespace = "13485083-0e72-47f7-af56-18b16a79ab56"
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
// 默认值
dataId = "seataServer.properties"
}
....
....
}
....
从v1.4.2版本开始,已支持从一个Nacos dataId中获取所有配置信息,你只需要额外添加一个dataId配置项。
在nacos新建配置,此处dataId为seataServer.properties
。注意建立的nameSpace
和group
要与registry.conf
文件中的一致。
配置内容,参考:https://github.com/seata/seata/blob/develop/script/config-center/config.txt中的内容,并按实际需要进行修改,主要修改内容如下(其他保持不变):
# 配置事务组
# default_tx_group 事务分组名称可以自定义为项目名称,比如:seata_demo
# default表示registry.conf中配置的cluster名称
service.vgroupMapping.default_tx_group=default
....
store.mode=db
....
....
#与file.conf配置的mysql数据源一致
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://111.229.160.175:3316/seata?useUnicode=true&serverTimezone=Asia/Shanghai
store.db.user=root
store.db.password=123456
....
....
注意:云服务器安全组或者服务防火墙需要开启 8091
端口
seata
启动时,默认的JVM内存参数为:-Xmx2048m -Xms2048m -Xmn1024m
,如果有需要可以根据服务器的情况进行修改。
启动命令:
# -h: 注册到注册中心的ip
# -p: Server rpc 监听端口(默认8091)
# -n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
sh seata-server.sh -h 111.14.18.14 -p 8091 -n 1 >seata.log 2>1 & echo $! > pidfile.txt
为了方便快捷的实现启动与停止以及观察日志信息,通过创建sh脚本
实现,如下:
1、创建启动脚本:
vim /opt/cloud/seata/seata-server-1.4.2/bin/start.sh
内容:
#!/bin/sh
export LANG="en_US.UTF-8"
cd /opt/cloud/seata/seata-server-1.4.2/bin
runMessage=`ps aux | grep \`cat pidfile.txt\``
projectStartCommand="sh seata-server.sh"
if [[ $runMessage == *$projectStartCommand* ]]
then
echo "Application has starting ,restarting..."
kill -9 `cat pidfile.txt`
nohup sh seata-server.sh -h 162.14.115.18 -p 8091 -n 1 >seata.log 2>1 & echo $! > pidfile.txt
else
echo "Application has stopped ,starting..."
nohup sh seata-server.sh -h 162.14.115.18 -p 8091 -n 1 >seata.log 2>1 & echo $! > pidfile.txt
fi
2、创建停止脚本:
vim /opt/cloud/seata/seata-server-1.4.2/bin/stop.sh
内容:
#!/bin/sh
cd /opt/cloud/seata/seata-server-1.4.2/bin
PID=$(cat pidfile.txt)
if [ ${PID} ];
then
echo 'Application is stpping...'
echo kill $PID DONE
kill $PID
else
echo 'Application is already stopped...'
fi
3、修改脚本权限:
chmod 777 stop.sh
chmod 777 start.sh
4、启动、停止:
# 启动
sh start.sh
# 停止
sh stop.sh
修改seata-server.sh文件,指定JAVA_HOME路径
手动指定seata-server.sh
中JAVACMD
的值,加入以下语句:
# 指定实际的jdk路径
JAVACMD=/usr/local/java/jdk1.8.0_131/bin/java
创建service:
vim /usr/lib/systemd/system/seata.service
内容:
[Unit]
Description=seata
[Service]
Type=forking
ExecStart=/opt/cloud/seata/bin/seata-server-1.4.2/start.sh
ExecStop=/opt/cloud/seata/bin/seata-server-1.4.2/stop.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
加入自启:
systemctl daemon-reload
systemctl enable seata.service
启动、停止:
# 启动
systemctl start seata
# 停止
systemctl stop seata
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-seataartifactId>
dependency>
注意:
在引入pom
需要注意SpringCloudAlibaba
的版本,版本对应关系查询,比如:
如果SpringCloudAlibaba
与Seata
的默认版本不对应时,可以单独引入seata
依赖,比如:
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-seataartifactId>
<exclusions>
<exclusion>
<artifactId>seata-spring-boot-starterartifactId>
<groupId>io.seatagroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.seatagroupId>
<artifactId>seata-spring-boot-starterartifactId>
<version>1.4.2version>
dependency>
在微服务客户端连接的数据库中建立undo_log
表,语句如下:
CREATE TABLE IF NOT EXISTS `undo_log`
(
`branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(100) NOT NULL COMMENT 'global transaction id',
`context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
`log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` DATETIME(6) NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime',
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
seata:
enabled: true
application-id: ${spring.application.name}
# 事务组的名称,对应service.vgroupMapping.default_tx_group=xxx中配置的default_tx_group
tx-service-group: default_tx_group
# 配置事务组与集群的对应关系
service:
vgroup-mapping:
# default_tx_group为事务组的名称,default为集群名称(与registry.conf中的一致)
default_tx_group: default
disable-global-transaction: false
registry:
type: nacos
nacos:
application: seata-server
server-addr: 112.15.114.18:8848
group: SEATA_GROUP
namespace: 64ed9ca7-d705-4655-b4e4-f824e420a12a
username: nacos
password: nacos
# registry.conf中,配置cluster名称
cluster: default
config:
type: nacos
nacos:
server-addr: 112.15.114.18:8848
group: SEATA_GROUP
namespace: 64ed9ca7-d705-4655-b4e4-f824e420a12a
username: nacos
password: nacos
# nacos配置中心配置的dataId
data-id: seataServer.properties
全局事务注解:
@GlobalTransactional
在事务发起的微服务方法上加上注解@GlobalTransactional
表示全局事务中的一个TM。
注意:异常不能被捕获需要抛出,事务才会发起回滚
比如:
@RestController
@RequestMapping("test")
public class TestController {
@GetMapping("/insert")
@GlobalTransactional
public void selectUserWageByUserId() {
......
......
// 如果RootContext.getXID()不为空,则表示seata生效
System.out.println("RootContext.getXID():" + RootContext.getXID());
}
}
效果:
可以看到在undo_log
以及seata服务
的global_table
、branch_table
、lock_table
中存在seata
执行分布式事务时的数据。
隔离级别:
Seata的隔离级别默认为读未提交
。
数据源支持情况如下:
参考:https://seata.io/zh-cn/docs/user/datasource.html