目录
- 1. maven依赖
- 2. yml配置
- 3. druid配置类编写
- 4. 后台性能监控
https://github.com/sevenyoungairye/spring-boot-study
- druid优点:提供性能监控,配置灵活丰富
1. maven依赖
<!-- mysql驱动 springboot内置 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!--druid 数据源--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.21</version></dependency>
2. yml配置
spring:datasource:username: rootpassword: 123456# springboot默认数据源com.zaxxer.hikari.HikariDataSource # 比druid快url: jdbc:mysql://localhost:3306/mybatis_study?serverTimezone=GMTdriver-class-name: com.mysql.cj.jdbc.Driver# 切换成druid 有性能监控...type: com.alibaba.druid.pool.DruidDataSource# 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大initial-size: 5min-idle: 5max-active: 20# 配置获取连接等待超时的时间max-wait: 60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒time-between-eviction-runs-millis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒min-evictable-idle-time-millis: 300000validation-query: SELECT 1 FROM DUALtest-while-idle: truetest-on-borrow: falsetest-on-return: false# 打开PSCache,并且指定每个连接上PSCache的大小pool-prepared-statements: true# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙max-pool-prepared-statement-per-connection-size: 20filters: stat,walluse-global-data-source-stat: true# 通过connectProperties属性来打开mergeSql功能;慢SQL记录connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
3. druid配置类编写
package cn.bitqian.config;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.servlet.Filter;
import java.util.HashMap;
import java.util.Map;/*** druid 配置类* @author echo lovely* @date 2020/10/23 22:01*/
@Configuration
public class DruidConfig {@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DruidDataSource druidDataSource() {return new DruidDataSource();}// 后台监控: web.xml ServletRegistrationBean// spring boot 内置了servlet 没有web.xml配置文件,所以用下面的方式配置@Beanpublic ServletRegistrationBean druidServlet() {ServletRegistrationBean servletRegistrationBean = newServletRegistrationBean(new StatViewServlet(), "/druid/*");Map<String, String> initParameters = new HashMap<>();// 后台登录的账号密码// initParameters.put("jmxUsername", "admin"); // loginUsername// initParameters.put("jmxPassword", "123"); // loginPasswordinitParameters.put("loginUsername", "admin"); // loginUsernameinitParameters.put("loginPassword", "123"); // loginPassword// 允许访问 白名单initParameters.put("allow", "127.0.0.1");// initParameters.put("deny", "168.403.432.234"); 禁止访问黑名单servletRegistrationBean.setInitParameters(initParameters);return servletRegistrationBean;}/*** 过滤器* @return null*/@Beanpublic FilterRegistrationBean filterRegistrationBean() {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();// 过滤请求filterRegistrationBean.setFilter(new WebStatFilter());Map<String, String> initParameters = new HashMap<>();// 设置静态资源放行initParameters.put("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");filterRegistrationBean.setInitParameters(initParameters);return filterRegistrationBean;}}
4. 后台性能监控
请忽略广告…