springboot整合shardingsphere和seata實(shí)現(xiàn)分布式事務(wù)的實(shí)踐
各個框架版本信息
- 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 會導(dǎo)致啟動數(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)用降級,所以需要統(tǒng)一返回錯誤碼,根據(jù)錯誤碼主動拋出異常,讓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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis?連接mysql數(shù)據(jù)庫底層運(yùn)行的原理分析
這篇文章主要介紹了Mybatis?連接mysql數(shù)據(jù)庫底層運(yùn)行的原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
最新IntelliJ?IDEA?2022配置?Tomcat?8.5?的詳細(xì)步驟演示
這篇文章主要介紹了IntelliJ?IDEA?2022?詳細(xì)配置?Tomcat?8.5?步驟演示,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
Java 動態(tài)代理你真的懂了嗎(動態(tài)和代理)
動態(tài)代理分兩部分,動態(tài)和代理,今天通過本文給大家普及代碼模式及動態(tài)代理的概念及示例代碼,感興趣的朋友跟隨小編一起看看吧2021-07-07
zuul轉(zhuǎn)發(fā)后服務(wù)取不到請求路徑的解決
這篇文章主要介紹了zuul轉(zhuǎn)發(fā)后服務(wù)取不到請求路徑的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
java實(shí)現(xiàn)國產(chǎn)sm4加密算法
這篇文章主要介紹了java實(shí)現(xiàn)國產(chǎn)sm4加密算法的步驟,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12

