欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring集成Seata方式(案例演示)

 更新時(shí)間:2023年08月08日 15:59:33   作者:Demonor_  
這篇文章主要介紹了Spring集成Seata方式,本案例使用Seata-All演示,結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Seata的集成方式有:

1. Seata-All

2. Seata-Spring-Boot-Starter

3. Spring-Cloud-Starter-Seata

本案例使用Seata-All演示:

第一步:下載Seata

第二步:為了更好看到效果,我們將Seata的數(shù)據(jù)存儲(chǔ)改為db

將seata\script\server\db\mysql.sql語句在mysql中執(zhí)行,創(chuàng)建相關(guān)表

 然后修改seata配置文件,相關(guān)配置可以參考application.example.yml,此處修改mode為db并且配置db相關(guān)配置

啟動(dòng)seata:

 第三步:開始集成Seata

1.在我們的項(xiàng)目pom.xml引入seata包(1.5.x有bug,我們使用1.4.2)

<!--seata-->
<dependency>
	<groupId>io.seata</groupId>
	<artifactId>seata-all</artifactId> 
</dependency>
<dependency>
    <groupId>com.esotericsoftware</groupId>
    <artifactId>kryo</artifactId>
</dependency>
<dependency>
    <groupId>de.javakaffee</groupId>
    <artifactId>kryo-serializers</artifactId>
</dependency>

2.application.properties

#環(huán)境配置
spring.profiles.active=dev
spring.application.name=user-service-provider
dubbo.application.id=user-service
dubbo.application.name=user-service
#dubbo.protocol.port=-1
#dubbo.protocol.name=dubbo
#多協(xié)議支持
dubbo.protocols.tri.name=tri
dubbo.protocols.tri.port=-1
dubbo.protocols.tri.id=tri
dubbo.protocols.dubbo.name=dubbo
dubbo.protocols.dubbo.port=-1
dubbo.protocols.dubbo.id=dubbo
#序列化方式
dubbo.protocols.dubbo.serialization=kryo
dubbo.application.logger=log4j
#調(diào)用信息跟蹤-log4j 日志
#dubbo.protocol.accesslog=true
#調(diào)用信息跟蹤-將訪問日志輸出到指定文件
#dubbo.protocol.accesslog=/log/access.log
#是否簡(jiǎn)化 url
#dubbo.registry.simplified=true
#dubbo.registry.id=zk-registry
#dubbo.registry.address=zookeeper://192.168.1.105:2181?timeout=20000
#dubbo.config-center.address=zookeeper://192.168.1.105:2181?timeout=20000
#dubbo.metadata-report.address=zookeeper://192.168.1.105:2181?timeout=20000
#多注冊(cè)中心
#dubbo.registries.zk-registry.id=zk-registry
#dubbo.registries.zk-registry.address=zookeeper://192.168.1.105:2181?timeout=20000&blockUntilConnectedWait=30
dubbo.registries.nacos-registry.id=nacos-registry
dubbo.registries.nacos-registry.address=nacos://192.168.1.105:8848
#dubbo.registries.nacos-registry.address=nacos://192.168.1.105:8848?namespace=2e73933a-34d4-4fba-b21e-f08cd9b8adc9
dubbo.registry.use-as-config-center=false
dubbo.registry.use-as-metadata-center=false
#nacos配置中心
nacos.config.server-addr=192.168.1.105:8848
#mybatis
mybatis.mapper-locations= classpath*:com/lee/demo/dubbo/demo/dao/*Mapper.xml
mybatis.type-aliases-package=com.lee.demo.dubbo.demo.entity
#datasource
spring.datasource.url=jdbc:mysql://localhost:3308/coupon-platform?useUnicode=true&characterEncoding=utf8&useOldAliasMetadataBehavior=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Aa1225102411
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 

3.在resources目錄下添加配置文件file.conf和registry.conf,seata會(huì)掃描這兩個(gè)配置文件從而獲取相關(guān)配置。

registry.conf:

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "file"
  file {
    name = "file.conf"
  }
}
config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "file"
  file {
    name = "file.conf"
  }
}

file.conf:

transport {
  # tcp, unix-domain-socket
  type = "TCP"
  #NIO, NATIVE
  server = "NIO"
  #enable heartbeat
  heartbeat = true
  # the tm client batch send request enable
  enableTmClientBatchSendRequest = false
  # the rm client batch send request enable
  enableRmClientBatchSendRequest = true
   # the rm client rpc request timeout
  rpcRmRequestTimeout = 2000
  # the tm client rpc request timeout
  rpcTmRequestTimeout = 10000
  # the tc client rpc request timeout
  rpcTcRequestTimeout = 5000
  #thread factory for netty
  threadFactory {
    bossThreadPrefix = "NettyBoss"
    workerThreadPrefix = "NettyServerNIOWorker"
    serverExecutorThread-prefix = "NettyServerBizHandler"
    shareBossWorker = false
    clientSelectorThreadPrefix = "NettyClientSelector"
    clientSelectorThreadSize = 1
    clientWorkerThreadPrefix = "NettyClientWorkerThread"
    # netty boss thread size
    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.demo-tx-default-group = "default"
  #only support when registry.type=file, please don't set multiple addresses
  default.grouplist = "192.168.8.133: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
    tableMetaCheckerInterval = 60000
    reportSuccessEnable = false
    sagaBranchRegisterEnable = false
    sagaJsonParser = "fastjson"
    sagaRetryPersistModeUpdate = false
    sagaCompensatePersistModeUpdate = false
    tccActionInterceptorOrder = -2147482648 #Ordered.HIGHEST_PRECEDENCE + 1000
  }
  tm {
    commitRetryCount = 5
    rollbackRetryCount = 5
    defaultGlobalTransactionTimeout = 60000
    degradeCheck = false
    degradeCheckPeriod = 2000
    degradeCheckAllowTimes = 10
    interceptorOrder = -2147482648 #Ordered.HIGHEST_PRECEDENCE + 1000
  }
  undo {
    dataValidation = true
    onlyCareUpdateColumns = true
    logSerialization = "kryo"
    logTable = "undo_log"
    compress {
      enable = true
      # allow zip, gzip, deflater, 7z, lz4, bzip2, zstd default is zip
      type = zip
      # if rollback info size > threshold, then will be compress
      # allow k m g t
      threshold = 64k
    }
  }
  loadBalance {
      type = "RandomLoadBalance"
      virtualNodes = 10
  }
}
log {
  exceptionRate = 100
}
tcc {
  fence {
    # tcc fence log table name
    logTableName = tcc_fence_log
    # tcc fence log clean period
    cleanPeriod = 1h
  }
}

4.編寫Seata全局事務(wù)掃描器配置類SeataTransctionConfig:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.seata.spring.annotation.GlobalTransactionScanner;
@Configuration
public class SeataTransctionConfig {
	@Bean
	public GlobalTransactionScanner globalTransactionScanner() {
		String txServiceGroup = "demo-tx-default-group";
		return new GlobalTransactionScanner(txServiceGroup);
	}
}

注意:在file.conf中的service配置要和SeataTransctionConfig 中指定的txServiceGroup 一致

5. 由于AT模型是使用本地undo_log進(jìn)行回滾,因此,我們每個(gè)事務(wù)所在的數(shù)據(jù)庫(kù)中需要添加該表

在seata github中有樣板,鏈接:https://github.com/seata/seata/tree/develop/srcipt/client/at/db

其中mysql.sql如下: 

-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log`  (
  `branch_id` bigint NOT NULL COMMENT 'branch transaction id',
  `xid` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'global transaction id',
  `context` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'undo_log context,such as serialization',
  `rollback_info` longblob NOT NULL COMMENT 'rollback info',
  `log_status` int NOT NULL COMMENT '0:normal status,1:defense status',
  `log_created` datetime(6) NOT NULL COMMENT 'create datetime',
  `log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',
  UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = 'AT transaction mode undo table' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of undo_log
-- ----------------------------

 測(cè)試:

在拋異常之前打斷點(diǎn),查看數(shù)據(jù)是否成功插入過:

看到記錄插入成功,并且undo_log中記錄了這兩個(gè)事務(wù)的日志:

斷點(diǎn)繼續(xù)往下走,觸發(fā)異常,,已插入的數(shù)據(jù)被回滾,且undo_log被清空,分布式事務(wù)回滾正常。

至此,Spring集成Seata演示完畢

到此這篇關(guān)于Spring集成Seata的文章就介紹到這了,更多相關(guān)Spring集成Seata內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論