In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use dynamic-datasource to achieve read-write separation in baomidou. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Maven com.baomidou dynamic-datasource-spring-boot-starter 2.5.7 pure read-write separation (mybatis environment)
Scene:
In a pure read-write separation environment, write operations are all master and read operations are all slave.
Do not want to complete the above functions through annotated configuration.
Answer: the above functions can be accomplished based on the mybatis plug-in and this data source in the mybatis environment. Manually inject the plug-in.
@ Beanpublic MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin () {return new MasterSlaveAutoRoutingPlugin ();}
The default master library name is master and the slave library name is slave.
problem
After I had configured it, debugging found that read operations to the database were not allowed into MasterSlaveAutoRoutingPlugin, and entered the default library. Only writes are written into the MasterSlaveAutoRoutingPlugin. Of course, it can also be defaulted to from the library, but it doesn't feel very good.
So I customized an aop section to complete the selection of the library. The code is as follows:
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 *
Description: dynamic data source configuration
* * @ version: V1.0 * @ author: BianPeng * * / @ Aspect@Component@Order (0) @ Lazy (false) @ Logpublic 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 () {} / / cut to your method directory @ 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 ("currently executed library:" + SLAVE) | DynamicDataSourceContextHolder.push (SLAVE);} else {log.info ("currently executed library:" + MASTER); DynamicDataSourceContextHolder.push (MASTER);} @ After ("checkArgs ()") public void afterAdvice () {DynamicDataSourceContextHolder.clear ();}}
However, I found that @ DS, which comes with baomidou/dynamic-datasource, was not useful, so I excluded the classes and methods with @ DS and went into the following code:
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 *
Description: dynamic data source configuration
* * @ version: V1.0 * @ author: BianPeng * * / @ Aspect@Component@Order (0) @ Lazy (false) @ Logpublic 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 () {} / / cut to your method directory @ 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)) {/ / get the comments on the 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 ("currently executed library:" + SLAVE) | DynamicDataSourceContextHolder.push (SLAVE);} else {log.info ("currently executed library:" + MASTER); DynamicDataSourceContextHolder.push (MASTER);} @ After ("checkArgs ()") public void afterAdvice () {DynamicDataSourceContextHolder.clear () }} on how to use dynamic-datasource to achieve read-write separation in baomidou is shared here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.