NACOS
下载nacos
https://github.com/alibaba/nacos/releases/tag/2.2.0
启动nacos
startup.cmd -m standalone
SEATA
下载seata
https://seata.apache.org/release-history/seata-server
新建数据库-seata
CREATE TABLE `branch_table` (
`branch_id` bigint NOT NULL,
`xid` varchar(128) NOT NULL,
`transaction_id` bigint DEFAULT NULL,
`resource_group_id` varchar(32) DEFAULT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`branch_type` varchar(8) DEFAULT NULL,
`status` tinyint DEFAULT NULL,
`client_id` varchar(64) DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime(6) DEFAULT NULL,
`gmt_modified` datetime(6) DEFAULT NULL,
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `global_table` (
`xid` varchar(128) NOT NULL,
`transaction_id` bigint DEFAULT NULL,
`status` tinyint NOT NULL,
`application_id` varchar(32) DEFAULT NULL,
`transaction_service_group` varchar(32) DEFAULT NULL,
`transaction_name` varchar(128) DEFAULT NULL,
`timeout` int DEFAULT NULL,
`begin_time` bigint DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`xid`),
KEY `idx_status_gmt_modified` (`status`,`gmt_modified`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `lock_table` (
`row_key` varchar(128) NOT NULL,
`xid` varchar(128) DEFAULT NULL,
`transaction_id` bigint DEFAULT NULL,
`branch_id` bigint NOT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`table_name` varchar(32) DEFAULT NULL,
`pk` varchar(36) DEFAULT NULL,
`status` tinyint NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`row_key`),
KEY `idx_status` (`status`),
KEY `idx_branch_id` (`branch_id`),
KEY `idx_xid_and_branch_id` (`xid`,`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `distributed_lock` VALUES ('AsyncCommitting', ' ', '0');
INSERT INTO `distributed_lock` VALUES ('RetryCommitting', ' ', '0');
INSERT INTO `distributed_lock` VALUES ('RetryRollbacking', ' ', '0');
INSERT INTO `distributed_lock` VALUES ('TxTimeoutCheck', ' ', '0');
INSERT INTO `distributed_lock` VALUES ('UndologDelete', ' ', '0');
修改配置文件
seata-server-2.0.0\seata\conf\application.yml
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${log.home:${user.home}/logs/seata}
#extend:
# logstash-appender:
# destination: 127.0.0.1:4560
# kafka-appender:
# bootstrap-servers: 127.0.0.1:9092
# topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: dev_seata
group: DEV_GROUP
username: nacos
password: nacos
context-path: /nacos
data-id: seataServer.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: DEV_GROUP
namespace: dev_seata
cluster: default
username: nacos
password: nacos
context-path: /nacos
server:
service-port: 8091 #If not configured, the default is '${server.port} + 1000'
max-commit-retry-timeout: -1
max-rollback-retry-timeout: -1
rollback-retry-timeout-unlock-enable: false
enable-check-auth: true
enable-parallel-request-handle: true
enable-parallel-handle-branch: false
retry-dead-threshold: 130000
xaer-nota-retry-timeout: 60000
enableParallelRequestHandle: true
recovery:
committing-retry-period: 1000
async-committing-retry-period: 1000
rollbacking-retry-period: 1000
timeout-retry-period: 1000
undo:
log-save-days: 7
log-delete-period: 86400000
session:
branch-async-queue-size: 5000 #branch async remove queue size
enable-branch-async-remove: false #enable to asynchronous remove branchSession
store:
# support: file 、 db 、 redis 、 raft
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata
user: root
password: 111
min-conn: 10
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 1000
max-wait: 5000
metrics:
enabled: false
registry-type: compact
exporter-list: prometheus
exporter-prometheus-port: 9898
transport:
rpc-tc-request-timeout: 15000
enable-tc-server-batch-send-response: false
shutdown:
wait: 3
thread-factory:
boss-thread-prefix: NettyBoss
worker-thread-prefix: NettyServerNIOWorker
boss-thread-size: 1
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login,/metadata/v1/**
nacos增加配置文件seataServer.properties
#Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption
#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
store.db.datasource=druid
store.db.dbType=mysql
#使用mysql8驱动
store.db.driverClassName=com.mysql.cj.jdbc.Driver
#设置时区
store.db.url=jdbc:mysql://127.0.0.1:3306/seata
store.db.user=root
store.db.password=Loit@0487
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
启动seata
seata-server.bat
启动成功标识
业务代码
数据库sql
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 80040
Source Host : localhost:3306
Source Database : order
Target Server Type : MYSQL
Target Server Version : 80040
File Encoding : 65001
Date: 2025-01-16 13:59:52
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for order_tbl
-- ----------------------------
DROP TABLE IF EXISTS `order_tbl`;
CREATE TABLE `order_tbl` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int DEFAULT '0',
`money` int DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of order_tbl
-- ----------------------------
INSERT INTO `order_tbl` VALUES ('5', '1', '1', '1', '500');
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint NOT NULL AUTO_INCREMENT,
`branch_id` bigint NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of undo_log
-- ----------------------------
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 80040
Source Host : localhost:3306
Source Database : account
Target Server Type : MYSQL
Target Server Version : 80040
File Encoding : 65001
Date: 2025-01-16 14:00:10
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for account_tbl
-- ----------------------------
DROP TABLE IF EXISTS `account_tbl`;
CREATE TABLE `account_tbl` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`money` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- ----------------------------
-- Records of account_tbl
-- ----------------------------
INSERT INTO `account_tbl` VALUES ('1', '1', '99500');
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint NOT NULL AUTO_INCREMENT,
`branch_id` bigint NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of undo_log
-- ----------------------------
nacos增加配置文件
命名空间:dev
spring:
datasource:
# 当前数据源操作类型
type: com.alibaba.druid.pool.DruidDataSource
# mysql驱动类
#driver-class-name: com.mysql.cj.jdbc.Driver
#url: jdbc:mysql://localhost:3306/order?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
#username: root
#password: Loit@0487
#driver-class-name: org.postgresql.Driver
#url: jdbc:postgresql://10.251.106.100:5432/order
#username: vbadmin
#password: SObr14uPEWBZ8A3M0R__
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://10.251.106.99:5432/order
username: sysadmin
password: A@ssemble1q2w3e4r5t6y
mybatis:
mapper-locations: classpath*:mapper/*.xml
spring:
datasource:
# 当前数据源操作类型
type: com.alibaba.druid.pool.DruidDataSource
# mysql驱动类
#driver-class-name: com.mysql.cj.jdbc.Driver
#url: jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
#username: root
#password: Loit@0487
#driver-class-name: org.postgresql.Driver
#url: jdbc:postgresql://10.251.106.100:5432/account
#username: vbadmin
#password: SObr14uPEWBZ8A3M0R__
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://10.251.106.99:5432/account
username: sysadmin
password: A@ssemble1q2w3e4r5t6y
mybatis:
mapper-locations: classpath*:mapper/*.xml
YML配置文件
account服务配置文件
spring:
cloud:
nacos:
# 注册中心
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: dev
group: DEV_GROUP
# 配置中心
config:
server-addr: 127.0.0.1:8848
context-path: /nacos
username: nacos
password: nacos
namespace: dev
group: DEV_GROUP
#☆ 配置文件后缀名 cloud项目启动时,自动调用nacos配置中心中DataID = ${spring.application.name}-{spring.profiles.active}.${spring.cloud.nacos.config.file-extension} (business-dev.yml) 文件
file-extension: yml
# 使用nacos负载均衡
loadbalancer:
nacos:
enabled: true
config:
#☆ 导入Nacos配置中心的配置文件
import:
- optional:nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}?refresh=true
seata:
enabled: true
application-id: account-seata
#设置分组名称
tx-service-group: shop_tx_group
service:
vgroup-mapping:
# 当前分组的集群名称,seata单机模式集群名称默认为default
shop_tx_group: default
registry:
type: nacos
nacos:
# 配置seata服务在注册中心服务名,默认为seata-server
application: seata-server
server-addr: 127.0.0.1:8848
context-path: /nacos
username: nacos
password: nacos
namespace: dev_seata
group: DEV_GROUP
cluster: default
feign:
client:
config:
# default:对所有服务生效 需要对单个服务配置将default改为服务名
default:
# 设置连接超时时间
connect-timeout: 3000
# 设置读取数据超时时间
read-timeout: 2000
# 调用日志打印等级,需要同步将Feign调用类的日志等级设置为Debug才生效
logger-level: basic
order服务配置文件
spring:
cloud:
nacos:
# 注册中心
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: dev
group: DEV_GROUP
# 配置中心
config:
server-addr: 127.0.0.1:8848
context-path: /nacos
username: nacos
password: nacos
namespace: dev
group: DEV_GROUP
#☆ 配置文件后缀名 cloud项目启动时,自动调用nacos配置中心中DataID = ${spring.application.name}-{spring.profiles.active}.${spring.cloud.nacos.config.file-extension} (business-dev.yml) 文件
file-extension: yml
# 使用nacos负载均衡
loadbalancer:
nacos:
enabled: true
config:
#☆ 导入Nacos配置中心的配置文件
import:
- optional:nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}?refresh=true
seata:
enabled: true
application-id: order-seata
#设置分组名称
tx-service-group: shop_tx_group
service:
vgroup-mapping:
# 当前分组的集群名称,seata单机模式集群名称默认为default
shop_tx_group: default
registry:
type: nacos
nacos:
# 配置seata服务在注册中心服务名,默认为seata-server
application: seata-server
server-addr: 127.0.0.1:8848
context-path: /nacos
username: nacos
password: nacos
namespace: dev_seata
group: DEV_GROUP
cluster: default
feign:
client:
config:
# default:对所有服务生效 需要对单个服务配置将default改为服务名
default:
# 设置连接超时时间
connect-timeout: 3000
# 设置读取数据超时时间
read-timeout: 2000
# 调用日志打印等级,需要同步将Feign调用类的日志等级设置为Debug才生效
logger-level: basic