baomidou的dynamic-datasource读写分离实现和加入AOP根据方法名选择库


声明:本文转载自https://my.oschina.net/bianxin/blog/3120455,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

文档

https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter/wikis/pages

maven

        <dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
			<version>2.5.7</version>
		</dependency>

纯读写分离(mybatis环境)

场景:

  1. 在纯的读写分离环境,写操作全部是master,读操作全部是slave。
  2. 不想通过注解配置完成以上功能。

答:在mybatis环境下可以基于mybatis插件结合本数据源完成以上功能。 手动注入插件。

@Bean
public MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin(){
    return new MasterSlaveAutoRoutingPlugin();
}

默认主库名称master,从库名称slave。

问题

       我在配置好了之后,调试发现对数据库读的操作不得进入MasterSlaveAutoRoutingPlugin,而且进入了默认的库。只有写进入了MasterSlaveAutoRoutingPlugin中。当然也可以默认为从库,但是感觉就不是很好。

       于是我自定义了一个aop切面来,来完成库的选择,代码如下:

import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.extern.java.Log;

/**
 * Copyright: Copyright (c) 2019
 * <p> 说明:动态数据源配置 </P>
 * 
 * @version: V1.0
 * @author: BianPeng
 * 
 */
@Aspect
@Component
@Order(0)
@Lazy(false)
@Log
public class DataSourceAop{

	private static final String MASTER = "master";

	private static final String SLAVE = "slave";

	
	@Pointcut("execution(* com.buybit.power.service..*.*(..)) || execution(* com.baomidou.mybatisplus.extension.service..*.*(..))")
    public void checkArgs() {
    }
	
	// 这里切到你的方法目录
	@Before("checkArgs()")
	public void process(JoinPoint joinPoint) throws NoSuchMethodException, SecurityException {
		String methodName = joinPoint.getSignature().getName();
		if (methodName.startsWith("get") 
                || methodName.startsWith("count") 
                || methodName.startsWith("find")
				|| methodName.startsWith("list") 
                || methodName.startsWith("select") 
                || methodName.startsWith("check")
				|| methodName.startsWith("page")) {

			log.info("当前执行的库:"+SLAVE);
			DynamicDataSourceContextHolder.push(SLAVE);
		} else {
			log.info("当前执行的库:"+MASTER);
			DynamicDataSourceContextHolder.push(MASTER);
		}
	}
	@After("checkArgs()")
    public void afterAdvice(){
        DynamicDataSourceContextHolder.clear();
    }
}

但是发现,baomidou/dynamic-datasource自带的@DS没失去了着用,于是我把有@DS的类和方法排除掉,代码入下:

import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.extern.java.Log;

/**
 * Copyright: Copyright (c) 2019
 * <p> 说明:动态数据源配置 </P>
 * 
 * @version: V1.0
 * @author: BianPeng
 * 
 */
@Aspect
@Component
@Order(0)
@Lazy(false)
@Log
public class DataSourceAop{

	private static final String MASTER = "master";

	private static final String SLAVE = "slave";

	
	@Pointcut("execution(* com.buybit.power.service..*.*(..)) || execution(* com.baomidou.mybatisplus.extension.service..*.*(..))")
    public void checkArgs() {
    }
	
	// 这里切到你的方法目录
	@Before("checkArgs()")
	public void process(JoinPoint joinPoint) throws NoSuchMethodException, SecurityException {
		String methodName = joinPoint.getSignature().getName();
        Class clazz = joinPoint.getTarget().getClass();
		if(clazz.isAnnotationPresent(DS.class)){
		    //获取类上注解
			return;
		} 
		
	    String targetName = clazz.getSimpleName();
	    Class[] parameterTypes = 
        ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterTypes();
	    Method methdo = clazz.getMethod(methodName,parameterTypes);
	    if (methdo.isAnnotationPresent(DS.class)) {
	    	return;
	    }
		if (methodName.startsWith("get") 
                || methodName.startsWith("count") 
                || methodName.startsWith("find")
				|| methodName.startsWith("list") 
                || methodName.startsWith("select") 
                || methodName.startsWith("check")
				|| methodName.startsWith("page")) {

			log.info("当前执行的库:"+SLAVE);
			DynamicDataSourceContextHolder.push(SLAVE);
		} else {
			log.info("当前执行的库:"+MASTER);
			DynamicDataSourceContextHolder.push(MASTER);
		}
	}
	@After("checkArgs()")
    public void afterAdvice(){
        DynamicDataSourceContextHolder.clear();
    }
}

这样可以让你有@DS的注解依然生效,而且也会根据方法名来自动切换数据源。

本文发表于2019年10月22日 11:00
(c)注:本文转载自https://my.oschina.net/bianxin/blog/3120455,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 4864 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1