🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
SpringBoot 默认的数据源为 com.zaxxer.hikari.HikariDataSource ,虽然该数据源比阿里的 Druid 性能高,但是 Druid 数据源提供了一套安全的机制和监控,所以开发中常用 Druid 而不是 Hikari。 [TOC] # 1. 切换为Druid数据源 步骤如下: **1. 引入druid-spring-boot-starter数据源** ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> ``` **2. 切换到Druid数据源** *`resources/application.yml`* ```xml spring: datasource: username: root password: admin url: jdbc:mysql://127.0.0.1:3306/learn?useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource ``` **3. 启动项目控制台打印如下信息说明切换到Druid数据源了** ```xml main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited ``` <br/> # 2. 自定义Druid数据源配置 我们可以在Druid数据源的基础上添加一些自定义配置。 <br/> **1. `resources/application.yml`** ```xml spring: datasource: username: root password: admin url: jdbc:mysql://127.0.0.1:3306/learn?useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poo1PreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall 用于防火墙 filters: stat ,wall maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 ``` **2. 编写配置类** 新添加的属性在数据源属性类 DataSourceProperties 上是没有的,所以我们需要自定义一个配置类,将上面这些新增的属性生效。 ```java import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; 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 javax.sql.DataSource; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @Configuration public class CustomDruidDataSourceConfig { /** * 让新增的配置生效 */ @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druid() { return new DruidDataSource(); } /** * 配置Druid监控 */ @Bean public ServletRegistrationBean servletRegistrationBean() { //监控 /druid/ 请求 ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String, String> initParams = new HashMap<>(1); //登录后台的用户名和密码 initParams.put("loginUsername", "admin"); initParams.put("loginPassword", "123456"); //允许localhost访问,如果不设置默认所有人可访问 initParams.put("allow", "localhost"); //拒绝该ip地址访问 initParams.put("deny", "192.168.15.21"); bean.setInitParameters(initParams); return bean; } /** * 配置一个过滤器 */ @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean bean = new FilterRegistrationBean(); //注册Filter bean.setFilter(new WebStatFilter()); Map<String, String> initParams = new HashMap<>(); //不拦截 *.js,*.css,/druid/* 请求 initParams.put("exclusions", "*.js,*.css,/druid/*"); bean.setInitParameters(initParams); // 拦截所有请求 bean.setUrlPatterns(Arrays.asList("/*")); return bean; } } ``` **3. 测试** 启动项目后访问 http://localhost:8080/druid/ ,输入在 CustomDruidDataSourceConfig 中设置的用户名和密码就可以进入Druid后台监控。 ![](https://img.kancloud.cn/9c/43/9c43eb4d6f618fe3460f7926a0af0fae_2256x685.png)