SpringCloud Alibaba + Dubbo + Nacos + Seata - 环境部署

本文介绍如何使用Spring Cloud Alibaba Seata实现分布式事务管理。主要内容包括Nacos和Seata的安装配置过程,数据库表结构创建,以及Seata配置提交至Nacos的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、版本

SpringBoot(2.1.6.RELEASE),SpringCloud(Greenwich.SR2),SpringCloudAlibaba(2.1.3.RELEASE),Nacos(1.2.1),Seata(1.2.0)

二、环境搭建

1、nacos安装

下载解压安装:https://github.com/alibaba/nacos/releases

运行:./bin/startup.sh -m standalone &

访问:http://localhost:8848/nacos/

2、安装seata

seata-service下载解压:https://seata.io/zh-cn/blog/download.html

seata源码:https://github.com/seata/seata/releases

undo_log表:在seata1.2源码seata-1.2.0/script/client/at/db目录下有提供针对mysqloraclepostgresql这三种数据库生成undo-log逆向日志回滚表的表创建脚本,根据自身数据库类型来选择。

seata事务相关表:在seata1.2源码seata-1.2.0/seata-1.2.0/script/server/db目录下有提供针对mysqloraclepostgresql这三种数据库事务表创建脚本,根据自身数据库类型来选择。

2.1、创建seata数据库,并在该库下执行事务相关表的创建(global_table、branch_table、lock_table)

2.2、分别创建相关服务数据库

  • 账户服务库:seata_account
CREATE TABLE `t_account` (
  `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
  `total` decimal(10,0) DEFAULT NULL COMMENT '总额度',
  `used` decimal(10,0) DEFAULT NULL COMMENT '已用额度',
  `residue` decimal(10,0) DEFAULT NULL COMMENT '剩余可用额度',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8 COMMENT='账户';
  • 订单服务库:seata_order
CREATE TABLE `t_order` (
  `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
  `product_id` bigint(11) DEFAULT NULL COMMENT '产品id',
  `count` int(11) DEFAULT NULL COMMENT '数量',
  `money` decimal(11,0) DEFAULT NULL COMMENT '金额',
  `status` int(1) DEFAULT NULL COMMENT '订单状态 0:创建中 1:已完成',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=220 DEFAULT CHARSET=utf8 COMMENT='订单表';
  • 库存服务库:seata_storage
CREATE TABLE `t_storage` (
  `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
  `product_id` bigint(11) DEFAULT NULL COMMENT '产品id',
  `total` int(11) DEFAULT NULL COMMENT '总库存',
  `used` int(11) DEFAULT NULL COMMENT '已用库存',
  `residue` int(11) DEFAULT NULL COMMENT '剩余库存',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='库存表';

2.3、分别在业务库seata_account、seata_order、seata_storage下创建undo-log表

2.4、配置seata

  • 配置registry.conf文件中的registry和config模块改nacos,配置nacos相关属性参数
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "localhost:8848"    # 上面安装的nacos访问地址
    group = "SEATA_GROUP"
    namespace = "public"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = 0
    password = ""
    cluster = "default"
    timeout = 0
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "localhost:8848"
    namespace = "public"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    appId = "seata-server"
    apolloMeta = "http://192.168.1.204:8801"
    namespace = "application"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}
  • 配置file.conf文件中的store模块改db,配置db相关属性参数
## transaction log store, only used in seata-server
store {
  ## store mode: file、db
  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) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata"    # 上面创建的seata事务相关数据库
    user = "mysql"
    password = "123456"
    minConn = 5
    maxConn = 30
    globalTable = "global_table"                # 上面创建的事务相关表
    branchTable = "branch_table"                # 上面创建的事务相关表
    lockTable = "lock_table"                    # 上面创建的事务相关表
    queryLimit = 100
    maxWait = 5000
  }
}

2.5、修改seata的配置提交到nacos,并提交到nacos

  • 修改:/seata-1.2.0/script/config-center/config.txt 下的这些参数。config.txt提供的脚本是seata-server服务自身所需要的配置文件信息,我们把相关脚本提交到nacos中,然后方便在nacos控制台上进行修改变更了
service.vgroupMapping.my_test_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=username
store.db.password=password
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
  • 运行:/seata-1.2.0/script/config-center/nacos/nacos-config.sh 将上面config.txt提交到nacos控制台,如果有需要修改参数,可直接通过登录nacos控制台修改
./nacos-config.sh

或

./nacos-config.sh -h host -p port -g group
host: nacos的ip地址
port:nacos的端口
group:nacos配置中心的组
  • 运行成功后可在nacos中的配置列表中看到相关配置:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值