@PheonixHkbxoic
2018-03-07T14:59:13.000000Z
字数 3177
阅读 1104
SpringBoot
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency>
spring:datasource:#连接池基本配置driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTCusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSource# 数据库连接池补充配置minIdle: 5maxActive: 20initialSize: 5timeBetweenEvictionRunsMillis: 3000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 'ZTM' FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: false# 打开PSCache,并且指定每个连接上PSCache的大小poolPreparedStatements: truemaxPoolPreparedStatementPerConnectionSize: 20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙filters: stat,wall,log4j# 通过connectProperties属性来打开mergeSql功能;慢SQL记录connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# 合并多个DruidDataSource的监控数据useGlobalDataSourceStat: true
type须配置成DruidDataSource,其它的可以不用配置(上面已有说明)
package cn.pheker.springbootdemo.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import javax.sql.DataSource;/*** <pre>* author cn.pheker* date 2018/3/7 9:03* email 1176479642@qq.com* desc 配置Druid数据源,并全之优先被使用** </pre>*/@Configurationpublic class DruidDataSourceConfiguration {//相关的参数会自动赋值到datasource里@Bean@ConfigurationProperties(prefix = "spring.datasource")@Primarypublic DataSource dataSource(){return new DruidDataSource();}}
package cn.pheker.springbootdemo.filter;import com.alibaba.druid.support.http.WebStatFilter;import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam;/*** <pre>* author cn.pheker* date 2018/3/7 9:19* email 1176479642@qq.com* desc Druid过滤器** </pre>*/@WebFilter(filterName = "druidStatFilter",urlPatterns = "/*",initParams = {// 忽略资源@WebInitParam(name = "excusions",value = "/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico")})public class DruidStatFilter extends WebStatFilter {}
package cn.pheker.springbootdemo.filter;import com.alibaba.druid.support.http.StatViewServlet;import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;/*** <pre>* author cn.pheker* date 2018/3/7 9:25* email 1176479642@qq.com* desc Druid监控界面Servlet** </pre>*/@WebServlet(urlPatterns = "/druid/*",initParams = {@WebInitParam(name = "allow",value = "127.0.0.1,10.96.74.187"),// IP白名单 (没有配置或者为空,则允许所有访问)@WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)@WebInitParam(name="loginUsername",value="admin"),// 用户名@WebInitParam(name="loginPassword",value="admin"),// 密码@WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能})public class DruidStatViewServlet extends StatViewServlet {}
@ServletComponentScan //扫描Servlet
http://localhost:8080/druid[/login.html]
用户名和密码就是servlet中配置的admin/admin