最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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語(yǔ)句在mysql中執(zhí)行,創(chuàng)建相關(guān)表

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

啟動(dòng)seata:

 第三步:開(kāi)始集成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)用信息跟蹤-將訪(fǎng)問(wèn)日志輸出到指定文件
#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.編寫(xiě)Seata全局事務(wù)掃描器配置類(lèi)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. 由于A(yíng)T模型是使用本地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ù)是否成功插入過(guò):

看到記錄插入成功,并且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)文章

  • 舉例講解Java的RTTI運(yùn)行時(shí)類(lèi)型識(shí)別機(jī)制

    舉例講解Java的RTTI運(yùn)行時(shí)類(lèi)型識(shí)別機(jī)制

    這篇文章主要介紹了Java的RTTI運(yùn)行時(shí)類(lèi)型識(shí)別機(jī)制,包括泛化的Class引用以及類(lèi)型檢查instanceof等知識(shí)點(diǎn),需要的朋友可以參考下
    2016-05-05
  • Java SpringBoot自定義注解的使用及說(shuō)明

    Java SpringBoot自定義注解的使用及說(shuō)明

    本文介紹了在Spring Boot中創(chuàng)建和使用自定義注解的方法,通過(guò)自定義注解,可以減少重復(fù)代碼、增強(qiáng)代碼可讀性和可維護(hù)性,具體步驟包括定義注解、創(chuàng)建注解處理器以及在業(yè)務(wù)方法上使用注解
    2025-11-11
  • Java旋轉(zhuǎn)數(shù)組中最小數(shù)字具體實(shí)現(xiàn)(圖文詳解版)

    Java旋轉(zhuǎn)數(shù)組中最小數(shù)字具體實(shí)現(xiàn)(圖文詳解版)

    這篇文章主要給大家介紹了關(guān)于Java旋轉(zhuǎn)數(shù)組中最小數(shù)字具體實(shí)現(xiàn)的相關(guān)資料,旋轉(zhuǎn)數(shù)組,說(shuō)明數(shù)據(jù)不變,只是改變位置,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • SpringBoot實(shí)現(xiàn)接口防刷的五種方案

    SpringBoot實(shí)現(xiàn)接口防刷的五種方案

    接口防刷是保障系統(tǒng)安全與穩(wěn)定性的重要措施,惡意的高頻請(qǐng)求不僅會(huì)消耗服務(wù)器資源,還可能導(dǎo)致數(shù)據(jù)異常,甚至系統(tǒng)癱瘓,本文將介紹在SpringBoot框架下實(shí)現(xiàn)接口防刷的5種技術(shù)方案,需要的朋友可以參考下
    2025-04-04
  • Java實(shí)現(xiàn)郵箱發(fā)送功能實(shí)例(阿里云郵箱推送)

    Java實(shí)現(xiàn)郵箱發(fā)送功能實(shí)例(阿里云郵箱推送)

    這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)郵箱發(fā)送功能的相關(guān)資料,利用阿里云郵箱推送,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • MyBatis插件實(shí)現(xiàn)SQL執(zhí)行耗時(shí)監(jiān)控

    MyBatis插件實(shí)現(xiàn)SQL執(zhí)行耗時(shí)監(jiān)控

    MyBatis插件(Plugin)是其框架擴(kuò)展性的核心,本文深入剖析插件實(shí)現(xiàn)原理,手把手教你開(kāi)發(fā)企業(yè)級(jí)SQL監(jiān)控插件,有需要的小伙伴可以了解下
    2025-12-12
  • SpringMVC?處理后端日期格式的示例詳解

    SpringMVC?處理后端日期格式的示例詳解

    在WebMvcConfiguration中擴(kuò)展SpringMVC的消息轉(zhuǎn)換器,統(tǒng)一對(duì)日期類(lèi)型進(jìn)行格式處理,本文給大家介紹SpringMVC處理后端日期格式,感興趣的朋友一起看看吧
    2023-11-11
  • springboot3解決跨域的幾種方式小結(jié)

    springboot3解決跨域的幾種方式小結(jié)

    這篇文章主要介紹了springboot3解決跨域的幾種方式,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的解決跨域有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • 淺談在JAVA項(xiàng)目中LOG4J的使用

    淺談在JAVA項(xiàng)目中LOG4J的使用

    下面小編就為大家?guī)?lái)一篇淺談在JAVA項(xiàng)目中LOG4J的使用。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • Java split 分隔空值無(wú)法得到的解決方式

    Java split 分隔空值無(wú)法得到的解決方式

    這篇文章主要介紹了Java split 分隔空值無(wú)法得到的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10

最新評(píng)論

时尚| 瓮安县| 永兴县| 牡丹江市| 阿荣旗| 雅江县| 清河县| 白城市| 平顺县| 阿克苏市| 鹰潭市| 寻甸| 阳东县| 沂南县| 富源县| 巴林左旗| 盐边县| 宁安市| 石嘴山市| 密山市| 南陵县| 茂名市| 奉贤区| 万安县| 武冈市| 鹤岗市| 虎林市| 都江堰市| 隆尧县| 祁连县| 银川市| 冕宁县| 阿尔山市| 固始县| 侯马市| 永和县| 咸丰县| 曲水县| 若尔盖县| 佛教| 嫩江县|