Seata

Stella981
• 阅读 622

seata-server为seata中的事务协调器。

参考:http://seata.io/en-us/docs/ops/deploy-server.html

只能在jdk8环境

seata的wiki

https://github.com/seata/seata/wiki/Home_Chinese

一、下载并安装

wget -P /opt/downloads https://github.com/seata/seata/releases/download/v0.9.0/seata-server-0.9.0.tar.gz
mkdir /opt/seata-server
tar zxvf /opt/downloads/seata-server-0.9.0.tar.gz -C /opt/seata-servermv /opt/seata-server/seata/* /opt/seata-serverrm -r /opt/seata-server/seata/

二、导入配置

本文使用nacos作为配置中心和服务发现,file、apollo、redis、zk、consul等也可以举一反三。

我们这里用默认的导入先让seata-server跑起来,过后对着file.conf和java程序启动后的报错调整,重新导入即可。

vim /opt/seata-server/conf/nacos-config.txt

以下是我根据自己的环境修改后的配置值

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
transport.shutdown.wait=3
service.vgroup_mapping.my_test_tx_group=default
service.vgroup_mapping.user-web-seata-service-group=default
service.vgroup_mapping.user-seata-service-group=default
service.vgroup_mapping.order-seata-service-group=default
service.vgroup_mapping.user2-seata-service-group=default
service.vgroup_mapping.order2-seata-service-group=default
service.vgroup_mapping.business-service-seata-service-group=default
service.vgroup_mapping.account-service-seata-service-group=default
service.vgroup_mapping.storage-service-seata-service-group=default
service.vgroup_mapping.order-service-seata-service-group=default
service.default.grouplist=192.168.0.101:8091
service.enableDegrade=false
service.disable=false
service.max.commit.retry.timeout=-1
service.max.rollback.retry.timeout=-1
client.async.commit.buffer.limit=10000
client.lock.retry.internal=10
client.lock.retry.times=30
client.lock.retry.policy.branch-rollback-on-conflict=true
client.table.meta.check.enable=true
client.report.retry.count=5
client.tm.commit.retry.count=1
client.tm.rollback.retry.count=1
store.mode=db
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.flush-disk-mode=async
store.file.session.reload.read_size=100
store.db.datasource=druid
store.db.db-type=mysql
store.db.driver-class-name=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.0.101:3306/seata?useUnicode=true
store.db.user=root
store.db.password=root
store.db.min-conn=1
store.db.max-conn=3
store.db.global.table=global_table
store.db.branch.table=branch_table
store.db.query-limit=100
store.db.lock-table=lock_table
recovery.committing-retry-period=1000
recovery.asyn-committing-retry-period=1000
recovery.rollbacking-retry-period=1000
recovery.timeout-retry-period=1000
transaction.undo.data.validation=true
transaction.undo.log.serialization=jackson
transaction.undo.log.save.days=7
transaction.undo.log.delete.period=86400000
transaction.undo.log.table=undo_log
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registry-type=compact
metrics.exporter-list=prometheus
metrics.exporter-prometheus-port=9898
support.spring.datasource.autoproxy=false

ha见https://github.com/seata/seata-samples/tree/master/ha

service.default.grouplist为seata集群地址集

建立集群数据库 

https://github.com/seata/seata/blob/v0.9.0/server/src/main/resources/db_store.sql

注意版本对应分支,官方链接指向了develop分支sql不同是会报错的

-- 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`)
);

store.db.datasource的选择 ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.

store.db.driver-class-name = "com.mysql.jdbc.Driver"   store.db.driver-class-name = "com.mysql.cj.jdbc.Driver"

选择druid和com.mysql.jdbc.Driver(目前seata驱动为5.1.30)

导入配置到nacos。

格式为sh nacos-config.sh $Nacos-Server-IP

因为nacos-config.sh脚本中已经把8848端口写死,如果你的nacos-server不是8848端口,请修改nacos-config.sh。

cd /opt/seata-server/conf/bash /opt/seata-server/conf/nacos-config.sh nacosserver

nacosserver是nacos的ip

脚本执行最后输出 "init nacos config finished, please start seata-server." 说明推送配置成功。若想进一步确认可登陆Nacos控制台->配置列表->筛选查询Group为SEATA_GROUP的配置项。

将/opt/seata-server/conf/nacos-config.txt 脚本修改后重新导入即可。

三、启动seata-server(事务协调器)

修改日志目录(非必须)

vim /opt/seata-server/conf/logback.xml

改为

<!--<property name="LOG_HOME" value="${user.home}/logs/seata"/>-->
    <property name="LOG_HOME" value="/var/log/seata-server"/>

sudo mkdir -p /var/log/seata-server;sudo chmod -R 777 /var/log/seata-server

配置修改

vim /opt/seata-server/conf/registry.conf 并复制到java代码的properties目录下

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    serverAddr = "nacosserver"
    namespace = "public"
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = "0"
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  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 {
    serverAddr = "nacosserver"
    namespace = ""
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    app.id = "seata-server"
    apollo.meta = "http://192.168.1.204:8801"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

注:serverAddr不要填端口号   public为小写

有处BUG:config.nacos.namespace="public"的值要去掉public写成""

启动格式sh seata-server.sh $LISTEN_PORT $PATH_FOR_PERSISTENT_DATA $IP(此参数可选)

$IP参数 用于多IP环境下指定 Seata-Server 注册服务的IP    虽然是可选,但还是要填,之前我偷懒没填,一大堆虚拟ip各种乱定位。

命令启动

sh /opt/seata-server/bin/seata-server.sh -p 8091 -h 192.168.0.101 -m db -n 1

守护进程启动

vim /opt/seata-server/startup.sh

填入

#!/bin/bash
sh /opt/seata-server/bin/seata-server.sh -p 8091 -h 192.168.0.101 -m db -n 1

vim /lib/systemd/system/seata-server.service

文件中填入

[Unit]
Description=seata-serverAfter=syslog.target network.target remote-fs.target nss-lookup.target[Service]Type=simpleExecStart=/opt/seata-server/startup.shRestart=alwaysPrivateTmp=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的条目

查看日志
sudo journalctl -u seata-server
sudo journalctl -u seata-server --since="2019-11-11 11:11:11"
sudo journalctl -u seata-server --since "30 min ago"
sudo journalctl -u seata-server --since yesterday
sudo journalctl -u seata-server --since "2019-11-10" --until "2018-11-11 11:11"
sudo journalctl -u seata-server --since 04:00 --until "2 hour ago"

四、创建数据表

(创建mysql数据库略)

UNDO_LOG table is required by SEATA AT mode.

-- for AT mode you must to init this sql for you business databese. the seata server not need it.
drop table `undo_log`;
CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
  `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 NOT NULL COMMENT 'create datetime',
  `log_modified` datetime NOT NULL COMMENT 'modify datetime',
  `ext` varchar(100) DEFAULT NULL COMMENT 'reserved field',
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='AT transaction mode undo table';

每个业务数据库都要建一个undo_log表

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
3年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这