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、编写配置类
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
// 加入监控/防火墙 功能
// druidDataSource.setFilters("stat,wall");
// 最大活跃线程数
// druidDataSource.setMaxActive(12);
return druidDataSource;
}
/**
*
*/
@Bean
public 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;
}
@Bean
public 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_test
username: db_test
password: 12345678
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
filters: stat,wall # 开启监控统计、监控
1.4、controller
@Controller
@Slf4j
public class IndexController {
@Autowired
JdbcTemplate 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_jeecg
username: db_jeecg
password: Rebox1930!
driver-class-name: com.mysql.jdbc.Driver
druid:
filters: stat,wall
aop-patterns: com.example.admin.*
stat-view-servlet:
enabled: true
login-username: admin
login-password: admin
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
filter:
stat:
slow-sql-millis: 1000
log-slow-sql: true
enabled: true
wall:
enabled: true
2.3、controller
@Controller
@Slf4j
public class IndexController {
@Autowired
JdbcTemplate 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 方式