Github:https://github.com/alibaba/druid
Wiki:https://github.com/alibaba/druid/wiki
官网:https://druid.apache.org/

Spring Boot 整合第三方技术的两种方式
1、自定义
2、找 starter

先来讲第一种

1、自定义方式

1.1、引入 Maven 依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid</artifactId>
  4. <version>1.2.5</version>
  5. </dependency>

1.2、编写配置类

  1. @Configuration
  2. public class MyDataSourceConfig {
  3. @ConfigurationProperties("spring.datasource")
  4. @Bean
  5. public DataSource dataSource() throws SQLException {
  6. DruidDataSource druidDataSource = new DruidDataSource();
  7. // 加入监控/防火墙 功能
  8. // druidDataSource.setFilters("stat,wall");
  9. // 最大活跃线程数
  10. // druidDataSource.setMaxActive(12);
  11. return druidDataSource;
  12. }
  13. /**
  14. *
  15. */
  16. @Bean
  17. public ServletRegistrationBean StatViewServlet() {
  18. StatViewServlet statViewServlet = new StatViewServlet();
  19. ServletRegistrationBean<StatViewServlet> statViewServletServletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(statViewServlet, "/druid/*");
  20. // 配置 druid 账号密码
  21. statViewServletServletRegistrationBean.addInitParameter("loginUsername", "admin");
  22. statViewServletServletRegistrationBean.addInitParameter("loginPassword", "admin");
  23. return statViewServletServletRegistrationBean;
  24. }
  25. @Bean
  26. public FilterRegistrationBean webStatFilter() {
  27. WebStatFilter webStatFilter = new WebStatFilter();
  28. FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
  29. webStatFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
  30. webStatFilterFilterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  31. return webStatFilterFilterRegistrationBean;
  32. }
  33. }

1.3、application.yml

  1. spring:
  2. datasource:
  3. url: jdbc:mysql://192.168.20.128:3306/db_test
  4. username: db_test
  5. password: 12345678
  6. driver-class-name: com.mysql.jdbc.Driver
  7. type: com.zaxxer.hikari.HikariDataSource
  8. filters: stat,wall # 开启监控统计、监控

1.4、controller

  1. @Controller
  2. @Slf4j
  3. public class IndexController {
  4. @Autowired
  5. JdbcTemplate jdbcTemplate;
  6. @ResponseBody
  7. @GetMapping("/sql")
  8. public String queryFromDb() {
  9. Long departCount = jdbcTemplate.queryForObject("select count(*) from t_s_depart", Long.class);
  10. log.info("部门总数:{}", departCount);
  11. ArrayList departs = (ArrayList) jdbcTemplate.queryForList("select * from t_s_depart");
  12. // List departs = jdbcTemplate.queryForList("select * from t_s_depart");
  13. log.info("部门信息:{}", departs);
  14. return departCount.toString();
  15. }
  16. }

1.5、测试

Druid 监控页面:http://127.0.0.1/druid 账号:admin 密码:admin
模拟请求:http://127.0.0.1/sql

image.png
image.png

现在是可以用起来了,但是这个配置过程实在是太麻烦了。

现在来看第二种方法。

2、引入 starter 方式

2.1、引入场景启动器依赖

文档:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid-spring-boot-starter</artifactId>
  4. <version>1.2.6</version>
  5. </dependency>

2.2、application.yml

  1. spring:
  2. datasource:
  3. url: jdbc:mysql://192.168.20.128:3306/db_jeecg
  4. username: db_jeecg
  5. password: Rebox1930!
  6. driver-class-name: com.mysql.jdbc.Driver
  7. druid:
  8. filters: stat,wall
  9. aop-patterns: com.example.admin.*
  10. stat-view-servlet:
  11. enabled: true
  12. login-username: admin
  13. login-password: admin
  14. web-stat-filter:
  15. enabled: true
  16. url-pattern: /*
  17. exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
  18. filter:
  19. stat:
  20. slow-sql-millis: 1000
  21. log-slow-sql: true
  22. enabled: true
  23. wall:
  24. enabled: true

2.3、controller

  1. @Controller
  2. @Slf4j
  3. public class IndexController {
  4. @Autowired
  5. JdbcTemplate jdbcTemplate;
  6. @ResponseBody
  7. @GetMapping("/sql")
  8. public String queryFromDb() {
  9. Long departCount = jdbcTemplate.queryForObject("select count(*) from t_s_depart", Long.class);
  10. log.info("部门总数:{}", departCount);
  11. ArrayList departs = (ArrayList) jdbcTemplate.queryForList("select * from t_s_depart");
  12. // List departs = jdbcTemplate.queryForList("select * from t_s_depart");
  13. log.info("部门信息:{}", departs);
  14. return departCount.toString();
  15. }
  16. }

2.4、测试

Druid 监控页面:http://127.0.0.1/druid 账号:admin 密码:admin
模拟请求:http://127.0.0.1/sql

image.png
image.png

推荐使用 starter 方式