一、下载Seata

本示例使用的是seata-server-1.4.0
http://seata.io/zh-cn/blog/download.html

二、配置文件设置

1、file.conf

需要修改存储类型,mysql的账号密码
image.png

  1. ## transaction log store, only used in seata-server
  2. store {
  3. ## store mode: file、db、redis
  4. mode = "db"
  5. ## file store property
  6. file {
  7. ## store location dir
  8. dir = "sessionStore"
  9. # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
  10. maxBranchSessionSize = 16384
  11. # globe session size , if exceeded throws exceptions
  12. maxGlobalSessionSize = 512
  13. # file buffer size , if exceeded allocate new buffer
  14. fileWriteBufferCacheSize = 16384
  15. # when recover batch read size
  16. sessionReloadReadSize = 100
  17. # async, sync
  18. flushDiskMode = async
  19. }
  20. ## database store property
  21. db {
  22. ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
  23. datasource = "druid"
  24. ## mysql/oracle/postgresql/h2/oceanbase etc.
  25. dbType = "mysql"
  26. driverClassName = "com.mysql.jdbc.Driver"
  27. url = "jdbc:mysql://127.0.0.1:3306/seata"
  28. user = "root"
  29. password = "Abc123!_"
  30. minConn = 5
  31. maxConn = 100
  32. globalTable = "global_table"
  33. branchTable = "branch_table"
  34. lockTable = "lock_table"
  35. queryLimit = 100
  36. maxWait = 5000
  37. }
  38. ## redis store property
  39. redis {
  40. host = "127.0.0.1"
  41. port = "6379"
  42. password = ""
  43. database = "0"
  44. minConn = 1
  45. maxConn = 10
  46. maxTotal = 100
  47. queryLimit = 100
  48. }
  49. }

2、DB模式执行SQL

在选择 db 方式后,需要在对应数据库创建 globalTable(持久化全局事务)、branchTable(持久化各提交分支的事务)、 lockTable(持久化各分支锁定资源事务)三张表。

  1. -- the table to store GlobalSession data
  2. -- 持久化全局事务
  3. CREATE TABLE IF NOT EXISTS `global_table`
  4. (
  5. `xid` VARCHAR(128) NOT NULL,
  6. `transaction_id` BIGINT,
  7. `status` TINYINT NOT NULL,
  8. `application_id` VARCHAR(32),
  9. `transaction_service_group` VARCHAR(32),
  10. `transaction_name` VARCHAR(128),
  11. `timeout` INT,
  12. `begin_time` BIGINT,
  13. `application_data` VARCHAR(2000),
  14. `gmt_create` DATETIME,
  15. `gmt_modified` DATETIME,
  16. PRIMARY KEY (`xid`),
  17. KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
  18. KEY `idx_transaction_id` (`transaction_id`)
  19. ) ENGINE = InnoDB
  20. DEFAULT CHARSET = utf8;
  21. -- the table to store BranchSession data
  22. -- 持久化各提交分支的事务
  23. CREATE TABLE IF NOT EXISTS `branch_table`
  24. (
  25. `branch_id` BIGINT NOT NULL,
  26. `xid` VARCHAR(128) NOT NULL,
  27. `transaction_id` BIGINT,
  28. `resource_group_id` VARCHAR(32),
  29. `resource_id` VARCHAR(256),
  30. `branch_type` VARCHAR(8),
  31. `status` TINYINT,
  32. `client_id` VARCHAR(64),
  33. `application_data` VARCHAR(2000),
  34. `gmt_create` DATETIME(6),
  35. `gmt_modified` DATETIME(6),
  36. PRIMARY KEY (`branch_id`),
  37. KEY `idx_xid` (`xid`)
  38. ) ENGINE = InnoDB
  39. DEFAULT CHARSET = utf8;
  40. -- the table to store lock data
  41. -- 持久化每个分支锁表事务
  42. CREATE TABLE IF NOT EXISTS `lock_table`
  43. (
  44. `row_key` VARCHAR(128) NOT NULL,
  45. `xid` VARCHAR(96),
  46. `transaction_id` BIGINT,
  47. `branch_id` BIGINT NOT NULL,
  48. `resource_id` VARCHAR(256),
  49. `table_name` VARCHAR(32),
  50. `pk` VARCHAR(36),
  51. `gmt_create` DATETIME,
  52. `gmt_modified` DATETIME,
  53. PRIMARY KEY (`row_key`),
  54. KEY `idx_branch_id` (`branch_id`)
  55. ) ENGINE = InnoDB
  56. DEFAULT CHARSET = utf8;

3、registry.conf

registry.conf 文件设置注册中心和配置中心:
目前注册中心支持 nacos 、eureka、redis、zk、consul、etcd3、sofa 七种,这里我使用的 eureka作为注册中心 ; 配置中心支持 nacos 、apollo、zk、consul、etcd3 五种方式。
这里注册中心和配置中心都选择nacos:

  1. registry {
  2. # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  3. type = "nacos"
  4. loadBalance = "RandomLoadBalance"
  5. loadBalanceVirtualNodes = 10
  6. nacos {
  7. application = "seata-server"
  8. serverAddr = "127.0.0.1:8848"
  9. group = "SEATA_GROUP"
  10. namespace = ""
  11. cluster = "default"
  12. username = ""
  13. password = ""
  14. }
  15. eureka {
  16. serviceUrl = "http://localhost:8761/eureka"
  17. application = "default"
  18. weight = "1"
  19. }
  20. redis {
  21. serverAddr = "localhost:6379"
  22. db = 0
  23. password = ""
  24. cluster = "default"
  25. timeout = 0
  26. }
  27. zk {
  28. cluster = "default"
  29. serverAddr = "127.0.0.1:2181"
  30. sessionTimeout = 6000
  31. connectTimeout = 2000
  32. username = ""
  33. password = ""
  34. }
  35. consul {
  36. cluster = "default"
  37. serverAddr = "127.0.0.1:8500"
  38. }
  39. etcd3 {
  40. cluster = "default"
  41. serverAddr = "http://localhost:2379"
  42. }
  43. sofa {
  44. serverAddr = "127.0.0.1:9603"
  45. application = "default"
  46. region = "DEFAULT_ZONE"
  47. datacenter = "DefaultDataCenter"
  48. cluster = "default"
  49. group = "SEATA_GROUP"
  50. addressWaitTime = "3000"
  51. }
  52. file {
  53. name = "file.conf"
  54. }
  55. }
  56. config {
  57. # file、nacos 、apollo、zk、consul、etcd3
  58. type = "nacos"
  59. nacos {
  60. serverAddr = "127.0.0.1:8848"
  61. namespace = ""
  62. group = "SEATA_GROUP"
  63. username = ""
  64. password = ""
  65. }
  66. consul {
  67. serverAddr = "127.0.0.1:8500"
  68. }
  69. apollo {
  70. appId = "seata-server"
  71. apolloMeta = "http://192.168.1.204:8801"
  72. namespace = "application"
  73. apolloAccesskeySecret = ""
  74. }
  75. zk {
  76. serverAddr = "127.0.0.1:2181"
  77. sessionTimeout = 6000
  78. connectTimeout = 2000
  79. username = ""
  80. password = ""
  81. }
  82. etcd3 {
  83. serverAddr = "http://localhost:2379"
  84. }
  85. file {
  86. name = "file.conf"
  87. }
  88. }

4、配置到nacos的配置

在根目录下生成该config.txt:

  1. #service.default.grouplist=127.0.0.1:8091
  2. #service.enableDegrade=false
  3. service.disableGlobalTransaction=false
  4. store.mode=db
  5. store.db.datasource=druid
  6. store.db.dbType=mysql
  7. store.db.driverClassName=com.mysql.cj.jdbc.Driver
  8. store.db.url=jdbc:mysql://127.0.0.1:3306/nacos_config_seata_manager
  9. store.db.user=root
  10. store.db.password=123456
  11. store.db.minConn=3
  12. store.db.maxConn=30
  13. store.db.globalTable=global_table
  14. store.db.branchTable=branch_table
  15. store.db.queryLimit=100
  16. store.db.lockTable=lock_table

在根目录下生成该nacos-config.sh:

  1. #!/bin/sh
  2. # Copyright 1999-2019 Seata.io Group.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at、
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. while getopts ":h:p:g:t:u:w:" opt
  16. do
  17. case $opt in
  18. h)
  19. host=$OPTARG
  20. ;;
  21. p)
  22. port=$OPTARG
  23. ;;
  24. g)
  25. group=$OPTARG
  26. ;;
  27. t)
  28. tenant=$OPTARG
  29. ;;
  30. u)
  31. username=$OPTARG
  32. ;;
  33. w)
  34. password=$OPTARG
  35. ;;
  36. ?)
  37. echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
  38. exit 1
  39. ;;
  40. esac
  41. done
  42. if [ -z ${host} ]; then
  43. host=localhost
  44. fi
  45. if [ -z ${port} ]; then
  46. port=8848
  47. fi
  48. if [ -z ${group} ]; then
  49. group="SEATA_GROUP"
  50. fi
  51. if [ -z ${tenant} ]; then
  52. tenant=""
  53. fi
  54. if [ -z ${username} ]; then
  55. username=""
  56. fi
  57. if [ -z ${password} ]; then
  58. password=""
  59. fi
  60. nacosAddr=$host:$port
  61. contentType="content-type:application/json;charset=UTF-8"
  62. echo "set nacosAddr=$nacosAddr"
  63. echo "set group=$group"
  64. urlencode() {
  65. length="${#1}"
  66. i=0
  67. while [ $length -gt $i ]; do
  68. char="${1:$i:1}"
  69. case $char in
  70. [a-zA-Z0-9.~_-]) printf $char ;;
  71. *) printf '%%%02X' "'$char" ;;
  72. esac
  73. i=`expr $i + 1`
  74. done
  75. }
  76. failCount=0
  77. tempLog=$(mktemp -u)
  78. function addConfig() {
  79. dataId=`urlencode $1`
  80. content=`urlencode $2`
  81. curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$dataId&group=$group&content=$content&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
  82. if [ -z $(cat "${tempLog}") ]; then
  83. echo " Please check the cluster status. "
  84. exit 1
  85. fi
  86. if [ "$(cat "${tempLog}")" == "true" ]; then
  87. echo "Set $1=$2 successfully "
  88. else
  89. echo "Set $1=$2 failure "
  90. failCount=`expr $failCount + 1`
  91. fi
  92. }
  93. count=0
  94. COMMENT_START="#"
  95. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  96. if [[ "$line" =~ ^"${COMMENT_START}".* ]]; then
  97. continue
  98. fi
  99. count=`expr $count + 1`
  100. key=${line%%=*}
  101. value=${line#*=}
  102. addConfig "${key}" "${value}"
  103. done
  104. echo "========================================================================="
  105. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  106. echo "========================================================================="
  107. if [ ${failCount} -eq 0 ]; then
  108. echo " Init nacos config finished, please start seata-server. "
  109. else
  110. echo " init nacos config fail. "
  111. fi

给nacos-config.sh新增执行权限,然后执行该文件,将config.txt中的配置都导入到nacos中。