`
dkload
  • 浏览: 8588 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

springboot结合atomikos+mybaits实现分布式事务及读写分离【最简配置】

 
阅读更多
一,读写分离思路:
将不同库的读写操作对应的mapper分别放到不同目录中,例如:
ums/r/
ums/rw/
bap/r/
bap/rw/

二,采用atomikos实现分布式事务:
0,gradle依赖:
compile("org.springframework.boot:spring-boot-starter-jta-atomikos")

1,application.yml配置如下:
#数据源配置
spring:
  jta:
    enabled: true
    service: com.atomikos.icatch.standalone.UserTransactionServiceFactory
    max-actives: 200
    enable-logging: false
    atomikos: 
      datasource:
        dataSourceBap:
          unique-resource-name: dataSourceBap
          xa-data-source-class-name: oracle.jdbc.xa.client.OracleXADataSource
          min-pool-size: 20
          max-pool-size: 200
          xa-properties:
            URL: jdbc:oracle:thin:@172.16.xxx.xxx:1521/bapdb
            user: xxx
            password: xxx
        dataSourceBapRW:
          unique-resource-name: dataSourceBapRW
          xa-data-source-class-name: oracle.jdbc.xa.client.OracleXADataSource
          min-pool-size: 20
          max-pool-size: 200
          xa-properties:
            URL: jdbc:oracle:thin:@172.16.xxx.xxx:1521/bapdb
            user: xxx
            password: xxx
        dataSourceUms:
          unique-resource-name: dataSourceUms
          xa-data-source-class-name: oracle.jdbc.xa.client.OracleXADataSource
          min-pool-size: 20
          max-pool-size: 200
          xa-properties:
            URL: jdbc:oracle:thin:@172.16.xxx.xxx:1521/bapdb
            user: xxx
            password: xxx
        dataSourceUmsRW:
          unique-resource-name: dataSourceUmsRW
          xa-data-source-class-name: oracle.jdbc.xa.client.OracleXADataSource
          min-pool-size: 20
          max-pool-size: 200
          xa-properties:
            URL: jdbc:oracle:thin:@172.16.xxx.xxx:1521/bapdb
            user: xxx
            password: xxx
    log-dir: /home/app/sxpservice/logs/xa/
#mybatis配置
mybatis:
  type-aliases-package: ${application.base-package}.domain
  configuration:
    mapUnderscoreToCamelCase: true
    jdbcTypeForNull: VARCHAR
    cacheEnabled: false
    default-statement-timeout: 5


2,java代码:数据源配置及mybaits配置如下:
// bap读写配置bean【@primary】
@Configuration
@MapperScan(basePackages = "com.xxx.ums.mapper.bap.rw", sqlSessionFactoryRef = "sqlSessionFactoryBapRW")
public class BapRWConfig {

	@Primary
	@Bean(name = "dataSourceBapRW", destroyMethod = "close")
	@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.dataSourceBapRW")
	public DataSource dataSourceBap() {
		return new AtomikosDataSourceBean();
	}

	@Primary
	@Bean(name = "sqlSessionFactoryBapRW")
	@ConfigurationProperties(prefix = "mybatis")
	public SqlSessionFactory sqlSessionFactoryBap(@Qualifier("dataSourceBapRW") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean.getObject();
	}
}
// bap读配置bean
@Configuration
@MapperScan(basePackages = "com.xxx.ums.mapper.bap.r", sqlSessionFactoryRef = "sqlSessionFactoryBap")
public class BapRConfig {

	@Bean(name = "dataSourceBap", destroyMethod = "close")
	@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.dataSourceBap")
	public DataSource dataSourceBap() {
		return new AtomikosDataSourceBean();
	}

	@Bean(name = "sqlSessionFactoryBap")
	@ConfigurationProperties(prefix = "mybatis")
	public SqlSessionFactory sqlSessionFactoryBap(@Qualifier("dataSourceBap") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean.getObject();
	}
}


// ums读写配置bean
@Configuration
@MapperScan(basePackages = "com.xxx.ums.mapper.ums.rw", sqlSessionFactoryRef = "sqlSessionFactoryUmsRW")
public class UmsRWConfig {

	@Bean(name = "dataSourceUmsRW", destroyMethod = "close")
	@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.dataSourceUmsRW")
	public DataSource dataSourceUms() {
		return new AtomikosDataSourceBean();
	}

	@Bean(name = "sqlSessionFactoryUmsRW")
	@ConfigurationProperties(prefix = "mybatis")
	public SqlSessionFactory sqlSessionFactoryUms(@Qualifier("dataSourceUmsRW") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean.getObject();
	}
}

// ums读配置bean
@Configuration
@MapperScan(basePackages = "com.xxx.ums.mapper.ums.r", sqlSessionFactoryRef = "sqlSessionFactoryUms")
public class UmsRConfig {

	@Bean(name = "dataSourceUms", destroyMethod = "close")
	@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.dataSourceUms")
	public DataSource dataSourceUms() {
		return new AtomikosDataSourceBean();
	}

	@Bean(name = "sqlSessionFactoryUms")
	@ConfigurationProperties(prefix = "mybatis")
	public SqlSessionFactory sqlSessionFactoryUms(@Qualifier("dataSourceUms") DataSource dataSource, Environment env) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean.getObject();
	}
}


3,测试
编写单元测试,使用@transactional注解测试事务
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics