美文网首页SpringB...
Springboot通过AOP整合多数据源,分析@Transac

Springboot通过AOP整合多数据源,分析@Transac

作者: TTTTTriM | 来源:发表于2019-02-22 09:49 被阅读0次

Springboot通过AOP整合多数据源原理

关于数据源的控制,在Spring中留了AbstractRoutingDataSource接口供使用者控制,其determineCurrentLookupKey方法关联对应的datasource即可。

AbstractRoutingDataSource

Abstract javax.sql.DataSource implementation that routes getConnection() calls to one of various target DataSources based on a lookup key.The latter is usually (but not necessarily) determined through some thread-bound transaction context.

将数据源放入Spring容器中管理,通过@Configuration @Bean的定义将datasource1,datasource2加入容器中
并且在每个线程中定义ThreadLocal,控制每个线程的在决定使用哪个数据源时不产生冲突 (记住必须在使用用对ThreadLocal进行clear,不然重复使用线程时有可能导致获取到旧的数据源配置)

Springboot的整合

每次构建工程时如果感觉很多重复工作,建议使用自定义模板偷个懒!地址飞过去!

pom.xml引入mybatis
<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">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hugui</groupId>
    <artifactId>springboot-mutil-database</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-mutil-database</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <dependencies>
        <!-- spring mvc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        
        <!-- mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <!-- lombok util -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <finalName>springboot-mutil-database</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-archetype-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml定义
mybatis:
  mapper-locations: classpath:com/hugui/*/mapper/*/*.xml
  
server:
  port: 8080
spring:
  datasource:
    db1:
      driver-class-name: com.mysql.cj.jdbc.Driver
      initial-size: 5
      max-idle: 10
      max-wait: 10000
      min-idle: 5
      password: 1234
      url: jdbc:mysql://127.0.0.1:3306/user?characterEncoding=utf-8&serverTimezone=GMT&useSSL=false
      username: root
    db2:
      driver-class-name: com.mysql.cj.jdbc.Driver
      initial-size: 5
      max-idle: 10
      max-wait: 10000
      min-idle: 5
      password: 1234
      url: jdbc:mysql://127.0.0.1:3306/user1?characterEncoding=utf-8&serverTimezone=GMT&useSSL=false
      username: root
定义启动类

注意要禁用数据源的自动配置类DataSourceAutoConfiguration

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class SpringbootMutiDatabaseApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMutiDatabaseApplication.class, args);
    }

}
自定义多数据源配置类
@Configuration
public class DatabaseConfig {

    @Bean(name = "datasource1")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "datasource2")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

    
    /**
     * 动态数据源
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Primary
    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource() {
        DynamicDatabase dynamicDataSource = new DynamicDatabase();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(dataSource1());
        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap();
        dsMap.put("datasource1", dataSource1());
        dsMap.put("datasource2", dataSource2());

        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }

}
通过ThreadLocal定义上下文数据源标识
public class DatabaseHolder {

    // ThreadLocal每个线程都独有的保存其线程所属的变量值
    private static ThreadLocal<String> holder = new ThreadLocal<>();

    public static void setDatabaseSource(String ds) {
        holder.set(ds);
    }

    public static String getDatabaseSource() {
        return holder.get();
    }

    public static void clearDatabaseSource() {
        holder.remove();
    }
}
继承AbstractRoutingDataSource定义数据源标识
public class DynamicDatabase extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DatabaseHolder.getDatabaseSource();
    }

}
多数据源注解MyDataSource
@Target(ElementType.METHOD)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface MyDataSource {

    String value() default "datasource1";

}
定义MyDataSource注解切片类
@Aspect
@Component
@Order(1)
public class DatasourceAspect {

    @Pointcut("@annotation(com.hugui.mutildatabase.annotation.MyDataSource)")
    public void annotationPointCut() {
        // APSECT POINT CUT FUNCTION
    }

    @Before(value = "annotationPointCut()&&@annotation(datasource)")
    public void beforeSwitchDS(JoinPoint point, MyDataSource datasource) {
        DatabaseHolder.setDatabaseSource(datasource.value());
    }

    /**
     * 
     * AOP数据源调用 
     * repository@Annotation(AOP)-->
     * DefaultSqlSession-->
     * SimpleExecutor-->
     * BaseExecutor.getConnection()-->
     * SpringManagedTransaction.getConnection()--->连接为空-->
     * AbstractRoutingDataSource.getConnection()-->
     * 拿到beforeAOP中注入的datasource的key,所以每次都会动态切换数据源
     * 
     * 数据库事务调用 service注解上@transactional-->
     * TransactionInterceptor.interpter()-->
     * TransactionAspectSupport.createTransactionIfNecessary()-->
     * AbstractPlatformTransactionManager.getTransaction()-->
     * DataSourceTransactionManager.doBegin()-->
     * AbstractRoutingDataSource.determineTargetDataSource()[lookupKey==null去拿默认的Datasource,不为空则使用获取到的连接]-->
     * DataSourceTransactionManager.setTransactional()[将连接设置到TransactionUtils的threadLocal中]--->
     * Repository@Annotation-->执行AOP数据源调用链,
     * 问题在于SpringManagedTransaction.getConnection()-->openConnection()-->
     * DataSourceUtils.getConnection()-->
     * TransactionSynchronizationManager.getResource(dataSource)不为空[从TransactionUtils的threadLocal中获取数据源],
     * 所以不会再去调用DynamicDataSource去获取数据源
     * 
     * ① 加入Transaction注解时默认是先执行transaction的doBegin,然后再去找AbstractRoutingDataSource.determineTargetDataSource发现是null
     * 然后就会设置默认的数据源,所以必须在执行transaction的doBegin前通过AOP把切换的数据源进行注入,所以在本过滤器中定义Order的优先级为1
     * ② 在每次切换数据源之后再次清除ThreadLocal中的数据源定义,防止线程重复使用时获取旧的数据源定义。
     * @param point
     */

    @After(value = "annotationPointCut()")
    public void afterSwitchDS(JoinPoint point) {
        DatabaseHolder.clearDatabaseSource();
    }
}
定义Mybatis Mapper、Service、Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class User {

    private Long id;
    private String password;
    private String username;

}

@Mapper
public interface UserMapper {

    List<User> findAll();

    Long add(@Param("password") String password, @Param("username") String username);
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hugui.mutildatabase.mapper.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap"
        type="com.hugui.mutildatabase.entity.User">
        <id column="id" property="id" />
        <result column="password" property="password" />
        <result column="username" property="username" />
    </resultMap>

    <select id="findAll" resultMap="BaseResultMap">
        select * from user
    </select>

    <insert id="add" useGeneratedKeys="true">
        insert into
        user(password,username) values (#{password},#{username})
    </insert>
</mapper>

public interface IUserService{

    List<User> findAll1();
    
    List<User> findAll2();
    
    Long add1(String password , String username);
    
    Long add2(String password , String username);
}


@Service
public class UseServiceImpl implements IUserService {

    @Autowired
    private UserMapper mapper;

    @MyDataSource("datasource2")
    @Transactional
    @Override
    public List<User> findAll2() {
        return mapper.findAll();
    }

    @MyDataSource
    @Transactional
    @Override
    public List<User> findAll1() {
        return mapper.findAll();
    }

    @SuppressWarnings("unused")
    @MyDataSource("datasource2")
    @Transactional
    @Override
    public Long add2(String password, String username) {
        Long id = mapper.add(password, username);
        throw new RuntimeErrorException(new Error("error!!!!!"));
    }

    @SuppressWarnings("unused")
    @MyDataSource
    @Override
    public Long add1(String password, String username) {
        Long id = mapper.add(password, username);
        throw new RuntimeErrorException(new Error("error!!!!!"));
    }
}

问题

加入@Transaction后多数据源切换失败?先看下文的调用过程
调用过程
无Transaction的AOP调用过程

repository@Annotation(AOP)-->
DefaultSqlSession-->
SimpleExecutor-->
BaseExecutor.getConnection()-->SpringManagedTransaction.getConnection()--->连接为空-->
AbstractRoutingDataSource.getConnection()-->
拿到beforeAOP中注入的datasource的key,所以每次都会动态切换数据源

有Transaction的AOP调用过程

TransactionInterceptor.interpter()-->
TransactionAspectSupport.createTransactionIfNecessary()-->
AbstractPlatformTransactionManager.getTransaction()-->
DataSourceTransactionManager.doBegin()-->
AbstractRoutingDataSource.determineTargetDataSource()[lookupKey==null去拿默认的Datasource,不为空则使用获取到的连接]-->
DataSourceTransactionManager.setTransactional()[将连接设置到TransactionUtils的threadLocal中]--->
Repository@Annotation-->执行AOP数据源调用链,
问题在于SpringManagedTransaction.getConnection()-->openConnection()-->
DataSourceUtils.getConnection()-->
TransactionSynchronizationManager.getResource(dataSource)不为空[从TransactionUtils的threadLocal中获取数据源],
这样发现不会再去调用DynamicDataSource去获取配置的动态数据源

解决办法是:

在transaction interpter执行之前就把动态数据源配置好,所以在动态数据源的配置的AOP切片上加入Order(1),让其先执行即可。

完整代码

Git路径:https://github.com/TrimGHU/springboot-mutil-database

启动服务测试多数据源以及@Transaction

正常访问
数据源1
数据源2
事务访问

数据源1未加事务


数据源1添加用户强制出错
数据源1数据添加成功

数据源2加了事务


数据源2添加用户强制出错
数据源2数据正常回滚

相关文章

网友评论

    本文标题:Springboot通过AOP整合多数据源,分析@Transac

    本文链接:https://www.haomeiwen.com/subject/ylqdyqtx.html