zoukankan      html  css  js  c++  java
  • 基于注解的springboot+mybatis的多数据源组件的实现

        通常业务开发中,我们会使用到多个数据源,比如,部分数据存在mysql实例中,部分数据是在oracle数据库中,那这时候,项目基于springboot和mybatis,其实只需要配置两个数据源即可,只需要按照

    dataSource - SqlSessionFactory - SqlSessionTemplate配置好就可以了。

    如下代码,首先我们配置一个主数据源,通过@Primary注解标识为一个默认数据源,通过配置文件中的spring.datasource作为数据源配置,生成SqlSessionFactoryBean,最终,配置一个SqlSessionTemplate。

     1 @Configuration
     2 @MapperScan(basePackages = "com.xxx.mysql.mapper", sqlSessionFactoryRef = "primarySqlSessionFactory")
     3 public class PrimaryDataSourceConfig {
     4 
     5     @Bean(name = "primaryDataSource")
     6     @Primary
     7     @ConfigurationProperties(prefix = "spring.datasource")
     8     public DataSource druid() {
     9         return new DruidDataSource();
    10     }
    11 
    12     @Bean(name = "primarySqlSessionFactory")
    13     @Primary
    14     public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
    15         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    16         bean.setDataSource(dataSource);
    17         bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    18         bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    19         return bean.getObject();
    20     }
    21 
    22     @Bean("primarySqlSessionTemplate")
    23     @Primary
    24     public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sessionFactory) {
    25         return new SqlSessionTemplate(sessionFactory);
    26     }
    27 }

    然后,按照相同的流程配置一个基于oracle的数据源,通过注解配置basePackages扫描对应的包,实现特定的包下的mapper接口,使用特定的数据源。

     1 @Configuration
     2 @MapperScan(basePackages = "com.nbclass.oracle.mapper", sqlSessionFactoryRef = "oracleSqlSessionFactory")
     3 public class OracleDataSourceConfig {
     4 
     5     @Bean(name = "oracleDataSource")
     6     @ConfigurationProperties(prefix = "spring.secondary")
     7     public DataSource oracleDruid(){
     8         return new DruidDataSource();
     9     }
    10 
    11     @Bean(name = "oracleSqlSessionFactory")
    12     public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {
    13         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    14         bean.setDataSource(dataSource);
    15         bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:oracle/mapper/*.xml"));
    16         return bean.getObject();
    17     }
    18 
    19     @Bean("oracleSqlSessionTemplate")
    20     public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sessionFactory) {
    21         return new SqlSessionTemplate(sessionFactory);
    22     }
    23 }

    这样,就实现了一个工程下使用多个数据源的功能,对于这种实现方式,其实也足够简单了,但是如果我们的数据库实例有很多,并且每个实例都主从配置,那这里维护起来难免会导致包名过多,不够灵活。

        现在考虑实现一种对业务侵入足够小,并且能够在mapper方法粒度上去支持指定数据源的方案,那自然而然想到了可以通过注解来实现,首先,自定义一个注解@DBKey:

    1 @Retention(RetentionPolicy.RUNTIME)
    2 @Target({ElementType.METHOD, ElementType.TYPE})
    3 public @interface DBKey {
    4 
    5     String DEFAULT = "default"; // 默认数据库节点
    6 
    7     String value() default DEFAULT;
    8 }

    思路和上面基于springboot原生的配置的类似,首先定义一个默认的数据库节点,当mapper接口方法/类没有指定任何注解的时候,默认走这个节点,注解支持传入value参数表示选择的数据源节点名称。至于注解的实现逻辑,可以通过反射来获取mapper接口方法/类的注解值,然后指定特定的数据源。

    那在什么时候执行这个操作获取呢?可以考虑使用spring AOP织入mapper层,在切入点执行具体mapper方法之前,将对应的数据源配置放入threaLocal中,有了这个逻辑,立即动手实现:

    首先,定义一个db配置的上下文对象。维护所有的数据源key实例,以及当前线程使用的数据源key:

     1 public class DBContextHolder {
     2     
     3     private static final ThreadLocal<String> DB_KEY_CONTEXT = new ThreadLocal<>();
     4 
     5     //在app启动时就加载全部数据源,不需要考虑并发
     6     private static Set<String> allDBKeys = new HashSet<>();
     7 
     8     public static String getDBKey() {
     9         return DB_KEY_CONTEXT.get();
    10     }
    11 
    12     public static void setDBKey(String dbKey) {
    13         //key必须在配置中
    14         if (containKey(dbKey)) {
    15             DB_KEY_CONTEXT.set(dbKey);
    16         } else {
    17             throw new KeyNotFoundException("datasource[" + dbKey + "] not found!");
    18         }
    19     }
    20 
    21     public static void addDBKey(String dbKey) {
    22         allDBKeys.add(dbKey);
    23     }
    24 
    25     public static boolean containKey(String dbKey) {
    26         return allDBKeys.contains(dbKey);
    27     }
    28 
    29     public static void clear() {
    30         DB_KEY_CONTEXT.remove();
    31     }
    32 }

    然后,定义切点,在切点before方法中,根据当前mapper接口的@@DBKey注解来选取对应的数据源key:

     1 @Aspect
     2 @Order(Ordered.LOWEST_PRECEDENCE - 1)
     3 public class DSAdvice implements BeforeAdvice {
     4 
     5     @Pointcut("execution(* com.xxx..*.repository.*.*(..))")
     6     public void daoMethod() {
     7     }
     8 
     9     @Before("daoMethod()")
    10     public void beforeDao(JoinPoint point) {
    11         try {
    12             innerBefore(point, false);
    13         } catch (Exception e) {
    14             logger.error("DefaultDSAdviceException",
    15                     "Failed to set database key,please resolve it as soon as possible!", e);
    16         }
    17     }
    18 
    19     /**
    20      * @param isClass 拦截类还是接口
    21      */
    22     public void innerBefore(JoinPoint point, boolean isClass) {
    23         String methodName = point.getSignature().getName();
    24 
    25         Class<?> clazz = getClass(point, isClass);
    26         //使用默认数据源
    27         String dbKey = DBKey.DEFAULT;
    28         Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
    29         Method method = null;
    30         try {
    31             method = clazz.getMethod(methodName, parameterTypes);
    32         } catch (NoSuchMethodException e) {
    33             throw new RuntimeException("can't find " + methodName + " in " + clazz.toString());
    34         }
    35         //方法上存在注解,使用方法定义的datasource
    36         if (method.isAnnotationPresent(DBKey.class)) {
    37             DBKey key = method.getAnnotation(DBKey.class);
    38             dbKey = key.value();
    39         } else {
    40             //方法上不存在注解,使用类上定义的注解
    41             clazz = method.getDeclaringClass();
    42             if (clazz.isAnnotationPresent(DBKey.class)) {
    43                 DBKey key = clazz.getAnnotation(DBKey.class);
    44                 dbKey = key.value();
    45             }
    46         }
    47         DBContextHolder.setDBKey(dbKey);
    48     }
    49 
    50 
    51     private Class<?> getClass(JoinPoint point, boolean isClass) {
    52         Object target = point.getTarget();
    53         String methodName = point.getSignature().getName();
    54 
    55         Class<?> clazz = target.getClass();
    56         if (!isClass) {
    57             Class<?>[] clazzList = target.getClass().getInterfaces();
    58 
    59             if (clazzList == null || clazzList.length == 0) {
    60                 throw new MutiDBException("找不到mapper class,methodName =" + methodName);
    61             }
    62             clazz = clazzList[0];
    63         }
    64 
    65         return clazz;
    66     }
    67 }

    既然在执行mapper之前,该mapper接口最终使用的数据源已经被放入threadLocal中,那么,只需要重写新的路由数据源接口逻辑即可:

     1 public class RoutingDatasource extends AbstractRoutingDataSource {
     2 
     3     @Override
     4     protected Object determineCurrentLookupKey() {
     5         String dbKey = DBContextHolder.getDBKey();
     6         return dbKey;
     7     }
     8 
     9     @Override
    10     public void setTargetDataSources(Map<Object, Object> targetDataSources) {
    11         for (Object key : targetDataSources.keySet()) {
    12             DBContextHolder.addDBKey(String.valueOf(key));
    13         }
    14         super.setTargetDataSources(targetDataSources);
    15         super.afterPropertiesSet();
    16     }
    17 }

    另外,我们在服务启动,配置mybatis的时候,将所有的db配置加载:

     1 @Bean
     2     @ConditionalOnMissingBean(DataSource.class)
     3     @Autowired
     4     public DataSource dataSource(MybatisProperties mybatisProperties) {
     5         Map<Object, Object> dsMap = new HashMap<>(mybatisProperties.getNodes().size());
     6         for (String nodeName : mybatisProperties.getNodes().keySet()) {
     7             dsMap.put(nodeName, buildDataSource(nodeName, mybatisProperties));
     8             DBContextHolder.addDBKey(nodeName);
     9         }
    10         RoutingDatasource dataSource = new RoutingDatasource();
    11         dataSource.setTargetDataSources(dsMap);
    12         if (null == dsMap.get(DBKey.DEFAULT)) {
    13             throw new RuntimeException(
    14                     String.format("Default DataSource [%s] not exists", DBKey.DEFAULT));
    15         }
    16         dataSource.setDefaultTargetDataSource(dsMap.get(DBKey.DEFAULT));
    17         return dataSource;
    18     }
    19 
    20 
    21 
    22 @ConfigurationProperties(prefix = "mybatis")
    23 @Data
    24 public class MybatisProperties {
    25 
    26     private Map<String, String> params;
    27 
    28     private Map<String, Object> nodes;
    29 
    30     /**
    31      * mapper文件路径:多个location以,分隔
    32      */
    33     private String mapperLocations = "classpath*:com/iqiyi/xiu/**/mapper/*.xml";
    34 
    35     /**
    36      * Mapper类所在的base package
    37      */
    38     private String basePackage = "com.iqiyi.xiu.**.repository";
    39 
    40     /**
    41      * mybatis配置文件路径
    42      */
    43     private String configLocation = "classpath:mybatis-config.xml";
    44 }

    那threadLocal中的key什么时候进行销毁呢,其实可以自定义一个基于mybatis的拦截器,在拦截器中主动调DBContextHolder.clear()方法销毁这个key。具体代码就不贴了。这样一来,我们就完成了一个基于注解的支持多数据源切换的中间件。

    那有没有可以优化的点呢?其实,可以发现,在获取mapper接口/所在类的注解的时候,使用了反射来获取的,那我们知道一般反射调用是比较耗性能的,所以可以考虑在这里加个本地缓存来优化下性能:

     1     private final static Map<String, String> METHOD_CACHE = new ConcurrentHashMap<>();
     2 //.... 
     3 public void innerBefore(JoinPoint point, boolean isClass) {
     4         String methodName = point.getSignature().getName();
     5 
     6         Class<?> clazz = getClass(point, isClass);
     7         //key为类名+方法名
     8         String keyString = clazz.toString() + methodName;
     9         //使用默认数据源
    10         String dbKey = DBKey.DEFAULT;
    11         //如果缓存中已经有这个mapper方法对应的数据源的key,那直接设置
    12         if (METHOD_CACHE.containsKey(keyString)) {
    13             dbKey = METHOD_CACHE.get(keyString);
    14         } else {
    15             Class<?>[] parameterTypes =
    16                     ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
    17             Method method = null;
    18 
    19             try {
    20                 method = clazz.getMethod(methodName, parameterTypes);
    21             } catch (NoSuchMethodException e) {
    22                 throw new RuntimeException("can't find " + methodName + " in " + clazz.toString());
    23             }
    24              //方法上存在注解,使用方法定义的datasource
    25             if (method.isAnnotationPresent(DBKey.class)) {
    26                 DBKey key = method.getAnnotation(DBKey.class);
    27                 dbKey = key.value();
    28             } else {
    29                 clazz = method.getDeclaringClass();
    30                 //使用类上定义的注解
    31                 if (clazz.isAnnotationPresent(DBKey.class)) {
    32                     DBKey key = clazz.getAnnotation(DBKey.class);
    33                     dbKey = key.value();
    34                 }
    35             }
    36            //先放本地缓存
    37             METHOD_CACHE.put(keyString, dbKey);
    38         }
    39         DBContextHolder.setDBKey(dbKey);
    40     }

    这样一来,只有在第一次调用这个mapper接口的时候,才会走反射调用的逻辑去获取对应的数据源,后续,都会走本地缓存,提升了性能。

  • 相关阅读:
    深入单机事务
    事务的本质
    云计算是什么?
    linux命令之tail
    linux命令之rpm
    String、StringBuffer与StringBuilder区别
    自定义今天的520
    logback中MDC使用
    数组(转载)
    spring获取webapplicationcontext,applicationcontext几种方法详解(转载)
  • 原文地址:https://www.cnblogs.com/jy107600/p/14663908.html
Copyright © 2011-2022 走看看