本文主要介绍了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 分布式事务内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:springboot整合shardingsphere和seata实现分布式事务的实践
基础教程推荐
猜你喜欢
- Java实现线程插队的示例代码 2022-09-03
- java实现多人聊天系统 2023-05-19
- Java实现查找文件和替换文件内容 2023-04-06
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- Java并发编程进阶之线程控制篇 2023-03-07
- springboot自定义starter方法及注解实例 2023-03-31
- Java文件管理操作的知识点整理 2023-05-19
- Java数据结构之对象比较详解 2023-03-07
- java基础知识之FileInputStream流的使用 2023-08-11
- JDK数组阻塞队列源码深入分析总结 2023-04-18