fast-family框架 fast-family-datasource多数据源以及整合xa分布式事务使用案例

介绍

fast-family-datasource是fast-family框架中多数据源处理模块,提供XA分布式事务处理,注解指定数据源,简单的配置文件指向即可完成多数据源及事务管理。

注意:暂时只支持druid数据源

使用案例

引入模块:


   com.github.fast-family
   fast-family-datasource
   0.1.0

注意:暂时未发布到中央仓库,需下载代码

不带XA事务配置:

fast:
  family:
    type: com.alibaba.druid.pool.DruidDataSource
    datasource:
      master:
        druid:
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/master-db?useUnicode=true&characterEncoding=utf8
          driverClassName: com.mysql.jdbc.Driver
        atomikos:
          uniqueResourceName: master-db
      slave_0:
        druid:
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/slave-db?useUnicode=true&characterEncoding=utf8
          driverClassName: com.mysql.jdbc.Driver
        atomikos:
          uniqueResourceName: slave-db

携带XA事务配置:

fast:
  family:
    type: com.alibaba.druid.pool.xa.DruidXADataSource
    datasource:
      master:
        druid:
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/master-db?useUnicode=true&characterEncoding=utf8
          driverClassName: com.mysql.jdbc.Driver
        atomikos:
          uniqueResourceName: master-db
      slave_0:
        druid:
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/slave-db?useUnicode=true&characterEncoding=utf8
          driverClassName: com.mysql.jdbc.Driver
        atomikos:
          uniqueResourceName: slave-db

示例代码:

通过使用DataSourceAnnotation注解来指定对应的数据源名称

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @DataSourceAnnotation(name = "slave_0")
    @Override
    public List selectAlldb1() {
        return userDao.selectAll();
    }

    @DataSourceAnnotation(name = "master")
    @Override
    public List selectAlldb2() {
        return userDao.selectAll();
    }

    @DataSourceAnnotation(name = "slave_0")
    @Override
    public void savedb1() {
        if (true){
            throw new RuntimeException("异常了");
        }
    }

    @DataSourceAnnotation(name = "master")
    @Override
    public void savedb2() {
        User user = new User();
        user.setPassword("1231");
        user.setUsername("1231232");
        userDao.insert(user);
    }
}

测试代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class FastFamilyDatasourceExampleApplicationTests {
    

    @Autowired
    private UserService userService;

    @Test
    public void contextLoads() {****
        System.out.println("查询数据大小:"+userService.selectAlldb1().size());
        System.out.println("查询数据大小:"+userService.selectAlldb2().size());
    }


    @Transactional
    @Test
    public void saveTest(){
        userService.savedb2();
        userService.savedb1();
    }

}

测试效果:

不带事务:

QQ图片20181015165603.png

QQ图片20181015165539.png

带XA事务:

QQ图片20181015165631.png

QQ图片20181015165644.png

如果您对此框架感兴趣或者想获取最新进度信息可以加qq群:390295286
github地址:https://github.com/fast-family/fast-family-master 欢迎star fork
github example地址: https://github.com/fast-family/fast-family-example

你可能感兴趣的:(fast-family框架 fast-family-datasource多数据源以及整合xa分布式事务使用案例)