SpringBoot Druid整合,SpringBoot 集成Druid

时间:2023-12-22 12:01:20

SpringBoot Druid整合,SpringBoot 集成Druid

================================

©Copyright 蕃薯耀 2018年4月8日

http://www.cnblogs.com/fanshuyao/

源代码下载见:http://fanshuyao.iteye.com/blog/2415353

一、SpringBoot引入Druid的依赖包

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba</groupId>
  8. <artifactId>druid</artifactId>
  9. <version>1.1.9</version>
  10. </dependency>

二、设置配置文件application.properties

  1. spring.datasource.url=jdbc:mysql://192.168.1.166:3306/study
  2. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  3. spring.datasource.username=root
  4. spring.datasource.password=root
  5. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  6. #spring.datasource.schema=
  7. #数据源其他配置,需要增加Druid的配置类(不然不生效)
  8. #配置见:com.lqy.springboot.datasource.druid.DruidConfig
  9. spring.datasource.initialSize=5
  10. spring.datasource.minIdle=5
  11. spring.datasource.maxActive=20
  12. spring.datasource.maxWait=60000
  13. spring.datasource.timeBetweenEvictionRunsMillis=60000
  14. spring.datasource.minEvictableIdleTimeMillis=300000
  15. spring.datasource.validationQuery=SELECT 1 FROM DUAL
  16. spring.datasource.testWhileIdle=true
  17. spring.datasource.testOnBorrow=false
  18. spring.datasource.testOnReturn=false
  19. spring.datasource.poolPreparedStatements=true
  20. #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  21. spring.datasource.filters=stat,wall,log4j
  22. spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
  23. spring.datasource.useGlobalDataSourceStat=true
  24. spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

三、增加Druid自定义配置类

为什么要增加呢?

因为SpringBoot本身提供的配置只是部分的数据库连接配置,很多Druid需要的属性配置是没有的,如:

  1. spring.datasource.initialSize

这个属性,SpringBoot是没有的,只有Druid才有,所以需要增加相应的配置类,自动注入。

DruidConfig 类

  1. import java.util.Arrays;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.sql.DataSource;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  7. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import com.alibaba.druid.pool.DruidDataSource;
  11. import com.alibaba.druid.support.http.StatViewServlet;
  12. import com.alibaba.druid.support.http.WebStatFilter;
  13. /**
  14. * 导入druid数据源,扩展相关的配置属性
  15. *
  16. */
  17. @Configuration
  18. public class DruidConfig {
  19. /**
  20. * 配置Druid的属性,和DataSource进行绑定,前缀设置为:spring.datasource
  21. * 不配置的话,很多初始化的属性是没有绑定的
  22. * @return
  23. */
  24. @Bean
  25. @ConfigurationProperties(prefix="spring.datasource")
  26. public DataSource druid() {
  27. return new DruidDataSource();
  28. }
  29. /**
  30. * 配置druid监控
  31. * 配置一个管理后台的servlet
  32. * 访问地址:http://localhost:8080/druid/
  33. * @return
  34. */
  35. @Bean
  36. public ServletRegistrationBean statViewServlet() {
  37. ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
  38. Map<String, String> initParameters = new HashMap<String, String>();
  39. initParameters.put("loginUsername", "admin");//属性见:com.alibaba.druid.support.http.ResourceServlet
  40. initParameters.put("loginPassword", "123456");
  41. initParameters.put("allow", "");//默认允许所有
  42. initParameters.put("deny", "");
  43. bean.setInitParameters(initParameters);
  44. return bean;
  45. }
  46. /**
  47. * 配置一个web监控的filter
  48. * @return
  49. */
  50. @Bean
  51. public FilterRegistrationBean webStatFilter() {
  52. FilterRegistrationBean filterBean = new FilterRegistrationBean();
  53. filterBean.setFilter(new WebStatFilter());
  54. filterBean.setUrlPatterns(Arrays.asList("/*"));
  55. Map<String, String> initParameters = new HashMap<String, String>();
  56. initParameters.put("exclusions", "*.js,*.css,/druid/*");//属性见:com.alibaba.druid.support.http.WebStatFilter
  57. filterBean.setInitParameters(initParameters);
  58. return filterBean;
  59. }
  60. }

 四、启动项目,测试数据源连接:

  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3. import javax.sql.DataSource;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class DataSourceController {
  9. @Autowired
  10. private DataSource dataSource;
  11. @RequestMapping("/dataSource")
  12. public String dataSource() {
  13. try {
  14. System.out.println("dataSource = "+dataSource);
  15. Connection conn = dataSource.getConnection();
  16. System.out.println("conn = "+conn);
  17. return "success";
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. }
  21. return "end.";
  22. }
  23. }

五、查看Druid的监控

浏览器打开:http://localhost:8080/druid

登录的账号密码在DruidConfig类配置有

================================

©Copyright 蕃薯耀 2018年4月8日

http://www.cnblogs.com/fanshuyao/