摘要:在开发的项目中大都都会使用读写分离的技术,本人目前开发的项目接触到的都是主从复制(一主一从),就是一个Master数据库,一个Slave数据库。主库负责数据插入、更新和实时数据查询,从库库负责非实时数据查询。在实际项目应用中,都是读多写少,而读取数据通常比较复杂而且耗时,SQL语句需要各种优化。采用读写分离技术可以有效缓解数据库的压力,加快响应速度,提升用户体验。如果随着业务不断扩展,数据不断增加,那么可以扩展多个从节点(一主多从),使用负载均衡,减轻每个从库的查询压力。本文将从一主一从说开去。
SpringAop面向切面编程在本文就不详细介绍,如果不清楚可自行百度Google。
1.项目搭建
可以参考本系列文章,博客地址:https://blog.csdn.net/caiqing116/article/details/84573166
或者直接下载项目,git地址:https://github.com/gitcaiqing/SSM_DEMO.git
2.搭建MySQL主从复制
在写本文的时候我特地搭建了2个MySQL服务,端口号分别是3306,3308
搭建MySQL主从复制可参考:https://blog.csdn.net/caiqing116/article/details/84995472
3.项目配置
(1)config/jdbc.config配置
#连接驱动
jdbc.driverClassName=com.mysql.jdbc.Driver
#端口号3306的数据源
jdbc.url = jdbc\:mysql\://localhost\:3306/db_ssmdemo?useUnicode\=true&characterEncoding\=UTF-8&allowMultiQueries\=true
jdbc.username = root
jdbc.password = 123456
#端口号3308的数据源
jdbc.3308.url = jdbc\:mysql\://localhost\:3308/db_ssmdemo?useUnicode\=true&characterEncoding\=UTF-8&allowMultiQueries\=true
jdbc.3308.username = root
jdbc.3308.password = 123456
#定义初始连接数
jdbc.initialSize=2
#定义最大连接数
jdbc.maxActive=20
#定义最大空闲
jdbc.maxIdle=20
#定义最小空闲
jdbc.minIdle=1
#定义最长等待时间
jdbc.maxWait=60000
#验证数据库连接的有效性
jdbc.validationQuery=select 1
(2)spring/mybatis.xml配置
(3)spring/dataAccessContext.xml配置
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
(5)修改spring/applicationContext.xml 配置启用AOP注解
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd ">
4.自定义类继承AbstractRoutingDataSource,实现determineCurrentLookupKey,并且自定义属性methodPrefix用于设置切换到从节点的方法名前缀
AbstractRoutingDataSource的相关源码在我的另一篇文章中有介绍,参考博文:https://blog.csdn.net/caiqing116/article/details/84979682
我们实现determineCurrentLookupKey方法,返回值为dataSource3306或dataSource3308,定义属性Map
package com.ssm.datasource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 定义动态数据源,集成spring提供的AbstractRoutingDataSource,实现determineCurrentLookupKey
* @author https://blog.csdn.net/caiqing116
*/
public class DynamicDataSource extends AbstractRoutingDataSource{
private static final Logger log = LoggerFactory.getLogger(DynamicDataSource.class);
/*
* 定义一个方法前缀methodPrefix属性,通过配置文件赋值,该map存储数据源和方法前缀的值形如形如
* {"slave",["list","count","find","get","select","query","等不一一列举"]}
* 这里的方法前缀根据开发人员的习惯可自行配置
*/
public static Map
//设置切换到从库的方法前缀
@SuppressWarnings("static-access")
public void setMethodPrefix(Map
this.methodPrefix = methodPrefix;
}
@Override
protected Object determineCurrentLookupKey() {
Object dataType = DataSourceContextHolder.getDataType();
log.info("当前数据源:{}",dataType);
return dataType;
}
}
5.借助ThreadLocal类,通过ThreadLocal类传递参数设置数据源,并且添加方法isSlave根据参数判断是否切换到从节点
package com.ssm.datasource;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
/**
* 切换数据源,清除数据源信息等
* @author https://blog.csdn.net/caiqing116
*/
public class DataSourceContextHolder {
//定义数据源标识和配置文件dataAccessContext.xml配置的bean id一致
public static final String DATASOURCE = "dataSource3306";
public static final String DATASOURCE3308 = "dataSource3308";
//定义从库标识,和配置
public static final String SLAVE = "slave";
private static final ThreadLocal
/**
* 设置当前数据源
* @param dataType 数据元类型 DATASOURCE | DATASOURCE3308
*/
public static void setDatatype(String dataType) {
contextHolder.set(dataType);
}
/**
* 获取当前数据源
* @return
*/
public static String getDataType(){
return contextHolder.get();
}
/**
* 清除
*/
public static void clear() {
contextHolder.remove();
}
/**
* 切到3306端口数据源
*/
public static void mark3306() {
setDatatype(DATASOURCE);
}
/**
* 切到3308端口数据源
*/
public static void mark3308() {
setDatatype(DATASOURCE3308);
}
/**
* 判断是否进入从库
* @return
*/
public static boolean isSlave(String methodName) {
//未配置切换到从库的方法名前缀,返回false,默认切换到主库
Map
if(methodPrefix == null) {
return false;
}
List
if(methodPrefixs.isEmpty()) {
return false;
}
//方法名前缀和配置的相同则切换到从库
if(StringUtils.startsWithAny(methodName, methodPrefixs.toArray(new CharSequence[methodPrefixs.size()]))) {
return true;
}
return false;
}
}
6.定义切面类
创建包com.ssm.aspect,创建类DataSourceAspect.java,具体实现如下
package com.ssm.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.ssm.datasource.DataSourceContextHolder;
/**
* 定义一个数据源切换的切面
* @aspect 注释对于在类路径中自动检测是不够的:为了达到这个目的,您需要添加一个单独的@component注解
* @author https://blog.csdn.net/caiqing116
*/
@Aspect
@Component
public class DataSourceAspect {
private static final Logger log = LoggerFactory.getLogger(DataSourceAspect.class);
/**
* 定义切面
* 跟数据源相关的操作都是在service包下
*/
@Pointcut("execution(* com.ssm.service.*.*(..))")
public void servicePoint(){}
/**
* 根据切入点进行数据源的切换
*/
@Before("servicePoint()")
public void before(JoinPoint joinpoint){
String className = joinpoint.getTarget().getClass().getName();
String methodName = joinpoint.getSignature().getName();
//判断如果方法名开头符合从库,则切换到从库(端口号3308的服务)
if(DataSourceContextHolder.isSlave(methodName)) {
log.info("执行类:"+className+" 方法:"+methodName+",切换到从库");
DataSourceContextHolder.mark3308();
}else {
log.info("执行类:"+className+" 方法:"+methodName+",切换到主库");
DataSourceContextHolder.mark3306();
}
}
}
7.测试
编写增删查改Service和实现类,这些在之前的文章中有介绍,就不重复介绍了
com/ssm/service/BasicUserService.java
package com.ssm.service;
import java.util.List;
import com.ssm.entity.BasicUser;
import com.ssm.entity.Page;
/**
* 用户Service
* @author https://blog.csdn.net/caiqing116
*
*/
public interface BasicUserService {
Integer insert(BasicUser basicUser);
Integer deleteById(Integer id);
BasicUser selectById(Integer id);
Integer updateById(BasicUser basicUser);
BasicUser selectByUsername(String username);
}
com/ssm/service/impl/BasicUserServiceImpl.java
package com.ssm.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.entity.BasicUser;
import com.ssm.entity.Page;
import com.ssm.mapper.BasicUserMapper;
import com.ssm.service.BasicUserService;
/**
* 用户Service实现类
* @author https://blog.csdn.net/caiqing116
*
*/
@Service
public class BasicUserServiceImpl implements BasicUserService{
@Autowired
private BasicUserMapper basicUserMapper;
/**
* 插入用户
*/
public Integer insert(BasicUser basicUser) {
return basicUserMapper.insertSelective(basicUser);
}
/**
* 根据id删除
*/
public Integer deleteById(Integer id) {
return basicUserMapper.deleteByPrimaryKey(id);
}
/**
* 根据id查询
*/
public BasicUser selectById(Integer id) {
return basicUserMapper.selectByPrimaryKey(id);
}
/**
* 根据id更新
*/
public Integer updateById(BasicUser basicUser) {
return basicUserMapper.updateByPrimaryKeySelective(basicUser);
}
/**
* 根据用户名查询
*/
public BasicUser selectByUsername(String username) {
return basicUserMapper.selectByUsername(username);
}
}
Service测试类
package com.ssm.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ssm.entity.BasicUser;
import com.ssm.util.EncryptKit;
import com.ssm.util.UuidUtil;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/*.xml","classpath:servlet/*.xml" })
public class BasicUserServiceTest {
private static final Logger log = LoggerFactory.getLogger(BasicUserServiceTest.class);
@Autowired
private BasicUserService basicUserService;
@Test
public void testInsert() {
BasicUser basicUser = new BasicUser();
basicUser.setUtype(1);
basicUser.setUserid(UuidUtil.getUuid());
basicUser.setUsername("叹稀奇");
basicUser.setRealname("叹稀奇");
basicUser.setPassword(EncryptKit.MD5("123456"));
basicUser.setAge(18);
//手动切换数据源测试代码
//切换到3308插入数据
/*DataSourceContextHolder.clear();
DataSourceContextHolder.mark3306();
int result = basicUserService.insert(basicUser);
DataSourceContextHolder.clear();*/
//自动切换数据源测试
int result = basicUserService.insert(basicUser);
log.info("basicUser:"+basicUser);
log.info("插入行数:"+result);
}
@Test
public void testDeleteById() {
int result = basicUserService.deleteById(1);
log.info("删除行数:"+result);
}
@Test
public void testSelectById() {
BasicUser basicUser = basicUserService.selectById(1);
log.info("basicUser:"+basicUser);
}
@Test
public void testUpdateById() {
BasicUser basicUser = new BasicUser();
basicUser.setId(1);
basicUser.setAge(19);
int result = basicUserService.updateById(basicUser);
log.info("更新行数:"+result);
}
@Test
public void testSelectByUsername() {
String username = "墨倾池";
BasicUser basicUser = basicUserService.selectByUsername(username);
log.info("basicUser:"+basicUser);
}
}
在测试中,我们看下日志文件的打印
测试方法: testSelectById,日志如下:
[INFO] 执行类:com.ssm.service.impl.BasicUserServiceImpl 方法:selectById,切换到从库
[INFO] 当前数据源:dataSource3308
12
测试方法:insert,日志如下:
[INFO] 执行类:com.ssm.service.impl.BasicUserServiceImpl,方法:insert,切换到主库
[INFO] 当前数据源:dataSource3306
12
在测试插入的时候,我们可以分别查询下主库和从库的数据,可以验证主从是否正常同步,如下图说明读写分离是成功的
8.Service层调用内部方法无法切换数据源问题
/**
* 根据id查询
*/
public BasicUser selectById(Integer id) {
//该方法内部含有一些其他业务处理,如插入删除更新操作等等需要,需要切换到主节点
//在这里进行了Service层内部方法调用
//一般理解,这里会切换到从库,实际是不会的
log.info("执行删除操作开始");
this.deleteById(3);
log.info("执行删除操作结束");
return basicUserMapper.selectByPrimaryKey(id);
}
执行测试方法testSelectById,日志如下:
[INFO] 执行类:com.ssm.service.impl.BasicUserServiceImpl 方法:selectById,切换到从库
[INFO] 执行删除操作开始
[INFO] 进入方法deleteById.....
[INFO] 执行删除操作结束
[INFO] 当前数据源:dataSource3308
[INFO] basicUser:BasicUser [id=1, userid=bc8bbfb770ee4f2c9ba0f988a7a92d4f, utype=1, username=墨倾池, password=E10ADC3949BA59ABBE56E057F20F883E, headimg=null, realname=云天望垂, sex=null, age=19, mobile=null, email=null, credate=Fri Dec 07 14:50:20 CST 2018, upddate=null]
可以看出在进入方法deleteById并没有切换数据源。这是为什么呢,原来Service中如此调用并非调用的是代理类中的方法,然而必须要调用代理类才会被切进去。
既然只有调用代理类的方法才能切入,那我们就拿到代理类,再进行调用。修改selectById方法。
/**
* 根据id查询
*/
public BasicUser selectById(Integer id) {
//该方法内部含有一些其他业务处理,如插入删除更新操作等等需要,需要切换到主节点
//在这里进行了Service层内部方法调用
//一般理解,这里会切换到从库,实际是不会的
log.info("执行删除操作开始");
//this.deleteById(3);
//获取代理类
BasicUserService proxy = ((BasicUserService)AopContext.currentProxy());
proxy.deleteById(3);
log.info("执行删除操作结束");
return basicUserMapper.selectByPrimaryKey(id);
}
执行后报错,提示需要设置expose-proxy属性为true,将代理暴露出来
java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.
at org.springframework.aop.framework.AopContext.currentProxy(AopContext.java:64)
at com.ssm.service.impl.BasicUserServiceImpl.selectById(BasicUserServiceImpl.java:59)
at com.ssm.service.impl.BasicUserServiceImpl$$FastClassBySpringCGLIB$$f982c283.invoke(
我们修改下spring/applicationContext.xml
再次运行我们查看BasicUserService对象为代理对象,如下图
查看日志如下,执行删除操作的时候数据源从从库切到了主库
[INFO] 执行类:com.ssm.service.impl.BasicUserServiceImpl 方法:selectById,切换到从库
[INFO] 执行删除操作开始
[INFO] 执行类:com.ssm.service.impl.BasicUserServiceImpl 方法:deleteById,切换到主库
[INFO] 进入方法deleteById.....
[INFO] 执行删除操作结束
[INFO] 当前数据源:dataSource3306
附上原文地址:https://blog.csdn.net/caiqing116/article/details/85058052