<!--这里真正实现读写分离,利用路由dataSource-->
<bean id="dataSource" class="org.ibase4j.core.aspect.ChooseDataSource" lazy-init="true">
<description>数据源</description>
<property name="targetDataSources">
<map key-type="java.lang.String" value-type="javax.sql.DataSource">
<!-- write -->
<entry key="write" value-ref="writeDataSource" />
<!-- read -->
<entry key="read" value-ref="readDataSource" />
</map>
</property>
<property name="defaultTargetDataSource" ref="writeDataSource" />
<property name="methodType">
<map key-type="java.lang.String">
<!-- read -->
<entry key="read" value=",get,select,count,list,query," />
<!-- write -->
<entry key="write" value=",add,insert,create,update,delete,remove," />
</map>
</property>
</bean>
<!-- 切面 -->
<bean class="org.ibase4j.core.aspect.DataSourceAspect" />
切面的定义
@Aspect
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DataSourceAspect {
private final Logger logger = LogManager.getLogger();
@Pointcut("execution(* org.ibase4j.service..*.*(..))")
public void aspect() {
}
/**
* 配置前置通知,使用在方法aspect()上注册的切入点
*/
@Before("aspect()")
public void before(JoinPoint point) {
String className = point.getTarget().getClass().getName();
String method = point.getSignature().getName();
logger.info(className + "." + method + "(" + StringUtils.join(point.getArgs(), ",") + ")");
try {
L: for (String key : ChooseDataSource.METHODTYPE.keySet()) {
for (String type : ChooseDataSource.METHODTYPE.get(key)) {
//也即service中的方法的开始以哪个关键词开始,如果是哪类就把读或者是写数据库写入到线程的独享变量中。
if (method.startsWith(type)) {
logger.info(key);
HandleDataSource.putDataSource(key);
break L;
}
}
}
} catch (Exception e) {
logger.error(e);
HandleDataSource.putDataSource("write");
}
}
@After("aspect()")
public void after(JoinPoint point) {
HandleDataSource.clear();
}
需要注意这里HandleDataSource的特殊性,读、写标志被写入到每个线程的共享变量中。
public class HandleDataSource {
// 数据源名称线程池
private static final ThreadLocal<String> holder = new ThreadLocal<String>();
public static void putDataSource(String datasource) {
holder.set(datasource);
}
public static String getDataSource() {
return holder.get();
}
public static void clear() {
holder.remove();
}
}
整个过程为:web调用servie端,eg:updateUser(),这样通过aop设置当前线程的共享变量为write,然后执行updateUser中的内容,UserMapper.updateUser(),这时候mybatis会调用ChooseDataSource进行数据源路由选择,这时会调用getConnection来获取数据库连接。
可以查看AbstractRoutingDataSource类的源码找到getConnection和determineCurrentLookupKey的关系。
其中重要的一点:把和当前线程相关的内容存到ThreadLocal当个线程共享变量中。后台并行即使执行多个请求,也不会错乱。
// 获取数据源名称
protected Object determineCurrentLookupKey() {
return HandleDataSource.getDataSource();
}
附加通过Durid进行数据库连接的配置文件,方便以后查阅。
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter" lazy-init="true">
<description>状态过滤器,定义慢sql的执行时间,同时合并没有参数的sql select *</description>
<property name="slowSqlMillis" value="3000" />
<property name="logSlowSql" value="true" />
<property name="mergeSql" value="true" />
</bean>
<bean id="wirteDataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method ="close" init-method="init" lazy-init="true">
<description>只写数据库连接</description>
<property name="driverClassName" value="${db.driver}"></property>
<property name="url" value="${db.writer.url}"></property>
<property name="username" value="${db.writer.username}"></property>
<property name="password" value="${db.writer.password}"></property>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${db.initialSize}" />
<!-- 连接池最大数量 -->
<property name="maxActive" value="${db.maxActive}" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${db.minIdle}" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${db.maxWait}" />
<!-- 配置数据库只读属性-->
<property name="defaultReadOnly" value="false" />
<!-- 配置过滤器,用来统计sql监控信息 ,和下面filters属性为组合属性-->
<property name="proxyFilters">
<list>
<ref bean="stat-filter" />
</list>
</property>
<property name="filters" value="${druid.filters}" />
<property name="timeBetweenLogStatsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}" />
</bean>
<bean id="readDataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method ="close" init-method="init" lazy-init="true">
<description>只读数据库连接</description>
<property name="driverClassName" value="${db.driver}"></property>
<property name="url" value="${db.read.url}"></property>
<property name="username" value="${db.read.username}"></property>
<property name="password" value="${db.read.password}"></property>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${db.initialSize}" />
<!-- 连接池最大数量 -->
<property name="maxActive" value="${db.maxActive}" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${db.minIdle}" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${db.maxWait}" />
<!-- 配置数据库只读属性-->
<property name="defaultReadOnly" value="true" />
<!-- 配置过滤器,用来统计sql监控信息 ,和下面filters属性为组合属性-->
<property name="proxyFilters">
<list>
<ref bean="stat-filter" />
</list>
</property>
<property name="filters" value="${druid.filters}" />
<property name="timeBetweenLogStatsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}" />
</bean>
<bean id="dataSource" class="org.ibase4j.core.aspect.ChooseDataSource" lazy-init="true">
<description>数据源</description>
<property name="targetDataSource">
<map key-type="java.lang.String" value-type="javax.sql.DataSource">
<entry key="write" value-ref="writeDataSource"></entry>
<entry key="read" value-ref="readDataSource"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="writeDataSource"></property>
<property name="methodType">
<map key-type="java.lang.String" value-type="java.lang.String">
<entry key="read" value=",get,select,count,list,query,"></entry>
<entry key="write" value=",add,insert,create,update,delete,remove,"></entry>
</map>
</property>
</bean>
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<description>spring和mybatis结合以及连接mysql</description>
<property name="dataSource" ref="dataSource"></property>
<!-- 扫描xml文件 -->
<property name="mapperLocations" value="classpath*:**/mapper/xml/*.xml" />
</bean>
<bean id="mapperScaner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<description>Dao接口所在的类,Spring自动查找下面的类</description>
<property name="basePackage" value="org.ldy.springmvc.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true">
</tx:annotation-driven>
web开启durid监控功能
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
<init-param>
<!-- 允许访问 -->
<param-name>allow</param-name>
<param-value>127.0.0.1,localhost</param-value>
</init-param>
<init-param>
<!-- 用户名 -->
<param-name>loginUsername</param-name>
<param-value>druid</param-value>
</init-param>
<init-param>
<!-- 密码 -->
<param-name>loginPassword</param-name>
<param-value>druid</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>