ps:springboot版本2.5.3
1、下载、安装、启动zookeeper(window):
https://zookeeper.apache.org/releases.html
旧版的下载地址:http://archive.apache.org/dist/zookeeper/
我这里使用的是最新版3.7.0
启动zookeeper服务:
1、复制zoo_sample.cfg重命名为zoo.cfg,根目录下创建data文件夹
2、修改配置文件zoo.cfg
dataDir=*****\\zookeeper-3.7.0\\data
3、bin目录下点击zkServer.cmd启动
4、可以通过启动客户端(点击zkEnv.cmd启动)检查是否正常启动
2、下载、启动seata:
http://seata.io/zh-cn/blog/download.html
我这里使用的是最新版1.4.2(seata-server-1.4.2)
启动seata服务(我这里使用的是db):
1、修改registry.conf
修改registry下type="zk",并注意serverAddr
注意config下的type="file"不用修改,指向下面的file.conf文件
2、修改file.conf
修改store下的db中的数据库访问路径、账号及密码
3、进入数据库创建表undo_log
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
4、bin目录下点击seata-server.bat启动seata
3、接下来直接上代码
0、补充父级pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.3
com.style.lanmao
parent
0.0.1-SNAPSHOT
parent
Demo project for Spring Boot
pom
1.8
agent
dao
service
merchant
component
common
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
dev
dev
daodev
componentdev
true
test
test
daotest
componenttest
pro
pro
daopro
componentpro
1、接下来是服务消费的工程的maven依赖、seata配置、测试用的类
4.0.0
com.style.lanmao
parent
0.0.1-SNAPSHOT
../pom.xml
com.style.lanmao
agent
0.0.1-SNAPSHOT
agent
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
io.seata
seata-spring-boot-starter
1.4.2
com.101tec
zkclient
0.11
org.slf4j
slf4j-log4j12
org.apache.dubbo
dubbo-dependencies-zookeeper
2.7.12
pom
org.slf4j
slf4j-log4j12
org.apache.dubbo
dubbo-spring-boot-starter
2.7.12
com.style.lanmao
service
0.0.1-SNAPSHOT
com.style.lanmao
component
0.0.1-SNAPSHOT
dubbo-agent
org.springframework.boot
spring-boot-maven-plugin
true
seata:
enabled: true
application-id: applicationName
tx-service-group: default
service:
vgroupMapping:
default: default
grouplist:
default: 127.0.0.1:8091
@Controller
@RequestMapping("/open")
public class OpenController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private TestService1 testService;
@RequestMapping("/test")
@ResponseBody
public Object test(HttpServletRequest request) throws Exception{
testService.test();
return null;
}
}
@Component
public class TestService1 {
@DubboReference(group = "TestMServiceImpl")
private TestService testService;
// seata注解
@GlobalTransactional(rollbackFor = Exception.class)
public String test() throws Exception{
System.out.println("TestService1");
testService.test();
throw new Exception("test exception");
// return null;
}
}
2、接下来服务提供者的工程的maven依赖、seata配置、测试代码
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.apache.dubbo
dubbo-spring-boot-starter
2.7.12
org.apache.dubbo
dubbo-dependencies-zookeeper
2.7.12
pom
org.slf4j
slf4j-log4j12
io.seata
seata-spring-boot-starter
1.4.2
com.101tec
zkclient
0.11
org.slf4j
slf4j-log4j12
seata:
enabled: true
application-id: applicationName
tx-service-group: default
service:
vgroupMapping:
default: default
grouplist:
default: 127.0.0.1:8091
@Component
@DubboService(group = "TestMServiceImpl")
public class TestMServiceImpl implements TestService {
@Autowired
CustAccountModelMapper custAccountModelMapper;
@Override
public String test() throws Exception{
System.out.println("TestMServiceImpl test------------->开始");
try{
CustAccountModelExample example = new CustAccountModelExample();
example.createCriteria().andAccountIdEqualTo(100001l);
CustAccountModel model = new CustAccountModel();
model.setBalance(new BigDecimal(11));
int res = custAccountModelMapper.updateByExampleSelective(model, example);
if (res>0){
System.out.println("修改成功");
}else{
System.out.println("修改不成功");
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println("TestMServiceImpl test------------->结束");
return null;
}
@Override
public String test1() {
return null;
}
@Override
public String test2() {
return null;
}
@Override
public String test3() {
return null;
}
}
分别启动服务提供工程、服务消费工程,通过postman测试是可以看到是可以回滚的
5、访问/open/test,服务消费者日志
6、访问/open/test,服务提供者日志
最后的结果:数据库对应数据没有变化
通过在TestService1中test()抛异常前加断点,可知:
1、数据库修改的对应数据有变化,且undo_log表中有新增数据,回滚后又被清
5、遇到一些问题暂时还未解决,后期一步步再确认:
1、配置文件中seata.tx-service-group的值必须为default,seata.service.vgroupMapping.default的值必须为default,否则测试报错不会回滚
2、seata.service.vgroupMapping后面的default这个参数是否是固定的也不确定