Seata Server服务端搭建

一、官网地址

Seata 文档

Seata github

Seata releases

二、Seata Server 下载

这里地址为 1.3.0版本

seata-server-1.3.0

  • Linux
wget https://github.com/seata/seata/releases/download/v1.3.0/seata-server-1.3.0.zip

三、批改配置文件

配置文件次要有:registry.conf file.conf

  • registry.conf : Seata 服务注册核心地址,配置核心地址
  • file.conf : 当配置核心应用 file时,此配置文件失效。配置store存储配置

    这里咱们应用eureka做为注册核心,Apollo做为配置核心

registry.conf

registry {  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa  type = "eureka"  # 配置eureka地址  eureka {    serviceUrl = "http://10.0.17.92:9001/eureka"    application = "seata-server"    weight = "1"  }  ...  file {    name = "file.conf"  }}config {  # file、nacos 、apollo、zk、consul、etcd3  #type = "file"  type = "apollo"  # 配置核心配置apollo地址和namespace  apollo {    appId = "seata-server"    apolloMeta = "http://10.0.17.92:9083"    namespace = "application"  }  file {    name = "file.conf"  }}

file.conf

## transaction log store, only used in seata-serverstore {  ## store mode: file、db、redis  mode = "db"  ## file store property  file {    ## store location dir    dir = "sessionStore"    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions    maxBranchSessionSize = 16384    # globe session size , if exceeded throws exceptions    maxGlobalSessionSize = 512    # file buffer size , if exceeded allocate new buffer    fileWriteBufferCacheSize = 16384    # when recover batch read size    sessionReloadReadSize = 100    # async, sync    flushDiskMode = async  }  ## 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://10.0.17.122:3306/seata"    user = "root"    password = "Pdhn^456"    minConn = 5    maxConn = 30    globalTable = "global_table"    branchTable = "branch_table"    lockTable = "lock_table"    queryLimit = 100    maxWait = 5000  }  ## redis store property  redis {    host = "127.0.0.1"    port = "6379"    password = ""    database = "0"    minConn = 1    maxConn = 10    queryLimit = 100  }}

Apollo配置Server配置

次要批改:数据库地址 用户名 明码等

  • 留神:mysql 版本问题 1.3.0后,Seata应用mysql 8.0版本
  • 应用 8.0版本后,driverClassName = com.mysql.cj.jdbc.Driver 。否则会报错:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server
transport.type = TCPtransport.server = NIOtransport.heartbeat = truetransport.enableClientBatchSendRequest = falsetransport.threadFactory.bossThreadPrefix = NettyBosstransport.threadFactory.workerThreadPrefix = NettyServerNIOWorkertransport.threadFactory.serverExecutorThreadPrefix = NettyServerBizHandlertransport.threadFactory.shareBossWorker = falsetransport.threadFactory.clientSelectorThreadPrefix = NettyClientSelectortransport.threadFactory.clientSelectorThreadSize = 1transport.threadFactory.clientWorkerThreadPrefix = NettyClientWorkerThreadtransport.threadFactory.bossThreadSize = 1transport.threadFactory.workerThreadSize = defaulttransport.shutdown.wait = 3transport.serialization = seatatransport.compressor = nonestore.mode = dbstore.db.datasource = druidstore.db.dbType = mysqlstore.db.driverClassName = com.mysql.cj.jdbc.Driverstore.db.url = jdbc:mysql://10.0.17.122:3306/seata?useUnicode=truestore.db.user = rootstore.db.password = Pdhn^456store.db.minConn = 5store.db.maxConn = 30store.db.globalTable = global_tablestore.db.branchTable = branch_tablestore.db.queryLimit = 100store.db.lockTable = lock_tablestore.db.maxWait = 5000server.recovery.committingRetryPeriod = 1000server.recovery.asynCommittingRetryPeriod = 1000server.recovery.rollbackingRetryPeriod = 1000server.recovery.timeoutRetryPeriod = 1000server.maxCommitRetryTimeout = -1server.maxRollbackRetryTimeout = -1server.rollbackRetryTimeoutUnlockEnable = falsemetrics.enabled = falsemetrics.registryType = compact

初始化数据库

  • global_table 保护全局事务
  • branch_table 保护分支事务
  • lock_table 保护全局锁
-- the table to store GlobalSession datadrop 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 datadrop 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 datadrop 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`));

四、Seata-Server启动

  • 先保障配置eureka apollo配置,启动注册核心和配置核心
  • Apollo 配置

  • 进入到: seata/bin目录
nohup sh seata-server.sh -h 10.0.17.92 -p 8091 &
  • Eureka

五、Docker启动 Seata server

1. 拉取 seata-server镜像

docker pull docker.io/seataio/seata-server:1.3.0

2. 批改配置文件registry.conf

  • 配置 Apollo
registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "eureka" loadBalance = "RandomLoadBalance" loadBalanceVirtualNodes = 10 eureka {   serviceUrl = "http://10.0.17.92:9001/eureka"   application = "seata-server"   weight = "1" } file {   name = "file.conf" }}config { # file、nacos 、apollo、zk、consul、etcd3 type = "apollo" apollo {   appId = "seata-server"   apolloMeta = "http://10.0.17.92:9083"   namespace = "application" } file {   name = "file.conf" }}

3. 启动脚本

start.sh

SEATA_PORT : 自定义端口

SERVER_NODE: 集群环境下,指定节点

seata日志目录:/root/logs/seata ,可挂载到服务器本地目录

#!/bin/bash docker stop seata-server;docker container rm seata-server; docker run --name seata-server -it -d  -p 8091:8091 \-e SEATA_CONFIG_NAME=file:/root/seata/config/registry \-e SEATA_IP=10.0.17.92 \-e SEATA_PORT=8091 \-e SERVER_NODE=1 \-v /opt/logs/seata:/root/logs/seata \-v /opt/seata/config:/root/seata/config \- v /opt/seata/config/libs/mysql-connector-java-8.0.21.jar:/root/seata/config/libs/mysql-connector-java-8.0.21.jar \--net=bridge --restart=always docker.io/seataio/seata-server:1.3.0

六、 Docker Compose 启动 Seata server

  • 留神数据库版本问题,1.3.0版本后 mysql驱动为 8.0版本

- 在config目录中,配置mysql-connector-java-8.0.21.jar驱动

  • Apollo数据库驱动 cj,连贯地址加 characterEncoding=utf-8&serverTimezone=UTC
store.db.driverClassName = com.mysql.cj.jdbc.Driverstore.db.url = jdbc:mysql://10.0.17.122:3306/seata?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
  • 启动容器
docker-compose up -d 

1. docker-compose.yml配置文件

version: "3"services:  seata-server:    image: seataio/seata-server:1.3.0    container_name: seata-server    hostname: seata-server    ports:      - "8091:8091"    volumes:      - "/opt/seata/config:/root/seata/config"      - "/opt/seata/config/libs/mysql-connector-java-8.0.21.jar:/root/seata/config/libs/mysql-connector-java-8.0.21.jar"    environment:      - SEATA_CONFIG_NAME=file:/root/seata/config/registry      - SEATA_IP=10.0.17.92      - SEATA_PORT=8091       - STORE_MODE=db    restart: always    deploy:      resources:        limits:          cpus: '0.3'          memory: 300M  

2. registry.conf

registry {  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa  type = "eureka"  loadBalance = "RandomLoadBalance"  loadBalanceVirtualNodes = 10  eureka {    serviceUrl = "http://10.0.17.92:9001/eureka"    application = "seata-server"    weight = "1"  }  file {    name = "file.conf"  }}config {  # file、nacos 、apollo、zk、consul、etcd3  type = "apollo"  apollo {    appId = "seata-server"    apolloMeta = "http://10.0.17.92:9083"    namespace = "application"  }   file {    name = "file:/root/seata/config/file.conf"  }}

3. 异样 database

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server
15:09:54.010 ERROR --- [ionPool-Create-1072506992] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://10.0.17.122:3306/seata?useUnicode=true, errorCode 0, state 08001==>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)        at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)        at com.mysql.jdbc.Util.getInstance(Util.java:372)

4. 进入seata server 容器

[root@lipei92 seata]# docker ps|grep seata55af30b183b7        seataio/seata-server:1.4.1          "java -Djava.secur..."   4 minutes ago       Up 8 seconds          0.0.0.0:8091->8091/tcp                                             seata-server[root@lipei92 seata]# docker exec -it 55af30b183b7 sh/seata-server # pwd/seata-server

九、参考文档

  • Docker-compose次要参考

应用docker装置seata-server,mysql8引擎,nacos作为注册和配置核心

  • Docker 次要参考文档

docker疾速部署一个seata-server+向nacos推送配置

  • Seata Server 数据版本升级 8.0后报异样

Mysql驱动类导致抛异样