springboot整合shardingsphere和seata實現分布式事務的實踐
更新時間:2022年07月26日 10:46:02 作者:「已注銷」
本文主要介紹了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 分布式事務內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解spring cloud config整合gitlab搭建分布式的配置中心
這篇文章主要介紹了詳解spring cloud config整合gitlab搭建分布式的配置中心,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01tio-boot整合hotswap-classloader實現熱加載方法實例
這篇文章主要為大家介紹了tio-boot整合hotswap-classloader實現熱加載方法實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12