本文主要介绍了springboot整合shardingsphere和seata实现分布式事务的实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
各个框架版本信息
- springboot: 2.1.3
- springcloud: Greenwich.RELEASE
- seata: 1.0.0
- shardingsphere:4.0.1
maven 依赖
<dependency>
<!--<groupId>io.shardingsphere</groupId>-->
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
</dependency>
<!--sharding-jdbc 4.0.0 以后版本不能加starter 会导致启动数据源冲突-->
<!--<dependency>-->
<!--<groupId>com.alibaba</groupId>-->
<!--<artifactId>druid-spring-boot-starter</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-transaction-base-seata-at</artifactId>
</dependency>
需要增加的切面类
@Component
@Aspect
@Slf4j
public class SeataTransactionalAspect {
@Before("execution(* com.XXX.dao.*.*(..))")
public void before(JoinPoint joinPoint) throws TransactionException {
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
log.info("拦截到需要分布式事务的方法," + method.getName());
if(StringUtils.startsWithAny(method.getName(),"insert","save"
,"update","delete")){
TransactionTypeHolder.set(TransactionType.BASE);
}
}
}
ProductServiceImpl代码
@Service
public class ProductServiceImpl implements ProductService {
@Resource
UserFeignClient userFeignClient;
@Resource
ProductDao productDao;
@Override
@GlobalTransactional(name="zhjy-product-tx-group",rollbackFor = Exception.class)
public void createProduct(Product product) {
productDao.insertProduct(product);
ProductDesc proDesc = new ProductDesc();
proDesc.setProductDesc(product.getProductDesc());
proDesc.setStoreId(product.getStoreId());
proDesc.setProductId(product.getProductId());
productDao.insertProductDesc(proDesc);
// if(product.getProductName().endsWith("5")){
// int i = 1/0;
// }
// int j = 1/0;
User user = new User();
user.setAge(product.getPrice().intValue());
user.setUserName(product.getProductName());
user.setRealName(product.getProductName());
String msg = userFeignClient.saveUser(user);
//由于开启了服务调用降级,所以需要统一返回错误码,根据错误码主动抛出异常,让seata回滚事务
if(msg.equals("新增用户失败")){
int i = 1/0;
}
}
}
UserFeignClient代码
@FeignClient(name="service-user",fallbackFactory =UserFeignClientFallbackFactory.class)
public interface UserFeignClient {
@GetMapping("/user/getUserById")
@ResponseBody
String getUserById(@RequestParam("id") Integer id);
@GetMapping("/user/getUserByIdWithPathVariable/{id}")
@ResponseBody
String getUserByIdWithPathVariable(@PathVariable("id") Integer id);
@PostMapping("/user/saveUser")
@ResponseBody
String saveUser(@RequestBody User user );
}
User服务 UserController代码
@RestController
@Slf4j
@RefreshScope
public class UserController {
@Autowired
UserService userService;
@PostMapping("/user/saveUser")
@ResponseBody
public String saveUser(@RequestBody User user) {
userService.saveUser(user.getUserName(),user.getRealName(),user.getAge());
// if(user.getAge()>5){
int i = 1/0;
// }
return "sucess";
}
}
UserServiceImpl代码
@Service
public class UserServiceImpl implements UserService {
@Resource
UserDao userDao;
@Override
public void saveUser(String userName, String realName, Integer age) {
userDao.insertUser(userName,realName,age);
}
}
到此这篇关于springboot整合shardingsphere和seata实现分布式事务的实践的文章就介绍到这了,更多相关springboot 分布式事务内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
织梦狗教程
本文标题为:springboot整合shardingsphere和seata实现分布式事务的实践


基础教程推荐
猜你喜欢
- Java File类的概述及常用方法使用详解 2023-05-18
- Project Reactor源码解析publishOn使用示例 2023-04-12
- Java去掉小数点后面无效0的方案与建议 2023-02-18
- Java使用EasyExcel进行单元格合并的问题详解 2023-01-18
- 用java实现扫雷游戏 2022-12-06
- JVM分析之类加载机制详解 2023-04-06
- 全局记录Feign的请求和响应日志方式 2023-01-09
- SpringBoot配置文件中密码属性加密的实现 2023-03-11
- 工厂方法在Spring框架中的运用 2023-06-23
- 一文了解Java 线程池的正确使用姿势 2023-06-17