Centos7下Seata的安装和配置
  D49yXDf6ruYT 2023年11月02日 31 0


Seata是由阿里中间件团队发起的开源项目 Fescar,后更名为Seata,它是一个是开源的分布式事务框架。

传统2PC的问题在Seata中得到了解决,它通过对本地关系数据库的分支事务的协调来驱动完成全局事务,是工作在应用层的中间件。主要优点是性能较好,且不长时间占用连接资源,它以高效并且对业务0侵入的方式解决微服务场景下面临的分布式事务问题,它目前提供AT模式(即2PC)及TCC模式的分布式事务解决方案。
我们这里是在Centos7上进行的操作;

下载:

去 https://github.com/seata/seata/releases 找最新的版本:

Centos7下Seata的安装和配置_redis


执行下载:

wget  https://github.com/seata/seata/releases/download/v1.4.2/seata-server-1.4.2.tar.gz

部署:

注意需要安装和配置Java环境

单机版部署

Seata单机版部署非常简单,直接解压启动即可

tar -zxvf seata-server-1.4.2.tar.gz
cd seata
sh ./bin/seata-server.sh

解压后有4个文件: bin conf lib LICENSE

Centos7下Seata的安装和配置_seata_02


启动成功后控制台会显示相关信息。默认端口是8901,使用文件来记录日志。

(注意关闭防火墙)

Centos7下Seata的安装和配置_mysql_03

Seata的启动命令参数说明

Centos7下Seata的安装和配置_全局事务_04

因此单机版部署使用的完成命令如下:

cd 到bin下面

nohup sh seata-server.sh -p 8091 -h 192.168.8.150  -m file &> seata.log &

高可用集群版本部署

Seata的高可用依赖于注册中心、配置中心和数据库来实现;因此我们需要修改相关的配置。下面我们以nacos和MySQL为例子,Seata-Server使用注册中心进行集群间的通信,同时将事务数据保存到数据库中让整个集群统一使用事务信息。

修改配置文件 registry.conf

将里面的registry节点下的type值改为nacos;然后修改nacos节点中的地址;下面是修改后的示例:

registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server"
serverAddr = "192.168.8.150:8848"
namespace = "65becc9f-e858-4b7f-8b3d-c58ecc1394a7"
group = "SEATA_GROUP"
cluster = "default"
}
}

config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.8.150:8848"
namespace = "65becc9f-e858-4b7f-8b3d-c58ecc1394a7"
group = "SEATA_GROUP"
}

}

修改配置文件 file.conf

transport {
# tcp udt unix-domain-socket
type = "TCP"
#NIO NATIVE
server = "NIO"
#enable heartbeat
heartbeat = true
#thread factory for netty
thread-factory {
boss-thread-prefix = "NettyBoss"
worker-thread-prefix = "NettyServerNIOWorker"
server-executor-thread-prefix = "NettyServerBizHandler"
share-boss-worker = false
client-selector-thread-prefix = "NettyClientSelector"
client-selector-thread-size = 1
client-worker-thread-prefix = "NettyClientWorkerThread"
# netty boss thread size,will not be used for UDT
boss-thread-size = 1
#auto default pin or 8
worker-thread-size = 8
}
shutdown {
# when destroy server, wait seconds
wait = 3
}
serialization = "seata"
compressor = "none"
}
service {
#vgroup->rgroup
vgroup_mapping.pms-server-fescar-service-group = "default" # pms-server需要修改为你的微服务的spring.application.name,后面的-fescar-service-group保留
#only support single node
default.grouplist = "192.168.8.144:8091" # 修改为你的seata的地址
#degrade current not support
enableDegrade = false
#disable
disable = false
#unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
max.commit.retry.timeout = "-1"
max.rollback.retry.timeout = "-1"
}

client {
async.commit.buffer.limit = 10000
lock {
retry.internal = 10
retry.times = 30
}
report.retry.count = 5
tm.commit.retry.count = 1
tm.rollback.retry.count = 1
}

## transaction log store
store {
## store mode: file、db
mode = "file"

## file store
file {
dir = "sessionStore"

# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
max-branch-session-size = 16384
# globe session size , if exceeded throws exceptions
max-global-session-size = 512
# file buffer size , if exceeded allocate new buffer
file-write-buffer-cache-size = 16384
# when recover batch read size
session.reload.read_size = 100
# async, sync
flush-disk-mode = async
}

## database store
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "dbcp"
## mysql/oracle/h2/oceanbase etc.
db-type = "mysql"
driver-class-name = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://192.168.8.144:3306/seata"
user = "root"
password = "root"
min-conn = 1
max-conn = 3
global.table = "global_table"
branch.table = "branch_table"
lock-table = "lock_table"
query-limit = 100
}
}
lock {
## the lock store mode: local、remote
mode = "remote"

local {
## store locks in user's database
}

remote {
## store locks in the seata's server
}
}
recovery {
#schedule committing retry period in milliseconds
committing-retry-period = 1000
#schedule asyn committing retry period in milliseconds
asyn-committing-retry-period = 1000
#schedule rollbacking retry period in milliseconds
rollbacking-retry-period = 1000
#schedule timeout retry period in milliseconds
timeout-retry-period = 1000
}

transaction {
undo.data.validation = true
undo.log.serialization = "jackson"
undo.log.save.days = 7
#schedule delete expired undo_log in milliseconds
undo.log.delete.period = 86400000
undo.log.table = "undo_log"
}

## metrics settings
metrics {
enabled = false
registry-type = "compact"
# multi exporters use comma divided
exporter-list = "prometheus"
exporter-prometheus-port = 9898
}

support {
## spring
spring {
# auto proxy the DataSource bean
datasource.autoproxy = false
}
}

Server端存储模式(store.mode)现有file、db、redis三种(后续将引入raft,mongodb),file模式无需改动,直接启动即可,下面专门讲下db和redis启动步骤。

注: file模式为单机模式,全局事务会话信息内存中读写并持久化本地文件root.data,性能较高;

db模式为高可用模式,全局事务会话信息通过db共享,相应性能差些;(mysql 5.7+)

redis模式Seata-Server 1.3及以上版本支持,性能较高,存在事务信息丢失风险,请提前配置合适当前场景的redis持久化配置.

Centos7下Seata的安装和配置_全局事务_05

Centos7下Seata的安装和配置_全局事务_06

创建相关表和数据库

全局事务会话信息由3块内容构成,全局事务–>分支事务–>全局锁,对应表global_table、branch_table、lock_table

需要新建一个名为seata数据库(名称可以是其他的,只要上面的配置能够对应上即可),然后创建global_table、branch_table、lock_table三张表;建表语句;

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

Centos7下Seata的安装和配置_redis_07

启动seata服务:

启动seata服务

修改 seata-server 解压目录下 conf 中的 file.conf 文件。该配置文件用于指定 seata server

存放日志的位置。

注意:mysql默认支持mysql5,最好使用5.7以上的版本,有些seata版本不支持5.7以下的mysql版本。

Centos7下Seata的安装和配置_redis_08


Centos7下Seata的安装和配置_mysql_09

查看启动日志:

tail -1000f seata.log

Centos7下Seata的安装和配置_全局事务_10

当我们看到-Server started时并未发现其他错误信息,我们的seata server已经启动成功。

现在到nacos的控制台中的服务管理里面就可以看到seata注册的服务信息了。

Centos7下Seata的安装和配置_seata_11


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
D49yXDf6ruYT