springboot整合shardingsphere和seata实现分布式事务的实践
作者:「已注销」
本文主要介绍了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 分布式事务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!