欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot 整合druid

程序员文章站 2022-07-10 18:32:53
目录1. maven依赖2. yml配置3. druid配置类编写https://github.com/sevenyoungairye/spring-boot-studydruid优点:提供性能监控,配置灵活丰富1. maven依赖 mysql mysql-connect...


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: root
    password: 123456
    # springboot默认数据源com.zaxxer.hikari.HikariDataSource # 比druid快
    url: jdbc:mysql://localhost:3306/mybatis_study?serverTimezone=GMT
    driver-class-name: com.mysql.cj.jdbc.Driver

    # 切换成druid 有性能监控...
    type: com.alibaba.druid.pool.DruidDataSource

    # 下面为连接池的补充设置,应用到上面所有数据源中
    # 初始化大小,最小,最大
    initial-size: 5
    min-idle: 5
    max-active: 20
    # 配置获取连接等待超时的时间
    max-wait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    time-between-eviction-runs-millis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    min-evictable-idle-time-millis: 300000
    validation-query: SELECT 1 FROM DUAL
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    # 打开PSCache,并且指定每个连接上PSCache的大小
    pool-prepared-statements: true
    #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    max-pool-prepared-statement-per-connection-size: 20
    filters: stat,wall
    use-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")
    @Bean
    public DruidDataSource druidDataSource() {

        return new DruidDataSource();
    }

    // 后台监控: web.xml ServletRegistrationBean
    // spring boot 内置了servlet 没有web.xml配置文件,所以用下面的方式配置
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new
                ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        Map<String, String> initParameters = new HashMap<>();

        // 后台登录的账号密码
        // initParameters.put("jmxUsername", "admin"); // loginUsername
        // initParameters.put("jmxPassword", "123"); // loginPassword
        initParameters.put("loginUsername", "admin"); // loginUsername
        initParameters.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
     */
    @Bean
    public 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. 后台性能监控

springboot 整合druid
springboot 整合druid
请忽略广告…
springboot 整合druid

本文地址:https://blog.csdn.net/qq_44783283/article/details/109273616