springboot整合Atomikos的示例详解
目录
- 什么是Atomikos
- 实战
- 创建springBoot项目,pom.XML文件如下
- 配置多数据源
- 多数据源配置类
- Atomikos配置
- dao层
- Atomikos有什么缺点吗?
- 1:单点问题
- 2:资源锁定问题
- 3:性能瓶颈问题
- 4:数据不一致
什么是Atomikos
Atomikos是一个开源的事务管理器,用于管理和控制分布式事务的执行流程,提供了可靠的,高性能的事务管理解决方案,可以与多种应用和数据库集成
实战
创建springBoot项目,pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVerwww.devze.comsion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.14</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xpc</groupId> <artifactId>distribute-transaction</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jta-atomikos</artifactId> </dependency> <dependency> <groupId>org.myBATis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-Java</artifactId> <version>8.0.22</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter-test</artifactId> <version>2.3.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <http://www.devze.com;plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <http://www.devze.comexclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
配置多数据源
server.port=8080 #数据源test spring.test-datasource.driverClassName = com.mysql.jdbc.Driver spring.test-datasource.jdbc-url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowpublicKeyRetrieval=true spring.test-datasource.username = root spring.test-datasource.password = 12345 #数据源cyp spring.cyp-datasource.driverClassName = com.mysql.jdbc.Driver spring.cyp-datasource.jdbc-url = jdbc:mysql://localhost:3306/test_1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true spring.cyp-datasource.username = root spring.cyp-datasource.password = 12345 logging.level.com.atomikos = debug
多数据源配置类
//配置扫描对应的dao层的包 @MapperScan(basePackages = "com.xpc.dao", sqlSessionFactoryRef = "testSqlSessionFactory") @Configuration public class TestMyBatisConfig { @SneakyThrows @Bean public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean.getObject(); } }
package com.xpc.config; import lombok.SneakyThrows; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.xpc.dao1",sqlSessionFactoryRef = "cypSqlSessionFactory") public class CypMyBatisConfig { @SneakyThrows @Bean public SqlSessionFactory cypSqlSessionFactory(@Qualifier("cypDataSource") DataSource dataSource) { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean.getObject(); } }
Atomikos配置
import com.atomikos.jdbc.AtomikosDataSourceBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; import java.util.Properties; @Configuration public class AtomikosDataSourceConfig { @Value("${spring.test-datasource.jdbc-url}") private String testUrl; @Value("${spring.test-datasource.username}") private String testUser; @Value("${spring.test-datasource.password}") private String testPassword; @Value("${spring.cyp-datasource.jdbc-url}") private String cypUrl; @Value("${spring.cyp-datasource.username}") private String cypUser; @Value("${spring.cyp-datasource.password}") private String cypPassword; @Bean(name = "testDataSource") public DataSource testDataSource() { AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean(); //设置唯一资源name atomikosDataSourceBean.setUniqueResourceName("testDataSource"); atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource"); Properties properties = new Properties(); properties.setProperty("URL", testUrl); properties.setProperty("user", testUser); properties.setProperty("password", testPassword); atomikosDataSourceBean.setXaProperties(properties); return atomikosDataSourceBean; } @Bean(name = "cypDataSource") public DataSource cypDataSource() { AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean(); //设置唯一资源name atomikosDataSourceBean.setUniqueResourceName("cypDataSource"); atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource"); Properties properties = new Properties(); properties.setProperty("URL", cypUrl); properties.setProperty("user", cypUser); properties.setProperty("password", cypPassword); atomikosDataSourceBean.setXaProperties(properties); return atomikosDataSourceBean; } }
import org.springframework.context.annotation.Configuration; import com.atomikos.icatch.jta.UserTransactionImp; import com.atomikos.icatch.jta.UserTransactionManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.DependsOn; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.jta.JtaTransactionManager; import javax.transaction.TransactionManager; import javax.transaction.UserTransaction; @Configuration public class AtomikosConfig { @Bean(name = "userTransaction") public UserTransaction userTransaction() { return new UserTransactionImp(); } @Bean(name = "atomikosTransactionManager") public TransactionManager atomikosTransactionManager() { return new UserTransactionManager(); } @Bean(name = "platformTransactionManager") @DependsOn({"userTransaction", "atomikosTransactionManager"}) public PlatformTransactionManager transactionManager() { UserTransaction userTransaction = userTransaction(); TransactionManaghttp://www.devze.comer transactionManager = atomikosTransactionManager(); return new JtaTransactionManager(userTransaction, transactionManager); } }
dhttp://www.devze.comao层
@Mapper public interface TestDao { @Insert("insert into t_xpc (name) values (#{name})") void insert(Test1 test1); }
@Mapper public interface CypDao { @Insert("insert into t_cyp (name) values (#{name})") void insert(Tcyp tcyp); }
接下来就是service业务了
@Service public class XpcService { @Resource private TestDao testDao; @Resource private CypDao cypDao; @Transactional(rollbackFor = Exception.class) public String test() { Test1 test1 = new Test1(); test1.setName("xpc-5"); testDao.insert(test1); Tcyp tcyp = new Tcyp(); tcyp.setName("xpc-5"); cypDao.insert(tcyp); int i = 1/0; return "success"; } }
到这里springboot整合Atomikos就完成了,然后我们可以调用下该test方法,因为 int i = 1/0; 会抛出异常,所以这两条数据都不会插入成功,也就意味着事务成功回滚了
Atomikos有什么缺点吗?
Atomiko提供了两阶段提交,那么两阶段提交会有什么问题?
1:单点问题
事务管理器是直接集成在我们服务中的,也就是java进程中的,如果这时候这个服务挂了,那么整个分布式事务就都不可用了
2:资源锁定问题
在事务第一阶段的时候,我们的资源管理器RM就会锁定一些资源,这时候,其它事务进来就无法再次锁定同样的资源了,也就造成了阻塞的问题
如果这时候碰到了单点问题,导致这些资源释放不掉,那么其它事务就再也拿不到这些资源了
3:性能瓶颈问题
事务管理器在发起第一阶段的时候,必须等到所有的资源管理器都返回OK了,才会发起第二阶段commit,如果这时候有一个资源管理在第一阶段迟迟没有返回OK,那么事务管理器就会一直阻塞在这里
4:数据不一致
假设现在事务管理器(TM)收到所有资源管理器(RM)的第一阶段OK响应了,这时候就会发起第二阶段的commit了,但是这时候由于某些原因,导致某个资源管理器(RM)没有收到commit,这时候其它的资源管理器(RM)已经提交了,但是这个资源管理器(RM)还是没有提交事务的,这时候就造成了数据不一致的问题了
以上就是springboot整合Atomikos的示例详解的详细内容,更多关于springboot整合Atomikos的资料请关注编程客栈(www.devze.com)其它相关文章!
精彩评论