在Mysql主从复制的基础上,可以使用读写分离来降低单台Mysql节点的压力,从而来提高访问效率,读写分离的架构如下:
对于读写分离的实现,可以通过Spring AOP 来进行动态的切换数据源,进行操作 :
在原有建立主从复制的数据库中来进行操作,首先先定义两个数据库的配置,一个是write数据库用来写数据,一个是read数据库用来读数据。
db.properties
jdbc.write.driver=com.mysql.jdbc.Driver jdbc.write.url=jdbc:mysql:// 192. 168. 142. 128 : 3306 /mysql_demo jdbc.write.username=root jdbc.write.password=123456 jdbc.read.driver=com.mysql.jdbc.Driver jdbc.read.url=jdbc:mysql://192.168.142.129:3306/mysql_demo jdbc.read.username=root jdbc.read.password=123456
applicationContext-datasource.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="readDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.read.driver}"></property> <property name="jdbcUrl" value="${jdbc.read.url}"></property> <property name="user" value="${jdbc.read.username}"></property> <property name="password" value="${jdbc.read.password}"></property> </bean> <bean id="writeDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.write.driver}"></property> <property name="jdbcUrl" value="${jdbc.write.url}"></property> <property name="user" value="${jdbc.write.username}"></property> <property name="password" value="${jdbc.write.password}"></property> </bean> <!-- 继承自 AbstractRoutingDataSource --> <bean id="dataSource" class="cn.zhangcc.aop.datasource.ChooseDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String" value-type="javax.sql.DataSource"> <entry key="write" value-ref="writeDataSource"/> <entry key="read" value-ref="readDataSource"/> </map> </property> <property name="defaultTargetDataSource" ref="writeDataSource"/> <property name="methodType"> <map key-type="java.lang.String"> <entry key="read" value=",get,select,count,list,query,find"/> <entry key="write" value=",add,create,update,delete,remove,insert"/> </map> </property> </bean> </beans>
改变选择数据源的类
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ChooseDataSource extends AbstractRoutingDataSource { public static Map<String, List<String>> METHOD_TYPE_MAP = new HashMap<String, List<String>>(); /** * 实现父类中的抽象方法,获取数据源名称 * @return */ protected Object determineCurrentLookupKey() { return DataSourceHandler.getDataSource(); } // 设置方法名前缀对应的数据源 public void setMethodType(Map<String, String> map) { for (String key : map.keySet()) { List<String> v = new ArrayList<String>(); String[] types = map.get(key).split(","); for (String type : types) { if (!StringUtils.isEmpty(type)) { v.add(type); } } METHOD_TYPE_MAP.put(key, v); } System.out.println("METHOD_TYPE_MAP : "+METHOD_TYPE_MAP); } }
DataSourceHandler.java类,在项目启动的时候将配置的读、写数据源加到holder中
public class DataSourceHandler { // 数据源名称 public static final ThreadLocal<String> holder = new ThreadLocal<String>(); /** * 在项目启动的时候将配置的读、写数据源加到holder中 */ public static void putDataSource(String datasource) { holder.set(datasource); } /** * 从holer中获取数据源字符串 */ public static String getDataSource() { return holder.get(); }
定义数据源的AOP切面类DataSourceAspect.java,通过该切面类判断Service的方法名是应该走读库还是写库
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component @Aspect @EnableAspectJAutoProxy//开启AspectJ自动代理 @Order(-9999)// 用来控制Bean的加载顺序,值越大优先级最高 public class DataSourceAspect { @Before("execution(* cn.zhangcc.service.*.*(..))") public void beforeExecute(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); System.out.println("------> 拦截的方法名 : " + name); for (String key : ChooseDataSource.METHOD_TYPE_MAP.keySet()) { for (String type : ChooseDataSource.METHOD_TYPE_MAP.get(key)) { if(name.startsWith(type)){ DataSourceHandler.putDataSource(key); System.out.println("---------> 获取当前使用的数据库连接池 : " + key); break; } } } } }
通过 @Order(-9999) 注解来控制事务管理器, 与该通知类的加载顺序 , 需要让通知类 , 先加载 , 来判定使用哪个数据源 .