@Autowired
RestTemplate restTemplate;
/**
* 下单
*
* @return
*/
@Transactional // 开启事务 异常后触发数据库回滚操作
@Override
public Order create(Order order) {
// 插入订单
orderMapper.insert(order);
// 扣减库存
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();paramMap.add("productId", order.getProductId());
String msg = restTemplate.postForObject("http://localhost:8071/stock/reduct", paramMap, String.class);
// 制造异常
int a = 1 / 0;
return order;
}
>> SeataServer的搭建,上一篇已详述,这里就不多介绍了。
<!--nacos-服务注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--1. 添加openfeign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--seata的依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
spring:
datasource:
username: root
password: xxxxx
url: jdbc:mysql://localhost:3307/seata_stock?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#初始化时运行sql脚本
schema: classpath:sql/schema.sql
initialization-mode: never
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8847
username: nacos
password: nacos
alibaba:
seata:
tx-service-group: my_test_tx_group
application:
name: stock-seata
seata:
registry:
# 配置seata的注册中心, 告诉seata client 怎么去访问seata server(TC) type: nacos
nacos:
server-addr: 127.0.0.1:8847 # seata server 所在的nacos服务地址
application: seata-server # seata server 的服务名seata-server ,如果没有修改可以不配
username: nacos
password: nacos
group: SEATA_GROUP # seata server 所在的组,默认就是SEATA_GROUP,没有改也可以不配
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8847
username: nacos
password: nacos
group: SEATA_GROUP
>> 启动类增加 @EnableFeignClients 注解
@FeignClient(value = "stock-seata", path = "/stock")
public interface StockFeignService {
// @RequestMapping("/reduct")
// public String reduct(@RequestParam(value="productId") Integer productId){
// stockService.reduct(productId);
// return "扣减库存";
// }
@RequestMapping("/reduct")
String reduct(@RequestParam(value = "productId") Integer productId);
}
>> @Transactional 变更为 @GlobalTransactional
@Autowired
StockFeignService stockFeignService;
/**
* 下单
* @return
*/
// @Transactional
// @Override
// public Order create(Order order) {
// // 插入订单
// orderMapper.insert(order);
//
//
// // 扣减库存
// MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
// paramMap.add("productId", order.getProductId());
//
// String msg = restTemplate.postForObject("http://localhost:8071/stock/reduct", paramMap,String.class );
//
// // 制造异常
// int a=1/0;
//
// return order;
// }
/**
* 下单
* @return
*/
@GlobalTransactional
@Override
public Order create(Order order) {
// 插入订单
orderMapper.insert(order);
// 扣减库存
stockFeignService.reduct(order.getProductId());
// 制造异常
int a=1/0;
return order;
}
>> 库存表数量为 99 ,订单表记录为空
@GlobalTransactional
@Override
public Order create(Order order) {
// 插入订单
orderMapper.insert(order);
// 扣减库存
stockFeignService.reduct(order.getProductId());
// 制造异常
// int a=1/0;
return order;
}
微服务框架下,分布式事务的控制已多见不怪了,Seata组件就是其中的一种解决方案;本文介绍了Seata客户端使用,希望对你有所帮助!!!