1.简介

前面使用C3P0连接池进行过数据库的读写分离的实验,今天换一下数据库连接池改造一下,原理还是和原来的一样。
Druid是阿里出品,淘宝和支付宝专用数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个 SQL Parser。支持所有JDBC兼容的数据库,包括Oracle、MySql、Derby、Postgresql、SQL Server、H2等等。Druid针对Oracle和MySql做了特别优化,比如Oracle的PS Cache内存占用优化,MySql的ping检测优化。Druid提供了MySql、Oracle、Postgresql、SQL-92的SQL的完整支持,这是一个手写的高性能SQL Parser,支持Visitor模式,使得分析SQL的抽象语法树很方便。简单SQL语句用时10微秒以内,复杂SQL用时30微秒。通过Druid提供的SQL Parser可以在JDBC层拦截SQL做相应处理,比如说分库分表、审计等。Druid防御SQL注入攻击的WallFilter就是通过Druid的SQL Parser分析语义实现的。(本小段摘抄自)

2.引入依赖

<!-- mysql连接的jar包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.28</version>
        </dependency>

3.配置主从数据库

#数据库连接池类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# 主数据库连接
spring.datasource.master.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.master.url=jdbc:mysql:///mychat?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false
spring.datasource.master.url=jdbc:mysql://***.***.***.***:3306/mychat?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false
spring.datasource.master.username=****
spring.datasource.master.password=******
# 初始化大小
spring.datasource.master.initialSize=3
# 最小
spring.datasource.master.minIdle=3
# 最大
spring.datasource.master.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.master.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 
spring.datasource.master.timeBetweenEvictionRunsMillis=600000
# 配置一个连接在池中最小生存的时间,单位是毫秒 
spring.datasource.master.minEvictableIdleTimeMillis=300000
spring.datasource.master.validationQuery=SELECT 1 FROM DUAL
spring.datasource.master.testWhileIdle=true
spring.datasource.master.testOnBorrow=true
spring.datasource.master.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小 
spring.datasource.master.poolPreparedStatements=true
spring.datasource.master.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.master.filters=stat,wall,log4j
spring.datasource.master.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.master.useGlobalDataSourceStat=true

# 从数据库连接
spring.datasource.slave.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.slave.url=jdbc:mysql:///mychat?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false
spring.datasource.slave.url=jdbc:mysql://***.***.***.***:3306/mychat?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false
spring.datasource.slave.username=*****
spring.datasource.slave.password=*******
spring.datasource.slave.initialSize=3
spring.datasource.slave.minIdle=3
spring.datasource.slave.maxActive=20
spring.datasource.slave.maxWait=60000
spring.datasource.slave.timeBetweenEvictionRunsMillis=600000
spring.datasource.slave.minEvictableIdleTimeMillis=300000
spring.datasource.slave.validationQuery=SELECT 1 FROM DUAL
spring.datasource.slave.testWhileIdle=true
spring.datasource.slave.testOnBorrow=true
spring.datasource.slave.testOnReturn=false
spring.datasource.slave.poolPreparedStatements=true
spring.datasource.slave.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.slave.filters=stat,wall,log4j
spring.datasource.slave.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.slave.useGlobalDataSourceStat=true

4.配置数据源

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.center.constant.ConstantPool;
import com.center.support.database.ReadOnlyConnectionAspect;
import com.center.support.database.ReadWriteSplitRoutingDataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;

/**
 * Druid监控
 * 
 * @Title: DruidDBConfig.java
 * @Package com.center.config
 * @Description: TODO
 * @author Autumn、
 * @date 2018年8月19日
 */
@Configuration
public class DruidDBConfig {

    /**
     * 数据库连接池类型
     */
    @Value("${spring.datasource.type}")
    private Class<? extends DataSource> dataSourceType;

    /**
     * 动态数据源
     * 
     * @return 动态数据库连接池 DataSource
     */
    @Bean(name = "dataSource")
    @Qualifier(value = "dataSource")
    @Primary
    @DependsOn({ "masterDataSource", "slaveDataSource" })
    public DataSource dataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
            @Qualifier("slaveDataSource") DataSource slaveDataSource) {
        System.out.println(masterDataSource.toString());
        System.out.println(slaveDataSource.toString());
        ReadWriteSplitRoutingDataSource writeSplitRoutingDataSource = new ReadWriteSplitRoutingDataSource();
        Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
        List<String> slaveDataSourceNames = new ArrayList<String>();
        targetDataSources.put(ConstantPool.DATASOURCE_MASTER, masterDataSource);
        targetDataSources.put(ConstantPool.DATASOURCER_SLAVE, slaveDataSource);
        slaveDataSourceNames.add(ConstantPool.DATASOURCER_SLAVE);
        writeSplitRoutingDataSource.setTargetDataSources(targetDataSources);
        writeSplitRoutingDataSource.setDefaultTargetDataSource(masterDataSource);
        ReadOnlyConnectionAspect.setSlaveDataSourceNames(slaveDataSourceNames);
        return writeSplitRoutingDataSource;
    }

    /**
     * 主数据库(写数据库)
     * @return
     */
    @Bean(name = "masterDataSource", destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        System.out.println("主库");
        return DataSourceBuilder.create().type(dataSourceType).build();
    }

    /** 
     * 从数据库1(只读数据库)
     * @return 
     */  
    @Bean(name = "slaveDataSource")  
    @ConfigurationProperties(prefix = "spring.datasource.slave")  
    public DataSource slaveDataSource(){  
        System.out.println("从库");
        return DataSourceBuilder.create().type(dataSourceType).build();  
    }  

    /**
     * 注册ServletRegistrationBean
     * 
     * @return
     */
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("allow", ""); // 白名单
        return reg;
    }

    /**
     * 注册FilterRegistrationBean
     * 
     * @return
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");
        filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");
        filterRegistrationBean.addInitParameter("DruidWebStatFilter", "/*");
        return filterRegistrationBean;
    }
}

这里我们忽略了对数据库连接池监控的用户名密码等配置,我们可以在项目中的权限控制中配置,达到特定账号权限可以监控的效果。

5.路由

import com.center.constant.ConstantPool;

/**
 * 线程私有路由配置,用于ReadWriteSplitRoutingDataSource动态读取配置
 * 
 * @Title: DbContextHolder.java
 * @Package com.template.db
 * @Description: TODO
 * @author Autumn、
 * @date 2018年8月5日
 */
public class DbContextHolder {

    /**
     * 当前数据库类型
     */
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    /**
     * 设置数据库类型
     * 
     * @param dbType
     */
    public static void setDbType(String dbType) {
        if (dbType == null)
            throw new NullPointerException();
        contextHolder.set(dbType);
    }

    /**
     * 获取数据库类型
     * 
     * @return
     */
    public static String getDbType() {
        return contextHolder.get() == null ? ConstantPool.DATASOURCE_MASTER : contextHolder.get();
    }

    /**
     * 清除数据库类型
     */
    public static void clearDbType() {
        contextHolder.remove();
    }

}

6.动态数据源

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
 * 实现可动态路由的数据源,在每次数据库查询操作前执行
 * @Title: ReadWriteSplitRoutingDataSource.java
 * @Package com.template.db
 * @Description: TODO
 * @author Autumn、
 * @date 2018年8月5日
 */
public class ReadWriteSplitRoutingDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DbContextHolder.getDbType();
    }

}

7.只读注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 只读注解,用于标注方法的数据库操作只走从库
 * 
 * @Title: ReadOnlyConnection.java
 * @Package com.template.db
 * @Description: TODO
 * @author Autumn、
 * @date 2018年8月5日
 */
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ReadOnlyConnection {
}

8.只读注解切面

package com.center.support.database;

import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.center.constant.ConstantPool;

/**
 * 动态数据源绑定,指定只读数据源(从库)
 * 
 * @Title: ReadOnlyConnectionAspect.java
 * @Package com.template.db
 * @Description: TODO
 * @author Autumn、
 * @date 2018年8月5日
 */
@Aspect
@Component
@Order(5)
public class ReadOnlyConnectionAspect {

    /**
     * 从库列表
     */
    private static List<String> slaveDataSourceNames = null;
    /**
     * 数据库读取次数
     */
    private static Integer count = 0;

    /**
     * @return the slaveDataSourceNames
     */
    public static List<String> getSlaveDataSourceNames() {
        return slaveDataSourceNames;
    }

    /**
     * @param slaveDataSourceNames
     *            the slaveDataSourceNames to set
     */
    public static void setSlaveDataSourceNames(List<String> slaveDataSourceNames) {
        ReadOnlyConnectionAspect.slaveDataSourceNames = slaveDataSourceNames;
    }

    /**
     * 切换到从库
     *
     * @param proceedingJoinPoint
     * @param readOnlyConnection
     * @return
     * @throws Throwable
     */
    @Around("@annotation(readOnlyConnection)")
    public Object proceed(ProceedingJoinPoint proceedingJoinPoint, ReadOnlyConnection readOnlyConnection)
            throws Throwable {
        try {
            if (slaveDataSourceNames != null && slaveDataSourceNames.size() > 0) {
                DbContextHolder.setDbType(slaveDataSourceNames.get(count % slaveDataSourceNames.size()));
                count++;
            } else {
                DbContextHolder.setDbType(ConstantPool.DATASOURCER_SLAVE);
            }
            Object result = proceedingJoinPoint.proceed();
            return result;
        } finally {
            DbContextHolder.clearDbType();
        }
    }
}

9.测试,使用Spring Data JPA,其他同理

package com.test.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.backstage.domain.User;
import com.backstage.repository.UserRepository;
import com.template.db.ReadOnlyConnection;

/**
 * @Title: RWTest1.java
 * @Package com.test.service
 * @Description: TODO
 * @author Autumn、
 * @date 2018年8月5日
 */
@Service
public class RWTest1 {

    @Autowired
    private UserRepository userRepository;

    public List<User> findUser1() {
        return userRepository.findAll();
    }

    @ReadOnlyConnection
    public List<User> findUser2() {
        return userRepository.findAll();
    }

    /**
     * @return the userRepository
     */
    public UserRepository getUserRepository() {
        return userRepository;
    }

    /**
     * @param userRepository
     *            the userRepository to set
     */
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

}

之后就可以通过监控页面查看数据连接池情况

http://localhost:8088/Center/druid/

可以看到有两个数据库连接

springboot aop动态数据源切换 springboot动态数据源连接池_读写分离

springboot aop动态数据源切换 springboot动态数据源连接池_MySQL_02

10.补充1(利用AOP和自定义注解指定特定的数据源操作)

package com.center.support.database;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 指定使用的数据库连接
 * @Title: AssignConnection.java
 * @Package com.template.db
 * @Description: TODO
 * @author Autumn、
 * @date 2018年8月5日
 */
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface AssignConnection {

    /**
     * 指定使用的数据源的key
     * 在config中dataSource需要配置该连接池名称
     * targetDataSources.put(TemplateConstantPool.DATASOURCE_MASTER, dataSourceMaster);
     * @return 数据源key
     */
    String value();

}
package com.center.support.database;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 动态数据源绑定,切换至指定数据源
 * 
 * @Title: AssignConnectionAspect.java
 * @Package com.template.db
 * @Description: TODO
 * @author Autumn、
 * @date 2018年8月5日
 */
@Aspect
@Component
@Order(10)
public class AssignConnectionAspect {

    /**
     * 切换到从库
     *
     * @param proceedingJoinPoint
     * @param readOnlyConnection
     * @return
     * @throws Throwable
     */
    @Around("@annotation(assignConnection)")
    public Object proceed(ProceedingJoinPoint proceedingJoinPoint, AssignConnection assignConnection) throws Throwable {
        try {
            DbContextHolder.setDbType(assignConnection.value());
            Object result = proceedingJoinPoint.proceed();
            return result;
        } finally {
            DbContextHolder.clearDbType();
        }
    }

}

11.补充2(Druid与Spring关联,配置Spring监控)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置_Druid和Spring关联监控配置 -->
    <bean id="druid-stat-interceptor"
        class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>

    <!-- 方法名正则匹配拦截配置 -->
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
        scope="prototype">
        <property name="patterns">
            <list>
                <value>com.center.*</value>
            </list>
        </property>
    </bean>

    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="druid-stat-interceptor"
            pointcut-ref="druid-stat-pointcut" />
    </aop:config>

</beans>