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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot實現JWT動態(tài)密鑰輪換的示例詳解
這篇文章主要為大家詳細介紹了SpringBoot實現JWT動態(tài)密鑰輪換的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2025-10-10
Tomcat啟動分析(我們?yōu)槭裁匆渲肅ATALINA_HOME環(huán)境變量)
本文主要介紹Tomcat啟動分析的知識,這里整理了相關資料及分析原因和如何實現的方法,有興趣的小伙伴可以參考下2016-09-09
Netty分布式從recycler對象回收站獲取對象過程剖析
這篇文章主要為大家介紹了Netty分布式從recycler獲取對象的過程源碼剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03
Springboot 使用 JSR 303 對 Controller 控制層校驗及 Service 服務層 AOP 校驗
這篇文章主要介紹了Springboot 使用 JSR 303 對 Controller 控制層校驗及 Service 服務層 AOP 校驗 使用消息資源文件對消息國際化的相關知識,需要的朋友可以參考下2017-12-12

