[关闭]
@PheonixHkbxoic 2018-03-07T22:59:13.000000Z 字数 3177 阅读 984

SpringBoot-Druid

SpringBoot



1.POM添加依赖

  1. <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid</artifactId>
  5. <version>${druid.version}</version>
  6. </dependency>

2.数据源配置

  1. spring:
  2. datasource:
  3. #连接池基本配置
  4. driver-class-name: com.mysql.jdbc.Driver
  5. url: jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
  6. username: root
  7. password: root
  8. type: com.alibaba.druid.pool.DruidDataSource
  9. # 数据库连接池补充配置
  10. minIdle: 5
  11. maxActive: 20
  12. initialSize: 5
  13. timeBetweenEvictionRunsMillis: 3000
  14. minEvictableIdleTimeMillis: 300000
  15. validationQuery: SELECT 'ZTM' FROM DUAL
  16. testWhileIdle: true
  17. testOnBorrow: false
  18. testOnReturn: false
  19. # 打开PSCache,并且指定每个连接上PSCache的大小
  20. poolPreparedStatements: true
  21. maxPoolPreparedStatementPerConnectionSize: 20
  22. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  23. filters: stat,wall,log4j
  24. # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  25. connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  26. # 合并多个DruidDataSource的监控数据
  27. useGlobalDataSourceStat: true

type须配置成DruidDataSource,其它的可以不用配置(上面已有说明)

3.数据源配置类

  1. package cn.pheker.springbootdemo.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.Primary;
  7. import javax.sql.DataSource;
  8. /**
  9. * <pre>
  10. * author cn.pheker
  11. * date 2018/3/7 9:03
  12. * email 1176479642@qq.com
  13. * desc 配置Druid数据源,并全之优先被使用
  14. *
  15. * </pre>
  16. */
  17. @Configuration
  18. public class DruidDataSourceConfiguration {
  19. //相关的参数会自动赋值到datasource里
  20. @Bean
  21. @ConfigurationProperties(prefix = "spring.datasource")
  22. @Primary
  23. public DataSource dataSource(){
  24. return new DruidDataSource();
  25. }
  26. }

4.过滤器类

  1. package cn.pheker.springbootdemo.filter;
  2. import com.alibaba.druid.support.http.WebStatFilter;
  3. import javax.servlet.annotation.WebFilter;
  4. import javax.servlet.annotation.WebInitParam;
  5. /**
  6. * <pre>
  7. * author cn.pheker
  8. * date 2018/3/7 9:19
  9. * email 1176479642@qq.com
  10. * desc Druid过滤器
  11. *
  12. * </pre>
  13. */
  14. @WebFilter(filterName = "druidStatFilter",urlPatterns = "/*",
  15. initParams = {
  16. // 忽略资源
  17. @WebInitParam(name = "excusions",value = "/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico")
  18. })
  19. public class DruidStatFilter extends WebStatFilter {
  20. }

5.Servelet类

  1. package cn.pheker.springbootdemo.filter;
  2. import com.alibaba.druid.support.http.StatViewServlet;
  3. import javax.servlet.annotation.WebInitParam;
  4. import javax.servlet.annotation.WebServlet;
  5. /**
  6. * <pre>
  7. * author cn.pheker
  8. * date 2018/3/7 9:25
  9. * email 1176479642@qq.com
  10. * desc Druid监控界面Servlet
  11. *
  12. * </pre>
  13. */
  14. @WebServlet(urlPatterns = "/druid/*",
  15. initParams = {
  16. @WebInitParam(name = "allow",value = "127.0.0.1,10.96.74.187"),// IP白名单 (没有配置或者为空,则允许所有访问)
  17. @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)
  18. @WebInitParam(name="loginUsername",value="admin"),// 用户名
  19. @WebInitParam(name="loginPassword",value="admin"),// 密码
  20. @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
  21. }
  22. )
  23. public class DruidStatViewServlet extends StatViewServlet {
  24. }

6.启动类添加扫描servlet注解

@ServletComponentScan //扫描Servlet

7.访问页面

http://localhost:8080/druid[/login.html]
用户名和密码就是servlet中配置的admin/admin

8.相关链接

  1. 新手注解配置springboot+druid
  2. Druid常见问题
  3. Spring Boot 使用 Druid 和监控配置
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注