当前位置:   article > 正文

docker安装seata集群

docker安装seata集群

 

 

seata注册中心:nacos

seata配置中心:nacos(在配置中心中设置,store = db)

sql语句:

  1. -- -------------------------------- The script used when storeMode is 'db' --------------------------------
  2. -- the table to store GlobalSession data
  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. CREATE TABLE IF NOT EXISTS `branch_table`
  23. (
  24. `branch_id` BIGINT NOT NULL,
  25. `xid` VARCHAR(128) NOT NULL,
  26. `transaction_id` BIGINT,
  27. `resource_group_id` VARCHAR(32),
  28. `resource_id` VARCHAR(256),
  29. `branch_type` VARCHAR(8),
  30. `status` TINYINT,
  31. `client_id` VARCHAR(64),
  32. `application_data` VARCHAR(2000),
  33. `gmt_create` DATETIME(6),
  34. `gmt_modified` DATETIME(6),
  35. PRIMARY KEY (`branch_id`),
  36. KEY `idx_xid` (`xid`)
  37. ) ENGINE = InnoDB
  38. DEFAULT CHARSET = utf8;
  39. -- the table to store lock data
  40. CREATE TABLE IF NOT EXISTS `lock_table`
  41. (
  42. `row_key` VARCHAR(128) NOT NULL,
  43. `xid` VARCHAR(96),
  44. `transaction_id` BIGINT,
  45. `branch_id` BIGINT NOT NULL,
  46. `resource_id` VARCHAR(256),
  47. `table_name` VARCHAR(32),
  48. `pk` VARCHAR(36),
  49. `gmt_create` DATETIME,
  50. `gmt_modified` DATETIME,
  51. PRIMARY KEY (`row_key`),
  52. KEY `idx_branch_id` (`branch_id`)
  53. ) ENGINE = InnoDB
  54. DEFAULT CHARSET = utf8;
  1. CREATE TABLE IF NOT EXISTS `undo_log`
  2. (
  3. `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
  4. `branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
  5. `xid` VARCHAR(100) NOT NULL COMMENT 'global transaction id',
  6. `context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
  7. `rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
  8. `log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
  9. `log_created` DATETIME NOT NULL COMMENT 'create datetime',
  10. `log_modified` DATETIME NOT NULL COMMENT 'modify datetime',
  11. PRIMARY KEY (`id`),
  12. UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
  13. ) ENGINE = InnoDB
  14. AUTO_INCREMENT = 1
  15. DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';

 

版本信息:

  1. docker: 19.03.13
  2. nacos: 1.4.0
  3. seata: 1.4.0

 

步骤:

由于我们使用nacos作为配置中心,所以在nacos中创建存放配置信息的命名空间(也可以用默认的)

seata源码中的config.txt配置文件的内容上传到nacos去

配置文件地址:

https://github.com/seata/seata/blob/1.4.0/script/config-center/config.txt

下载后修改配置内容(也可以上传后在nacos里面去修改):

配置上传脚本地址:

https://github.com/seata/seata/blob/1.4.0/script/config-center/nacos/nacos-config.sh

下载后,上传(注意:nacos-config.sh 要在config.txt的一下层,不然找不到文件,或者自己去修改sh脚本)

 

在文件夹中 右键,选择 git bash here 

复制下面的上传命令执行:

  1. sh nacos-config.sh -h 192.168.1.201 -p 8850 -g SEATA_GROUP -t seata -u nacos -w nacos
  2. # 参数说明:
  3. -h:配置中心地址
  4. -p:配置中心端口
  5. -g:配置中心的分组
  6. -t:配置中心的命名空间id
  7. -u:配置中心naocs用户名
  8. -w:配置中心Naocs密码

执行成功:

下载seata源码

  1. # 创建操作目录
  2. mkdir -p /usr/local/work/docker_compose/redis-cluster
  3. cd /usr/local/work/docker_compose/redis-cluster
  4. # 下载
  5. wget https://github.com/seata/seata/releases/download/v1.4.2/seata-server-1.4.2.tar.gz
  6. # 解压
  7. tar -zxvf seata-server-1.4.2.tar.gz

复制解压后的seata目录的conf/file/conf 和 conf/registry/conf  到 /usr/local/work/docker_compose/redis-cluster/conf

修改registry.conf配置

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

修改file.conf配置文件(mode如果不改为db的话,在nacos配置中心里面去修改mode值也可以(未测试))

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

编辑docker-compose.yml文件

(版本一)

  1. version: "3"
  2. services:
  3. seata1:
  4. container_name: seata1
  5. image: seataio/seata-server:1.4.2
  6. ports:
  7. - "8091:8091"
  8. volumes:
  9. - /etc/localtime:/etc/localtime
  10. - /usr/local/work/docker_compose/seata-cluster/conf/registry.conf:/seata-server/resources/registry.conf
  11. - /usr/local/work/docker_compose/seata-cluster/8091/logs:/root/logs
  12. environment:
  13. SEATA_PORT: 8091
  14. seata2:
  15. container_name: seata2
  16. image: seataio/seata-server:1.4.2
  17. ports:
  18. - "8092:8091"
  19. volumes:
  20. - /etc/localtime:/etc/localtime
  21. - /usr/local/work/docker_compose/seata-cluster/conf/registry.conf:/seata-server/resources/registry.conf
  22. - /usr/local/work/docker_compose/seata-cluster/8092/logs:/root/logs
  23. environment:
  24. SEATA_PORT: 8091
  25. seata3:
  26. container_name: seata3
  27. image: seataio/seata-server:1.4.2
  28. ports:
  29. - "8093:8091"
  30. volumes:
  31. - /etc/localtime:/etc/localtime
  32. - /usr/local/work/docker_compose/seata-cluster/conf/registry.conf:/seata-server/resources/registry.conf
  33. - /usr/local/work/docker_compose/seata-cluster/8093/logs:/root/logs
  34. environment:
  35. SEATA_PORT: 8091

启动后效果:

 

 

(版本二)

  1. version: "3"
  2. services:
  3. seata1:
  4. container_name: seata1
  5. image: seataio/seata-server:1.4.0
  6. ports:
  7. - "8091:8091"
  8. volumes:
  9. - /etc/localtime:/etc/localtime
  10. - /usr/local/work/docker/seata/conf/registry.conf:/seata-server/resources/registry.conf
  11. - /usr/local/work/docker/seata/logs/8091:/root/logs
  12. environment:
  13. SEATA_PORT: 8091
  14. SEATA_IP: 192.168.1.201
  15. seata2:
  16. container_name: seata2
  17. image: seataio/seata-server:1.4.0
  18. ports:
  19. - "8092:8092"
  20. volumes:
  21. - /etc/localtime:/etc/localtime
  22. - /usr/local/work/docker/seata/conf/registry.conf:/seata-server/resources/registry.conf
  23. - /usr/local/work/docker/seata/logs/8092:/root/logs
  24. environment:
  25. SEATA_PORT: 8092
  26. SEATA_IP: 192.168.1.201
  27. seata3:
  28. container_name: seata3
  29. image: seataio/seata-server:1.4.0
  30. ports:
  31. - "8093:8093"
  32. volumes:
  33. - /etc/localtime:/etc/localtime
  34. - /usr/local/work/docker/seata/conf/registry.conf:/seata-server/resources/registry.conf
  35. - /usr/local/work/docker/seata/logs/8093:/root/logs
  36. environment:
  37. SEATA_PORT: 8093
  38. SEATA_IP: 192.168.1.201
  1. seata_port:指定端口
  2. seata_ip:指定容器的ip

启动后效果:

为什么有个版本二?

在一台机器上安装了3个seata的docker 容器,每个容器自己有一个生成的ip,当注册到nacos的时候,也是注册的这个ip + port

这就导致了 自己的seata客户端启动 连接seata服务器的时候,连接的地址是docker内部地址,肯定连接不上,报错

can not connect to 172.25.0.3:8091 cause:can not register RM,err:can not connect to services-server

所以,在版本二中,seata_ip都设置为了宿主机ip(宿主机能ping通,同时放开8091 8092 8093端口),这样就好了

 

启动

docker-compose up -d

关闭

docker-compose down

客户端配置(不需要在resources目录下加 file.conf 和 registry.conf 文件):

  1. seata:
  2. enabled: true
  3. # Seata 应用编号,默认为 ${spring.application.name}
  4. application-id: ${spring.application.name}
  5. # Seata 事务组编号,用于 TC 集群名
  6. tx-service-group: my_test_tx_group
  7. # 自动代理
  8. enable-auto-data-source-proxy: true
  9. # 服务配置项
  10. service:
  11. vgroup-mapping:
  12. my_test_tx_group: default
  13. config:
  14. type: nacos
  15. nacos:
  16. serverAddr: 192.168.1.201:8850
  17. group: SEATA_GROUP
  18. namespace: seata
  19. registry:
  20. type: nacos
  21. nacos:
  22. application: seata-server
  23. server-addr: 192.168.1.201:8850
  24. namespace:
  25. cluster: default
  26. group: SEATA_GROUP

项目依赖:

  1. <!-- seata 依赖 -->
  2. <dependency>
  3. <groupId>com.alibaba.cloud</groupId>
  4. <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
  5. <version>2.2.5.RELEASE</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>io.seata</groupId>
  9. <artifactId>seata-spring-boot-starter</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>io.seata</groupId>
  15. <artifactId>seata-spring-boot-starter</artifactId>
  16. <version>1.4.0</version>
  17. <exclusions>
  18. <exclusion>
  19. <groupId>io.seata</groupId>
  20. <artifactId>seata-all</artifactId>
  21. </exclusion>
  22. </exclusions>
  23. </dependency>
  24. <dependency>
  25. <groupId>io.seata</groupId>
  26. <artifactId>seata-all</artifactId>
  27. <version>1.4.0</version>
  28. </dependency>

ps:  上面 有可能会报错:

Caused by: io.seata.common.exception.ShouldNeverHappenException: Can't find any object of class org.springframework.context.ApplicationContext

换成下面的依赖即可

  1. <!-- seata 依赖 -->
  2. <dependency>
  3. <groupId>io.seata</groupId>
  4. <artifactId>seata-spring-boot-starter</artifactId>
  5. <version>1.4.0</version>
  6. </dependency>

 

完成,如有错误,请指出~~~

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号