springboot整合shardingsphere和seata实现分布式事务的实践

 更新时间:2022年7月26日 13:42  点击:524 作者:「已注销」

各个框架版本信息

  • 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 分布式事务内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://blog.csdn.net/weixin_47574208/article/details/105948

[!--infotagslink--]

相关文章

  • 解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题

    这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • 详解springBoot启动时找不到或无法加载主类解决办法

    这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • 解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • Spring Boot项目@RestController使用重定向redirect方式

    这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02
  • Springboot+TCP监听服务器搭建过程图解

    这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • 详解SpringBoot之访问静态资源(webapp...)

    这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-14
  • SpringBoot接口接收json参数解析

    这篇文章主要介绍了SpringBoot接口接收json参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-19
  • springboot中使用@Transactional注解事物不生效的坑

    这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • ShardingSphere jdbc集成多数据源的实现步骤

    本文主要介绍了ShardingSphere jdbc集成多数据源的实现步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-21
  • springboot多模块包扫描问题的解决方法

    这篇文章主要介绍了springboot多模块包扫描问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解

    这篇文章主要介绍了Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-18
  • Springboot+MDC+traceId日志中打印唯一traceId

    本文主要介绍了Springboot+MDC+traceId日志中打印唯一traceId,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-17
  • Springboot实现多线程注入bean的工具类操作

    这篇文章主要介绍了Springboot实现多线程注入bean的工具类操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-27
  • SpringBoot部署到Linux读取resources下的文件及遇到的坑

    本文主要给大家介绍SpringBoot部署到Linux读取resources下的文件,在平时业务开发过程中,很多朋友在获取到文件内容乱码或者文件读取不到的问题,今天给大家分享小编遇到的坑及处理方案,感兴趣的朋友跟随小编一起看看吧...2021-06-21
  • 关于springboot中nacos动态路由的配置

    这篇文章主要介绍了springboot中nacos动态路由的配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-11
  • SpringBoot高版本修改为低版本时测试类报错的解决方案

    这篇文章主要介绍了SpringBoot高版本修改为低版本时测试类报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-18
  • 解决Springboot整合shiro时静态资源被拦截的问题

    这篇文章主要介绍了解决Springboot整合shiro时静态资源被拦截的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-26