1、读写分离:可以通过Spring提供的AbstractRoutingDataSource类,重写determineCurrentLookupKey方法,实现动态切换数据源的功能;读写分离可以有效减轻写库的压力,又可以把查询数据的请求分发到不同读库;MySql主从同步传送门
2、写数据库:当调用insert、update、delete及一些实时数据用到的库;
3、读数据库:当调用select查询数据用到的库;
4、JaveWeb工程通过AbstractRoutingDataSource类实现读写分离;
a、jdbc.properties文件配置读写数据源
复制代码
datasource.type=mysql
datasource.driverClassName=com.mysql.jdbc.Driver
datasource.username=root
#写库
w.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/ddt?characterEncoding\=utf-8
w.datasource.password=write123
#读库
r.datasource.url=jdbc\:mysql\://IP\:3306/ddt?characterEncoding\=utf-8
r.datasource.password=read123
#连接池配置
c3p0.acquireIncrement=3
c3p0.acquireRetryAttempts=10
c3p0.acquireRetryDelay=1000
c3p0.initialPoolSize=20
c3p0.idleConnectionTestPeriod=3600
c3p0.testConnectionOnCheckout=true
c3p0.minPoolSize=10
c3p0.maxPoolSize=80
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=10800
复制代码
b、application.xml文件
复制代码
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">;
${datasource.driverClassName}
${r.datasource.url}
${datasource.username}
${r.datasource.password}
${c3p0.acquireIncrement}
${c3p0.acquireRetryAttempts}
${c3p0.acquireRetryDelay}
${c3p0.initialPoolSize}
${c3p0.testConnectionOnCheckout}
${c3p0.minPoolSize}
${c3p0.maxPoolSize}
${c3p0.maxIdleTime}
${c3p0.idleConnectionTestPeriod}
${c3p0.maxStatements}
${c3p0.numHelperThreads}
${datasource.driverClassName}
${w.datasource.url}
${datasource.username}
${w.datasource.password}
${c3p0.acquireIncrement}
${c3p0.acquireRetryAttempts}
${c3p0.acquireRetryDelay}
${c3p0.initialPoolSize}
${c3p0.testConnectionOnCheckout}
${c3p0.minPoolSize}
${c3p0.maxPoolSize}
${c3p0.maxIdleTime}
${c3p0.idleConnectionTestPeriod}
${c3p0.maxStatements}
${c3p0.numHelperThreads}
${hibernate.dialect}
${hibernate.connection.autocommit}
${hibernate.show_sql}
${hibernate.format_sql}
${hibernate.hbm2ddl.auto}
utf-8
${hibernate.cache.user_query_cache}
${hibernate.user_second_level_cache}
${hibernate.cache.class}
${hibernate.ehcache_config_file}
复制代码
c、继承AbstractRoutingDataSource类的动态数据源类DynamicDataSource
复制代码
package com.eb3.ddt;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
/**
- 重写determineCurrentLookupKey方法br/>*/
@Override
protected Object determineCurrentLookupKey() {
Object obj = DBHelper.getDbType();
return obj;
}
}
复制代码
d、DBHelper工具类
复制代码
package com.eb3.ddt;
import org.apache.commons.lang.StringUtils;
public class DBHelper {
private static ThreadLocal
// 写数据源标识
public final static String DB_WRITE = "dataSourceWrite";
// 读数据源标识
public final static String DB_READ = "dataSourceRead";
/**
* 获取数据源类型,即是写数据源,还是读数据源
*
* @return
*/
public static String getDbType() {
String db_type = dbContext.get();
if (StringUtils.isEmpty(db_type)) {
// 默认是写数据源
db_type = DB_WRITE;
}
return db_type;
}
/**
* 设置该线程的数据源类型
*
* @param str
*/
public static void setDbType(String str) {
dbContext.set(str);
}
}
复制代码
e、服务层调用
复制代码
/@Aspect 此注解会影响数据源切换,运行代码得知不加的话会先执行DynamicDataSource里的determineCurrentLookupKey方法,后执行Service层里DBHelper.setDbType()方法,导致数据源切换失败!
/br/>@Aspect
@Component("userService")
public class UserServiceImpl extends BaseServiceImpl
private UserDao userDao;
@Override
protected BaseDao getDao() {
return this.userDao;
}
@Override
public void save(User user) {
DBHelper.setDbType(DBHelper.DB_WRITE); // 写库
this.userDao.save(user);
}
@Override
public User findByUserName(String username) {
DBHelper.setDbType(DBHelper.DB_READ); // 读库
List userList = this.userDao.findBy("username", username);
return CollectionUtils.isNotEmpty(userList) ? userList.get(0) : null;
}
}
欢迎工作一到五年的Java工程师朋友们加入Java群: 891219277
群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!