seata-server为seata中的事务协调器。
seata的wiki
https://github.com/seata/seata/wiki/Home_Chinese
一、下载并安装
wget -P /opt/downloads https://github.com/seata/seata/releases/download/v0.5.1/seata-server-0.5.1.tar.gz mkdir /opt/seata-server tar zxvf /opt/downloads/seata-server-0.5.1.tar.gz -C /opt/seata-server
二、导入配置
本文使用nacos作为配置中心和服务发现,file、apollo、redis、zk、consul等也可以举一反三。
我们这里用默认的导入先让seata-server跑起来,过后对着file.conf和java程序启动后的报错调整,重新导入即可。
vim /opt/seata-server/conf/nacos-config.txt
以下是我根据自己的环境修改后的配置值
cluster":"default","ip":"sxw-PC","metadata":{}
you must config ip when you start your fescar server .
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.thread-factory.boss-thread-prefix=NettyBoss
transport.thread-factory.worker-thread-prefix=NettyServerNIOWorker
transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandler
transport.thread-factory.share-boss-worker=false
transport.thread-factory.client-selector-thread-prefix=NettyClientSelector
transport.thread-factory.client-selector-thread-size=1
transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThread
transport.thread-factory.boss-thread-size=1
transport.thread-factory.worker-thread-size=8
service.vgroup_mapping.my_test_tx_group=default
service.vgroup_mapping.user-web-fescar-service-group=default
service.vgroup_mapping.user-fescar-service-group=default
service.vgroup_mapping.order-fescar-service-group=default
service.vgroup_mapping.business-service-fescar-service-group=default
service.vgroup_mapping.account-service-fescar-service-group=default
service.vgroup_mapping.storage-service-fescar-service-group=default
service.vgroup_mapping.order-service-fescar-service-group=default
service.default.grouplist=192.168.1.101:8091
service.enableDegrade=false
service.disable=false
client.async.commit.buffer.limit=10000
client.lock.retry.internal=10
client.lock.retry.times=30
store.mode=file
store.file.dir=file_store/data
store.file.max-branch-session-size=16384
store.file.max-global-session-size=512
store.file.file-write-buffer-cache-size=16384
store.file.session.reload.read_size=100
store.db.driver_class=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.1.102:3306/seata_server
store.db.user=root
store.db.password=123456
vim /opt/seata-server/conf/nacos-config.sh
for line in $(cat nacos-config.txt)
改为
for line in $(cat /opt/seata-server/conf/nacos-config.txt)
导入配置到nacos。
格式为sh nacos-config.sh $Nacos-Server-IP
因为nacos-config.sh脚本中已经把8848端口写死,如果你的nacos-server不是8848端口,请修改nacos-config.sh。
bash /opt/seata-server/conf/nacos-config.sh 192.168.1.102
192.168.1.102是nacos的ip
脚本执行最后输出 "init nacos config finished, please start seata-server." 说明推送配置成功。若想进一步确认可登陆Nacos控制台->配置列表->筛选查询Group为FESCAR_GROUP的配置项。
将/opt/seata-server/conf/nacos-config.txt 脚本修改后重新导入即可。
三、启动seata-server(事务协调器)
修改日志目录(非必须)
vim /opt/seata-server/conf/logback.xml
改为
配置修改
vim /opt/seata-server/conf/registry.conf 并复制到java代码的properties目录下
registry { type = "nacos" nacos { serverAddr = "192.168.1.102" namespace = "public" cluster = "default" } } config { type = "nacos" nacos { serverAddr = "192.168.1.102" namespace = "public" cluster = "default" } }
注:serverAddr不要填端口号 public为小写
启动格式sh seata-server.sh $LISTEN_PORT $PATH_FOR_PERSISTENT_DATA $IP(此参数可选)
$IP参数 用于多IP环境下指定 Fescar-Server 注册服务的IP 虽然是可选,但还是要填,之前我偷懒没填,一大堆虚拟ip各种乱定位。
命令启动
sh /opt/seata-server/bin/seata-server.sh 8091 file 192.168.1.102
守护进程启动
vim /opt/seata-server/startup.sh
填入
#!/bin/bash sh /opt/seata-server/bin/seata-server.sh 8091 file 192.168.1.102
vim /lib/systemd/system/seata-server.service
文件中填入
[Unit] Description=seata-server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
ExecStart=/opt/seata-server/startup.sh
Restart=always
PrivateTmp=true
[Install]
WantedBy=multi-user.target
赋予权限
chmod 777 /opt/seata-server/startup.sh chmod 777 /lib/systemd/system/seata-server.service
启用服务
systemctl enable seata-server.service
systemctl daemon-reload
运行
systemctl start seata-server.service
查看状态
systemctl status seata-server.service
查看进程
ps -ef|grep seata-server
运行成功后可在Nacos控制台的 服务列表 看到 服务名serverAddr的条目
四、创建数据表
(创建mysql数据库略)
UNDO_LOG table is required by SEATA AT mode.
CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `branch_id` bigint(20) NOT NULL, `xid` varchar(100) 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;
每个业务数据库都要建一个undo_log表