What is it?
One kind of database connection pool, manages and releases the connection of database, realizes the reuse of connection, and can compare thread pool.
Parameter configuration
What can we do?
1, Connection Pool 2, Monitoring 3, Encryption 4, Extending JDBC
How do you use it?
Adding dependencies:
<!--spring--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency>
<!--springboot --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency>
Connection pool:
spring form
<bean id="dataSourceDefault" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${itoo_jdbc_url}"/> <property name="username" value="${itoo_jdbc_username}"/> <property name="password" value="${itoo_jdbc_password}"/> <property name="maxActive" value="${itoo_jdbc_maxActive}"/> <property name="initialSize" value="${itoo_jdbc_initialSize}"/> <property name="maxWait" value="${itoo_jdbc_maxWait}"/> <property name="minIdle" value="${itoo_jdbc_minIdle}"/> <property name="validationQuery" value="${itoo_jdbc_validationQuery}"/> // sql used to check whether the connection is valid <property name="testWhileIdle" value="${itoo_jdbc_testWhileIdle}"/> //Timeout detection <property name="testOnBorrow" value="${itoo_jdbc_testOnBorrow}"/> <property name="testOnReturn" value="${itoo_jdbc_testOnReturn}"/> <property name="filters" value="mycat,stat,config"/> // mycat is a custom-configured filter plug-in, which will be explained next <property name="connectionProperties" value="${connectionProperties}"/> </bean>
spring-boot mode:
datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://192.168.22.58:3306/aa username: root password: root druid: initial-size: 2 min-idle: 2 max-active: 50 validation-query: SELECT 1 test-while-idle: true aop-patterns: com.dmsdbj.integral.provider.* // spring monitoring page configuration # Configure filters that are intercepted by monitoring statistics, and sql can not be counted after removing them.'wall'is used for firewall filters: stat,wall,log4j stat-view-servlet: url-pattern: /druid/* reset-enable: true login-username: druid login-password: druid
Monitoring:
<!--spring Formal configuration spring Monitor--> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"/> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> <property name="patterns"> <list> <value>com.tfjybj.itoo.exam.provider.service.*</value> <value>com.tfjybj.itoo.exam.provider.dao.*</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>
<!--springboot form --> aop-patterns: com.dmsdbj.integral.provider.*
Encryption:
Filter plug-in
config ,stat ,wall
Custom filter plug-in
mycat
It is modified on druid, not on p6spy, because sql without parameters is still transmitted to druid, and p6spy is only bypass monitoring.
Define a tool class
<!--springboot form --> @Configuration public class DruidMycatConfig { private static final String FILTER_MYCAT_PREFIX = "spring.datasource.druid.filter.mycat"; @Bean @ConfigurationProperties(FILTER_MYCAT_PREFIX) @ConditionalOnProperty(prefix = FILTER_MYCAT_PREFIX, name = "enabled", matchIfMissing = true) @ConditionalOnMissingBean public DruidMycatFilter mycatFilter(){ return new DruidMycatFilter(); } }
public class DruidMycatFilter extends WallFilter implements Filter { public DruidMycatFilter() { this.setDbType("mysql"); this.configFromProperties(System.getProperties()); } <!--critical code --> public String check(String sql) { String schema; if(TenancyContext.TenancyID.get() != null) { String schema = (String)TenancyContext.TenancyID.get(); schema = "/* !mycat:schema = " + schema + " */ "; sql = sql.startsWith(schema)?sql:schema + sql; } }
Spring Form:
1. Add META-INF file in the corresponding resource of the web and druid-filter.properties configuration in the file.
2. Add druid filter configuration in spring-db.xml: <property name= "filters" value= "mycat, stat, config"/>