一、SpringBoot引入Druid的依赖包
Xml代码
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.1.9
二、设置配置文件application.properties
Java代码
spring.datasource.url=jdbc:mysql://192.168.1.166:3306/study
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.schema=
#数据源其他配置,需要增加Druid的配置类(不然不生效)
#配置见:com.lqy.springboot.datasource.druid.DruidConfig
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.useGlobalDataSourceStat=true
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
三、增加Druid自定义配置类
为什么要增加呢?
因为SpringBoot本身提供的配置只是部分的数据库连接配置,很多Druid需要的属性配置是没有的,如:
Java代码
1. spring.datasource.initialSize
这个属性,SpringBoot是没有的,只有Druid才有,所以需要增加相应的配置类,自动注入。
DruidConfig 类:
Java代码
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* 导入druid数据源,扩展相关的配置属性
*
*/
@Configuration
public class DruidConfig {
/**
* 配置Druid的属性,和DataSource进行绑定,前缀设置为:spring.datasource
* 不配置的话,很多初始化的属性是没有绑定的
* @return
*/
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource druid() {
return new DruidDataSource();
}
/**
* 配置druid监控
* 配置一个管理后台的servlet
* 访问地址:http://localhost:8080/druid/
* @return
*/
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map initParameters = new HashMap();
initParameters.put("loginUsername", "admin");//属性见:com.alibaba.druid.support.http.ResourceServlet
initParameters.put("loginPassword", "123456");
initParameters.put("allow", "");//默认允许所有
initParameters.put("deny", "");
bean.setInitParameters(initParameters);
return bean;
}
/**
* 配置一个web监控的filter
* @return
*/
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean filterBean = new FilterRegistrationBean();
filterBean.setFilter(new WebStatFilter());
filterBean.setUrlPatterns(Arrays.asList("/*"));
Map initParameters = new HashMap();
initParameters.put("exclusions", "*.js,*.css,/druid/*");//属性见:com.alibaba.druid.support.http.WebStatFilter
filterBean.setInitParameters(initParameters);
return filterBean;
}
}
四、启动项目,测试数据源连接:
Java代码
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DataSourceController {
@Autowired
private DataSource dataSource;
@RequestMapping("/dataSource")
public String dataSource() {
try {
System.out.println("dataSource = "+dataSource);
Connection conn = dataSource.getConnection();
System.out.println("conn = "+conn);
return "success";
} catch (SQLException e) {
e.printStackTrace();
}
return "end.";
}
}
五、查看Druid的监控
浏览器打开:http://localhost:8080/druid
登录的账号密码在DruidConfig类配置有
在此我向大家推荐一个架构学习交流群。交流学习群号:575745314 里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多