前言 druid连接池 没啥好说的 的确好用 虽然极限性能可能跟 hikari低点 但是功能多啊 各种基本sql监控 、扩展等等
https://github.com/alibaba/druid https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
实例
例子在spring boot项目中引用
依赖 1 2 3 4 5 6 7 <druid.starter.version > 1.2.8</druid.starter.version > 。。。。 <dependency > <groupId > com.alibaba</groupId > <artifactId > druid-spring-boot-starter</artifactId > <version > ${druid.starter.version}</version > </dependency >
spring datasource配置
web ui地址 http://xxx.xx/spring-boot-druid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 spring: datasource: driver-class-name: url: username: password: type: com.alibaba.druid.pool.DruidDataSource druid: min-idle: 5 max-active: 15 filter: stat: enabled: true web-stat-filter: enabled: true filters: stat stat-view-servlet: enabled: true url-pattern: /spring-boot-druid/* reset-enable: false allow:
删除druid监控ui页面的广告 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 package com.ming.base.mvc;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;import com.alibaba.druid.util.Utils;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.servlet.*;import java.io.IOException;@Configuration @ConditionalOnWebApplication @AutoConfigureAfter(DruidDataSourceAutoConfigure.class) @ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true) public class RemoveDruidAdConfig { @Bean @SuppressWarnings("unchecked") public FilterRegistrationBean removeDruidAdFilterRegistrationBean (DruidStatProperties properties) { DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*" ; String commonJsPattern = pattern.replaceAll("\\*" , "js/common.js" ); final String filePath = "support/http/resources/js/common.js" ; Filter filter = new Filter () { @Override public void init (FilterConfig filterConfig) throws ServletException { } @Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request, response); response.resetBuffer(); String text = Utils.readFromResource(filePath); text = text.replaceAll("<a.*?banner\"></a><br/>" , "" ); text = text.replaceAll("powered.*?shrek.wang</a>" , "" ); response.getWriter().write(text); } @Override public void destroy () { } }; FilterRegistrationBean registrationBean = new FilterRegistrationBean (); registrationBean.setFilter(filter); registrationBean.addUrlPatterns(commonJsPattern); return registrationBean; } }
自定义druid filter
继承FilterEventAdapter 实现自定义filter
FilterEventAdapter 扩展了一些前置后置操作的函数 一般都继承这个来实现filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.ming.core.orm;import com.alibaba.druid.filter.FilterChain;import com.alibaba.druid.filter.FilterEventAdapter;import com.alibaba.druid.proxy.jdbc.ConnectionProxy;import lombok.extern.slf4j.Slf4j;import java.util.Properties;@Slf4j public class JdbcConnectionFilter extends FilterEventAdapter { @Override public void connection_connectBefore (FilterChain chain, Properties info) { log.info("jdbc-connection之前。。。。。。" ); super .connection_connectBefore(chain, info); } @Override public void connection_connectAfter (ConnectionProxy connection) { log.info("jdbc-connection之后。。。。。。" ); super .connection_connectAfter(connection); } }
在META-INF目录下增加 druid-filter.properties
如果是maven管理 直接在resources下增加META-INF/druid-filter.properties
1 2 druid.filters.my =com.ming.core.orm.JdbcConnectionFilter
1 spring.datasource.druid.filters =my
总结 druid 好用 官方文档也写的很清楚 这里只是作为速查使用