springboot 分布式事务
「已注销」 人气:0各个框架版本信息
- 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); } }
加载全部内容