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);
? ? ? ? //由于開(kāi)啟了服務(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)文章
Mybatis 中的sql批量修改方法實(shí)現(xiàn)
在項(xiàng)目中遇到需要批量更新的功能,原本想的是在Java中用循環(huán)訪問(wèn)數(shù)據(jù)庫(kù)去更新,但是心里總覺(jué)得這樣做會(huì)不會(huì)太頻繁了,太耗費(fèi)資源了,效率也很低,查了下mybatis的批量操作,原來(lái)確實(shí)有<foreach>標(biāo)簽可以做到,下面通過(guò)本文給大家介紹下2017-01-01
SpringBoot實(shí)現(xiàn)JWT動(dòng)態(tài)密鑰輪換的示例詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)JWT動(dòng)態(tài)密鑰輪換的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-10-10
Tomcat啟動(dòng)分析(我們?yōu)槭裁匆渲肅ATALINA_HOME環(huán)境變量)
本文主要介紹Tomcat啟動(dòng)分析的知識(shí),這里整理了相關(guān)資料及分析原因和如何實(shí)現(xiàn)的方法,有興趣的小伙伴可以參考下2016-09-09
Java如何接收XML格式參數(shù)并轉(zhuǎn)換為JSON
在 Java 應(yīng)用程序中,處理 XML 數(shù)據(jù)并將其轉(zhuǎn)換為 JSON 格式是很常見(jiàn)的任務(wù),這篇文章為大家整理了一下具體的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助2025-03-03
Netty分布式從recycler對(duì)象回收站獲取對(duì)象過(guò)程剖析
這篇文章主要為大家介紹了Netty分布式從recycler獲取對(duì)象的過(guò)程源碼剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
java實(shí)現(xiàn)從方法返回多個(gè)值功能示例
這篇文章主要介紹了java實(shí)現(xiàn)從方法返回多個(gè)值功能,結(jié)合實(shí)例形式分析了集合類、封裝對(duì)象、引用傳遞三種實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
Springboot 使用 JSR 303 對(duì) Controller 控制層校驗(yàn)及 Service 服務(wù)層 AOP 校驗(yàn)
這篇文章主要介紹了Springboot 使用 JSR 303 對(duì) Controller 控制層校驗(yàn)及 Service 服務(wù)層 AOP 校驗(yàn) 使用消息資源文件對(duì)消息國(guó)際化的相關(guān)知識(shí),需要的朋友可以參考下2017-12-12

