分布式事务Seata -- seata+springboot+jpa+dubbo+druid

目录

1.重点还是seata-server的配置:

1.1将seata-server注册到zk -- 服务注册

1.2 zk做为配置中心 :导入seata-server配置

 1.3 seata-server的事务log存储相关配置:

1.4 启动seata-server

1.5 启动zk

2.Seata client配置:

3.代码解读:

3.1 服务注册:

4.执行结果:

1.显示抛出异常,全局事务回滚:

2.正常执行,完成全局事务提交:


运行实例:https://github.com/seata/seata-samples/tree/master/springboot

  • seata-server版本:1.4.2
  • zk版本:3.5.7

按照user guide的步骤,一步一步的完成Seata、Dubbo和Springboot的集成

1.重点还是seata-server的配置:

1.1将seata-server注册到zk -- 服务注册

修改seata-server中的conf目录下的registry.conf文件,选择zk做为注册中心。

1.2 zk做为配置中心 :导入seata-server配置

还是修改seata-server中的conf目录下的registry.conf文件,选择zk做为配置中心,集中管理各个微服务的配置信息。

 1.3 seata-server的事务log存储相关配置:

修改seata-server中的conf目录下的文件file.conf,选择使用db去存储事务log。

## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
  ## rsa decryption public key
  publicKey = ""
  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true"
    user = "root"
    password = "123456"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

1.4 启动seata-server

seata-server.bat -h 127.0.0.1 -m db

1.5 启动zk

zk配置文件 zoo.cfg,本例中未做任何修改,默认使用2181端口,直接启动zk(windows中双击 /bin/zkServer.cmd)

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=C:\\technology\\java workspace\\backend\\zookeeper
#<pre name="code" class="html">dataDir=C:\\technology\\java workspace\\backend\\zookeeper</pre>/
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

2.Seata client配置:

我们的springboot应用做为seata client, 需要加入下面的依赖

<dependency>
	<groupId>io.seata</groupId>
	<artifactId>seata-all</artifactId>
	<version>${seata.version}</version>
</dependency>

还要在registry.conf中配置zk,这样seata client从zk中获取seata-server微服务

3.代码解读:

3.1 服务注册:

使用dubbo服务注解,该服务将会被注册到zk

4.执行结果:

启动springboot程序,然后访问http://127.0.0.1:9999/demo//asset/assign

1.显示抛出异常,全局事务回滚:

执行结果如下,分布式事务回滚。

2.正常执行,完成全局事务提交:

将显示抛出异常注释掉,然后重新启动springboot程序,访问http://127.0.0.1:9999/demo//asset/assign

执行结果如下,分布式事务二阶段成功提交。

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值