一.整體搭建準(zhǔn)備
? ?一.1.下載seata-server服務(wù)端(地址:https://seata.io/zh-cn/blog/download.html)
? ? 一.2. 修改seata-serve里面的配置file.conf文件,file.conf.example文件耳鸯,以及registry.conf文件
? ? 一.3. 從githhub下載好準(zhǔn)備好的demo,然后配更改好demo里的一些配置文件(改成自己本地的即可)
? ? ? ? ? ? demo地址(https://github.com/chengshugithub/springcloud-eureka-feign-seata)
? ? 一.4. 運(yùn)行 eureka的服務(wù)端撒强,然后運(yùn)行seata-server锌雀,然后繼續(xù)運(yùn)行我們demo里面的三個(gè)服務(wù)蹂随;
二.先通過官網(wǎng)下載 seata-server(地址:https://seata.io/zh-cn/blog/download.html)
? ?二.1.下載之后琉预,我們進(jìn)行解壓然后找到如下圖三個(gè)文件進(jìn)行修改
?我的file.conf文件配置
這里我采用的是db方式劝评,連接是mysql;自己改成自己本機(jī)的mysql的用戶名密碼和驅(qū)動(dòng)
## transaction log store, only used in seata-server
store {
? ## store mode: file倦淀、db蒋畜、redis
?#這里的話選擇是db,級(jí)尋找下面的db配置撞叽,這里我用的是mysql姻成;
? 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)/HikariDataSource(hikari) etc.
? ? datasource = "dbcp"
? ? ## mysql/oracle/postgresql/h2/oceanbase etc.
? ? dbType = "mysql"
? ? driverClassName = "com.mysql.cj.jdbc.Driver"
? ? url = "jdbc:mysql://127.0.0.1:3306/seata-server?serverTimezone=GMT%2B8"
? ? user = "root"
? ? password = "root"
? ? minConn = 5
? ? maxConn = 100
? ? globalTable = "global_table"
? ? branchTable = "branch_table"
? ? lockTable = "lock_table"
? ? queryLimit = 100
? ? maxWait = 5000
? }
? ## redis store property
? redis {
? ? host = "127.0.0.1"
? ? port = "6379"
? ? password = ""
? ? database = "0"
? ? minConn = 1
? ? maxConn = 10
? ? maxTotal = 100
? ? queryLimit = 100
? }
}
## metrics configuration, only used in server side
metrics {
? enabled = false
? registryType = "compact"
? # multi exporters use comma divided
? exporterList = "prometheus"
? exporterPrometheusPort = 9898
}
? 我的file.conf.example文件配置
? ?需要注意的是 service里面的配置? vgroupMapping.my_test_tx_group = "default";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?default.grouplist = "127.0.0.1:8091"
transport {
? # tcp udt unix-domain-socket
? type = "TCP"
? #NIO NATIVE
? server = "NIO"
? #enable heartbeat
? heartbeat = true
? # the client batch send request enable
? enableClientBatchSendRequest = true
? #thread factory for netty
? threadFactory {
? ? bossThreadPrefix = "NettyBoss"
? ? workerThreadPrefix = "NettyServerNIOWorker"
? ? serverExecutorThread-prefix = "NettyServerBizHandler"
? ? shareBossWorker = false
? ? clientSelectorThreadPrefix = "NettyClientSelector"
? ? clientSelectorThreadSize = 1
? ? clientWorkerThreadPrefix = "NettyClientWorkerThread"
? ? # netty boss thread size,will not be used for UDT
? ? bossThreadSize = 1
? ? #auto default pin or 8
? ? workerThreadSize = "default"
? }
? shutdown {
? ? # when destroy server, wait seconds
? ? wait = 3
? }
? serialization = "seata"
? compressor = "none"
}
service {
? #transaction service group mapping
? vgroupMapping.my_test_tx_group = "default"
? #only support when registry.type=file, please don't set multiple addresses
? default.grouplist = "127.0.0.1:8091"
? #degrade, current not support
? enableDegrade = false
? #disable seata
? disableGlobalTransaction = false
}
client {
? rm {
? ? asyncCommitBufferLimit = 10000
? ? lock {
? ? ? retryInterval = 10
? ? ? retryTimes = 30
? ? ? retryPolicyBranchRollbackOnConflict = true
? ? }
? ? reportRetryCount = 5
? ? tableMetaCheckEnable = false
? ? reportSuccessEnable = false
? }
? tm {
? ? commitRetryCount = 5
? ? rollbackRetryCount = 5
? }
? undo {
? ? dataValidation = true
? ? logSerialization = "jackson"
? ? logTable = "undo_log"
? }
? log {
? ? exceptionRate = 100
? }
}
?我的registry.conf文件配置
這個(gè)注冊(cè)的話愿棋,采用的是eureka的配置科展;這里會(huì)把seata服務(wù)注冊(cè)到eureka;
registry {
? # file 糠雨、nacos 才睹、eureka、redis甘邀、zk琅攘、consul、etcd3松邪、sofa
? type = "eureka"
? loadBalance = "RandomLoadBalance"
? loadBalanceVirtualNodes = 10
? nacos {
? ? application = "seata-server"
? ? serverAddr = "127.0.0.1:8848"
? ? group = "SEATA_GROUP"
? ? namespace = ""
? ? cluster = "default"
? ? username = ""
? ? password = ""
? }
? 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 = "file"
? nacos {
? ? serverAddr = "127.0.0.1:8848"
? ? namespace = ""
? ? group = "SEATA_GROUP"
? ? username = ""
? ? password = ""
? }
? consul {
? ? serverAddr = "127.0.0.1:8500"
? }
? apollo {
? ? appId = "seata-server"
? ? apolloMeta = "http://192.168.1.204:8801"
? ? namespace = "application"
? ? apolloAccesskeySecret = ""
? }
? zk {
? ? serverAddr = "127.0.0.1:2181"
? ? sessionTimeout = 6000
? ? connectTimeout = 2000
? ? username = ""
? ? password = ""
? }
? etcd3 {
? ? serverAddr = "http://localhost:2379"
? }
? file {
? ? name = "file.conf"
? }
}
二.2.接下來創(chuàng)建seata所需要的的數(shù)據(jù)庫(kù)和表;
根據(jù) file.conf文件里面的db連接的數(shù)據(jù)庫(kù)名荧关,當(dāng)然自己可以隨便改名溉奕;
我們需要?jiǎng)?chuàng)建三個(gè)表,這個(gè)表是給seata-server用的羞酗;
需要以下三張表腐宋;建表sql如下;
CREATE TABLE `branch_table` (
? `branch_id` bigint(20) NOT NULL,
? `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
? `transaction_id` bigint(20) NULL DEFAULT NULL,
? `resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `status` tinyint(4) NULL DEFAULT NULL,
? `client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `gmt_create` datetime(6) NULL DEFAULT NULL,
? `gmt_modified` datetime(6) NULL DEFAULT NULL,
? PRIMARY KEY (`branch_id`) USING BTREE,
? INDEX `idx_xid`(`xid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for global_table
-- ----------------------------
DROP TABLE IF EXISTS `global_table`;
CREATE TABLE `global_table`? (
? `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
? `transaction_id` bigint(20) NULL DEFAULT NULL,
? `status` tinyint(4) NOT NULL,
? `application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `transaction_service_group` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `timeout` int(11) NULL DEFAULT NULL,
? `begin_time` bigint(20) NULL DEFAULT NULL,
? `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `gmt_create` datetime(0) NULL DEFAULT NULL,
? `gmt_modified` datetime(0) NULL DEFAULT NULL,
? PRIMARY KEY (`xid`) USING BTREE,
? INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE,
? INDEX `idx_transaction_id`(`transaction_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for lock_table
-- ----------------------------
DROP TABLE IF EXISTS `lock_table`;
CREATE TABLE `lock_table`? (
? `row_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
? `xid` varchar(96) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `transaction_id` bigint(20) NULL DEFAULT NULL,
? `branch_id` bigint(20) NOT NULL,
? `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `table_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `pk` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? `gmt_create` datetime(0) NULL DEFAULT NULL,
? `gmt_modified` datetime(0) NULL DEFAULT NULL,
? PRIMARY KEY (`row_key`) USING BTREE,
? INDEX `idx_branch_id`(`branch_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
二.2此時(shí)去github上down我們的項(xiàng)目(地址:https://github.com/chengshugithub/springcloud-eureka-feign-seata)檀轨;
? ?@1然后每個(gè)服務(wù)下面都有創(chuàng)建自己表的sql語句胸竞;然后根據(jù)配置文件去創(chuàng)建每個(gè)服務(wù)的庫(kù);
? ?@2 我們服務(wù)的事物提交主要是通過seata服務(wù)去提交這個(gè)全局的事物参萄,每一個(gè)服務(wù)的數(shù)據(jù)庫(kù)都需要去創(chuàng)建一張這個(gè)表卫枝,sql如下;
CREATE TABLE `undo_log` (
? `id` bigint(20) NOT NULL AUTO_INCREMENT,
? `branch_id` bigint(20) NOT NULL,
? `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
? `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
? `rollback_info` longblob NOT NULL,
? `log_status` int(11) NOT NULL,
? `log_created` datetime(0) NOT NULL,
? `log_modified` datetime(0) NOT NULL,
? `ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
? PRIMARY KEY (`id`) USING BTREE,
? UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
?@3然后每個(gè)服務(wù)下面都需要去增加file.conf和regist.conf文件讹挎,這個(gè)regist.conf文件其實(shí)就是seata-server下的file.conf.example的文件校赤;具體的在項(xiàng)目里面看;
@4 總結(jié)一下:如果想把自己的項(xiàng)目整合到seata-server管理筒溃。
? ? ? ?@4.1 引入setata依賴:(因?yàn)橐蕾嚪旁谶@格式會(huì)亂马篮,所以就不放了)
? ? ? @4.2? resources下配置file.conf文件和 registry.conf文件;
? ? ? @4.3 數(shù)據(jù)庫(kù)里面增加一個(gè)undo_log的表怜奖;
? ? ? @4.4 修改配置文件浑测,如下:
? ? ?@4.5 數(shù)據(jù)源交給setata代理處理;(代碼上有)
? ? @4.6? 然后代碼塊使用@GlobalTransactional注解
三.此時(shí)我們?cè)龠M(jìn)行運(yùn)行程序歪玲;
? ?1.啟動(dòng) eureka-server
? ?2.啟動(dòng) seatea-serer
? ?3.陸續(xù)啟動(dòng)自己demo的三個(gè)服務(wù)迁央;
? 4.http://localhost:8180/order/create?userId=1&productId=1&count=10&money=100;
( 邏輯的話自己看就好了滥崩,基本上是訂單調(diào)用庫(kù)存岖圈,庫(kù)存減少,賬戶減少)