springboot整合shardingsphere和seata實(shí)現(xiàn)分布式事務(wù)的實(shí)踐
各個(gè)框架版本信息
- 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 會(huì)導(dǎo)致啟動(dòng)數(shù)據(jù)源沖突--> ? ? <!--<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("攔截到需要分布式事務(wù)的方法," + 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); ? ? ? ? //由于開啟了服務(wù)調(diào)用降級(jí),所以需要統(tǒng)一返回錯(cuò)誤碼,根據(jù)錯(cuò)誤碼主動(dòng)拋出異常,讓seata回滾事務(wù) ? ? ? ? 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服務(wù) 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); ? ? } }
到此這篇關(guān)于springboot整合shardingsphere和seata實(shí)現(xiàn)分布式事務(wù)的實(shí)踐的文章就介紹到這了,更多相關(guān)springboot 分布式事務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解spring cloud config整合gitlab搭建分布式的配置中心
這篇文章主要介紹了詳解spring cloud config整合gitlab搭建分布式的配置中心,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)實(shí)例
這篇文章主要給大家介紹了關(guān)于JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)的相關(guān)資料,在實(shí)際開發(fā)中經(jīng)常會(huì)遇到需要找出(刪除)一個(gè)list中某些元素的屬性相同的元素,需要的朋友可以參考下2023-07-07Spring Boot中進(jìn)行 文件上傳和 文件下載功能實(shí)現(xiàn)
開發(fā)Wb應(yīng)用時(shí),文件上傳是很常見(jiàn)的一個(gè)需求,瀏覽器 通過(guò) 表單形式 將 文件 以 流的形式傳遞 給 服務(wù)器,服務(wù)器再對(duì)上傳的數(shù)據(jù)解析處理,下面將通過(guò)一個(gè)案例講解使用 SpringBoot 實(shí)現(xiàn) 文件上傳,感興趣的朋友一起看看吧2024-07-07Java Socket+mysql實(shí)現(xiàn)簡(jiǎn)易文件上傳器的代碼
最近在做一個(gè)小項(xiàng)目,項(xiàng)目主要需求是實(shí)現(xiàn)一個(gè)文件上傳器,通過(guò)客戶端的登陸,把本地文件上傳到服務(wù)器的數(shù)據(jù)庫(kù)(本地的)。下面通過(guò)本文給大家分享下實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2016-10-10tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例
這篇文章主要為大家介紹了tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Java使用設(shè)計(jì)模式中迭代器模式構(gòu)建項(xiàng)目的代碼結(jié)構(gòu)示例
這篇文章主要介紹了Java使用設(shè)計(jì)模式中迭代器模式構(gòu)建項(xiàng)目的代碼結(jié)構(gòu)示例,迭代器模式能夠?qū)υL問(wèn)者隱藏對(duì)象的內(nèi)部細(xì)節(jié),需要的朋友可以參考下2016-05-05使用jpa之動(dòng)態(tài)插入與修改(重寫save)
這篇文章主要介紹了使用jpa之動(dòng)態(tài)插入與修改(重寫save),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11