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 依赖
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.5</version></dependency>
1.2、编写配置类
@Configurationpublic class MyDataSourceConfig {@ConfigurationProperties("spring.datasource")@Beanpublic DataSource dataSource() throws SQLException {DruidDataSource druidDataSource = new DruidDataSource();// 加入监控/防火墙 功能// druidDataSource.setFilters("stat,wall");// 最大活跃线程数// druidDataSource.setMaxActive(12);return druidDataSource;}/****/@Beanpublic ServletRegistrationBean StatViewServlet() {StatViewServlet statViewServlet = new StatViewServlet();ServletRegistrationBean<StatViewServlet> statViewServletServletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(statViewServlet, "/druid/*");// 配置 druid 账号密码statViewServletServletRegistrationBean.addInitParameter("loginUsername", "admin");statViewServletServletRegistrationBean.addInitParameter("loginPassword", "admin");return statViewServletServletRegistrationBean;}@Beanpublic FilterRegistrationBean webStatFilter() {WebStatFilter webStatFilter = new WebStatFilter();FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);webStatFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));webStatFilterFilterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return webStatFilterFilterRegistrationBean;}}
1.3、application.yml
spring:datasource:url: jdbc:mysql://192.168.20.128:3306/db_testusername: db_testpassword: 12345678driver-class-name: com.mysql.jdbc.Drivertype: com.zaxxer.hikari.HikariDataSourcefilters: stat,wall # 开启监控统计、监控
1.4、controller
@Controller@Slf4jpublic class IndexController {@AutowiredJdbcTemplate jdbcTemplate;@ResponseBody@GetMapping("/sql")public String queryFromDb() {Long departCount = jdbcTemplate.queryForObject("select count(*) from t_s_depart", Long.class);log.info("部门总数:{}", departCount);ArrayList departs = (ArrayList) jdbcTemplate.queryForList("select * from t_s_depart");// List departs = jdbcTemplate.queryForList("select * from t_s_depart");log.info("部门信息:{}", departs);return departCount.toString();}}
1.5、测试
Druid 监控页面:http://127.0.0.1/druid 账号:admin 密码:admin
模拟请求:http://127.0.0.1/sql


现在是可以用起来了,但是这个配置过程实在是太麻烦了。
现在来看第二种方法。
2、引入 starter 方式
2.1、引入场景启动器依赖
文档:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version></dependency>
2.2、application.yml
spring:datasource:url: jdbc:mysql://192.168.20.128:3306/db_jeecgusername: db_jeecgpassword: Rebox1930!driver-class-name: com.mysql.jdbc.Driverdruid:filters: stat,wallaop-patterns: com.example.admin.*stat-view-servlet:enabled: truelogin-username: adminlogin-password: adminweb-stat-filter:enabled: trueurl-pattern: /*exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'filter:stat:slow-sql-millis: 1000log-slow-sql: trueenabled: truewall:enabled: true
2.3、controller
@Controller@Slf4jpublic class IndexController {@AutowiredJdbcTemplate jdbcTemplate;@ResponseBody@GetMapping("/sql")public String queryFromDb() {Long departCount = jdbcTemplate.queryForObject("select count(*) from t_s_depart", Long.class);log.info("部门总数:{}", departCount);ArrayList departs = (ArrayList) jdbcTemplate.queryForList("select * from t_s_depart");// List departs = jdbcTemplate.queryForList("select * from t_s_depart");log.info("部门信息:{}", departs);return departCount.toString();}}
2.4、测试
Druid 监控页面:http://127.0.0.1/druid 账号:admin 密码:admin
模拟请求:http://127.0.0.1/sql


推荐使用 starter 方式
