版本:教程所用Seata版本:1.4.2

1. 下载

这里我们下载tar.gz版本,下载地址:https://github.com/seata/seata/releases

image.png

2. 上传并解压

上传到CentOS服务器,执行解压命令
tar -zxvf seata-server-1.4.2.tar.gz

3. 创建数据库

  1. -- global_table的表结构
  2. CREATE TABLE `global_table` (
  3. `xid` varchar(128) NOT NULL,
  4. `transaction_id` bigint(20) DEFAULT NULL,
  5. `status` tinyint(4) NOT NULL,
  6. `application_id` varchar(64) DEFAULT NULL,
  7. `transaction_service_group` varchar(64) DEFAULT NULL,
  8. `transaction_name` varchar(64) DEFAULT NULL,
  9. `timeout` int(11) DEFAULT NULL,
  10. `begin_time` bigint(20) DEFAULT NULL,
  11. `application_data` varchar(2000) DEFAULT NULL,
  12. `gmt_create` datetime DEFAULT NULL,
  13. `gmt_modified` datetime DEFAULT NULL,
  14. PRIMARY KEY (`xid`),
  15. KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
  16. KEY `idx_transaction_id` (`transaction_id`)
  17. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  18. -- branch_table的表结构
  19. CREATE TABLE `branch_table` (
  20. `branch_id` bigint(20) NOT NULL,
  21. `xid` varchar(128) NOT NULL,
  22. `transaction_id` bigint(20) DEFAULT NULL,
  23. `resource_group_id` varchar(32) DEFAULT NULL,
  24. `resource_id` varchar(256) DEFAULT NULL,
  25. `lock_key` varchar(128) DEFAULT NULL,
  26. `branch_type` varchar(8) DEFAULT NULL,
  27. `status` tinyint(4) DEFAULT NULL,
  28. `client_id` varchar(64) DEFAULT NULL,
  29. `application_data` varchar(2000) DEFAULT NULL,
  30. `gmt_create` datetime DEFAULT NULL,
  31. `gmt_modified` datetime DEFAULT NULL,
  32. PRIMARY KEY (`branch_id`),
  33. KEY `idx_xid` (`xid`)
  34. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  35. -- lock_table的表结构
  36. create table `lock_table` (
  37. `row_key` varchar(128) not null,
  38. `xid` varchar(96),
  39. `transaction_id` long ,
  40. `branch_id` long,
  41. `resource_id` varchar(256) ,
  42. `table_name` varchar(32) ,
  43. `pk` varchar(32) ,
  44. `gmt_create` datetime ,
  45. `gmt_modified` datetime,
  46. primary key(`row_key`)
  47. );

4. 修改配置文件

4.1 修改 conf/registry.conf 配置

目前seata支持如下的file、nacos 、apollo、zk、consul的注册中心和配置中心。这里我们以nacos 为例。 将 type 改为 nacos。
image.png

  • serverAddr = “ip:8848” :nacos 的地址
  • namespace = “” :nacos的命名空间默认为``
  • cluster = “default” :集群设置未默认 default

    4.2 修改 conf/file.conf 配置

    修改file.conf
    image.png

    5.将Seata配置信息导入Nacos

    5.1 方法一:通过命令导入

    新建nacos-config.txt
    image.png
    1. transport.type=TCP
    2. transport.server=NIO
    3. transport.heartbeat=true
    4. transport.thread-factory.boss-thread-prefix=NettyBoss
    5. transport.thread-factory.worker-thread-prefix=NettyServerNIOWorker
    6. transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandler
    7. transport.thread-factory.share-boss-worker=false
    8. transport.thread-factory.client-selector-thread-prefix=NettyClientSelector
    9. transport.thread-factory.client-selector-thread-size=1
    10. transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThread
    11. transport.thread-factory.boss-thread-size=1
    12. transport.thread-factory.worker-thread-size=8
    13. transport.shutdown.wait=3
    14. service.vgroup_mapping.order-service-seata-service-group=default
    15. service.vgroup_mapping.account-service-seata-service-group=default
    16. service.vgroup_mapping.storage-service-seata-service-group=default
    17. service.vgroup_mapping.business-service-seata-service-group=default
    18. service.enableDegrade=false
    19. service.disable=false
    20. service.max.commit.retry.timeout=-1
    21. service.max.rollback.retry.timeout=-1
    22. client.async.commit.buffer.limit=10000
    23. client.lock.retry.internal=10
    24. client.lock.retry.times=30
    25. store.mode=db
    26. store.file.dir=file_store/data
    27. store.file.max-branch-session-size=16384
    28. store.file.max-global-session-size=512
    29. store.file.file-write-buffer-cache-size=16384
    30. store.file.flush-disk-mode=async
    31. store.file.session.reload.read_size=100
    32. store.db.driver-class-name=com.mysql.jdbc.Driver
    33. store.db.datasource=dbcp
    34. store.db.db-type=mysql
    35. store.db.url=jdbc:mysql://192.168.10.200:3306/seata?useUnicode=true
    36. store.db.user=lidong
    37. store.db.password=cwj887766@@
    38. store.db.min-conn=1
    39. store.db.max-conn=3
    40. store.db.global.table=global_table
    41. store.db.branch.table=branch_table
    42. store.db.query-limit=100
    43. store.db.lock-table=lock_table
    44. recovery.committing-retry-period=1000
    45. recovery.asyn-committing-retry-period=1000
    46. recovery.rollbacking-retry-period=1000
    47. recovery.timeout-retry-period=1000
    48. transaction.undo.data.validation=true
    49. transaction.undo.log.serialization=jackson
    50. transaction.undo.log.save.days=7
    51. transaction.undo.log.delete.period=86400000
    52. transaction.undo.log.table=undo_log
    53. transport.serialization=seata
    54. transport.compressor=none
    55. metrics.enabled=false
    56. metrics.registry-type=compact
    57. metrics.exporter-list=prometheus
    58. metrics.exporter-prometheus-port=9898
    59. client.report.retry.count=5
    60. service.disableGlobalTransaction=false
    61. client.support.spring.datasource.autoproxy=true

    数据项介绍:

    • store.mode :存储模式 默认file 这里我修改为db 模式 ,并且需要三个表global_table、branch_table和lock_table
    • store.db.driver-class-name: 0.8.0版本默认没有,会报错。添加了 com.mysql.jdbc.Driver
    • store.db.datasource=dbcp :数据源 dbcp
    • store.db.db-type=mysql : 存储数据库的类型为mysql
    • store.db.url=jdbc:mysql://192.168.10.200:3306/seata?useUnicode=true : 修改为自己的数据库url、port、数据库名称
    • store.db.user=lidong :数据库的账号
    • store.db.password=cwj887766@@ :数据库的密码
    • service.vgroup_mapping.order-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.business-service-seata-service-group=default
    • client.support.spring.datasource.autoproxy=true 开启数据源自动代理

新建nacos-config.sh
image.png

  1. while getopts ":h:p:g:t:u:w:" opt
  2. do
  3. case $opt in
  4. h)
  5. host=$OPTARG
  6. ;;
  7. p)
  8. port=$OPTARG
  9. ;;
  10. g)
  11. group=$OPTARG
  12. ;;
  13. t)
  14. tenant=$OPTARG
  15. ;;
  16. u)
  17. username=$OPTARG
  18. ;;
  19. w)
  20. password=$OPTARG
  21. ;;
  22. ?)
  23. echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
  24. exit 1
  25. ;;
  26. esac
  27. done
  28. if [[ -z ${host} ]]; then
  29. host=localhost
  30. fi
  31. if [[ -z ${port} ]]; then
  32. port=8848
  33. fi
  34. if [[ -z ${group} ]]; then
  35. group="SEATA_GROUP"
  36. fi
  37. if [[ -z ${tenant} ]]; then
  38. tenant=""
  39. fi
  40. if [[ -z ${username} ]]; then
  41. username=""
  42. fi
  43. if [[ -z ${password} ]]; then
  44. password=""
  45. fi
  46. nacosAddr=$host:$port
  47. contentType="content-type:application/json;charset=UTF-8"
  48. echo "set nacosAddr=$nacosAddr"
  49. echo "set group=$group"
  50. failCount=0
  51. tempLog=$(mktemp -u)
  52. function addConfig() {
  53. curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$1&group=$group&content=$2&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
  54. if [[ -z $(cat "${tempLog}") ]]; then
  55. echo " Please check the cluster status. "
  56. exit 1
  57. fi
  58. if [[ $(cat "${tempLog}") =~ "true" ]]; then
  59. echo "Set $1=$2 successfully "
  60. else
  61. echo "Set $1=$2 failure "
  62. (( failCount++ ))
  63. fi
  64. }
  65. count=0
  66. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  67. (( count++ ))
  68. key=${line%%=*}
  69. value=${line#*=}
  70. addConfig "${key}" "${value}"
  71. done
  72. echo "========================================================================="
  73. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  74. echo "========================================================================="
  75. if [[ ${failCount} -eq 0 ]]; then
  76. echo " Init nacos config finished, please start seata-server. "
  77. else
  78. echo " init nacos config fail. "
  79. fi

执行
sh nacos-config.sh -h 47.104.146.31 -p 8848 -g SEATA_GROUP
查看nacos界面,配置项已经导入
image.png

5.2 方法二: Nacos界面导入

image.png
image.png

6.启动 Seata Server

使用db 模式启动
nohup ./seata-server.sh -h 127.0.0.1 -p 8091 >log.out 2>1 &
这时候在 Nacos 的服务列表下面可以看到一个名为serverAddr的服务 image.png