目录
1.2 zk做为配置中心 :导入seata-server配置
运行实例: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
执行结果如下,分布式事务二阶段成功提交。