Seata的集成方式有:
1. Seata-All
2. Seata-Spring-Boot-Starter
3. Spring-Cloud-Starter-Seata
本案例使用Seata-Spring-Boot-Starter演示:
第一步:下載Seata
第二步:為了更好看到效果,我們將Seata的數(shù)據(jù)存儲改為db
將seata\script\server\db\mysql.sql語句在mysql中執(zhí)行,創(chuàng)建相關表
?
然后修改seata配置文件,相關配置可以參考application.example.yml,此處修改mode為db并且配置db相關配置
?
啟動seata:
?
第三步:Spring Boot集成Seata
1.pom.xml導包:
<!--seata-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
</dependency>
2.application.properties添加seata配置
#env
spring.profiles.active=dev
spring.application.name=coupon-service-provider
#dubbo.application.id=coupon-service
dubbo.application.name=coupon-service
dubbo.protocol.port=-1
dubbo.protocol.name=dubbo
#is simplified url
#dubbo.registry.simplified=true
#registry
#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
#multi registries
#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 config
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
#========== seata config =============
seata.data-source-proxy-mode=AT
#tx group
seata.tx-service-group=demo-tx-default-group
seata.service.vgroup-mapping.demo-tx-default-group=default
#tx group cluster
seata.service.grouplist.default=192.168.1.105:8091
seata.client.undo.log-serialization=kryo
就兩步完成Spring Boot集成Seata
測試:
1.首先在portal標明分布式事務
2.postman調用?
?打斷點調試
?異常觸發(fā)前,看到數(shù)據(jù)插入成功,且undo_log有兩條數(shù)據(jù)
斷點繼續(xù)往下走,觸發(fā)異常,,已插入的數(shù)據(jù)被回滾,且undo_log被清空,分布式事務回滾正常。
?文章來源:http://www.zghlxwxcb.cn/news/detail-627638.html
?至此,Spring Boot集成Seata演示完畢文章來源地址http://www.zghlxwxcb.cn/news/detail-627638.html
到了這里,關于Spring Boot 集成Seata的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!