网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

baomidou中怎么利用dynamic-datasource实现读写分离

这篇文章给大家介绍baomidou中怎么利用dynamic-datasource实现读写分离,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

十余年的达川网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整达川建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“达川网站设计”,“达川网站推广”以来,每个客户项目都认真落实执行。

maven

        
			com.baomidou
			dynamic-datasource-spring-boot-starter
			2.5.7
		

纯读写分离(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
 * 

 说明:动态数据源配置 

 *   * @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
 * 

 说明:动态数据源配置 

 *   * @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();     } }

关于baomidou中怎么利用dynamic-datasource实现读写分离就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享名称:baomidou中怎么利用dynamic-datasource实现读写分离
文章来源:http://bjjierui.cn/article/jpphij.html

其他资讯