• Springboot整合Druid


    Springboot整合Druid

    POM

    	<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>druid</artifactId>
    			<version>1.1.1</version>
    	</dependency>
    	<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<optional>true</optional>
    	</dependency>
    

    yml配置

    druid:
      allow:
        ip: 127.0.0.1
      login:
        user_name: root
        password: root
    
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/xdclass?autoReconnect=true&useUnicode=true&characterEncoding=UTF8&useSSL=false
        username: root
        password: cpf553457177
        type: com.alibaba.druid.pool.DruidDataSource
        # 连接池的配置信息
        # 初始化⼤⼩,最⼩等待连接数量,最⼤等待连接数量,最⼤连接数
        initialSize: 1
        minIdle: 1
        maxIdle: 5
        maxActive: 20
        # 配置获取连接等待超时的时间
        maxWait: 60000
        # 配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒
        timeBetweenEvictionRunsMillis: 60000
        # 配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: true
        testOnReturn: false
        # 打开PSCache,并且指定每个连接上PSCache的⼤⼩
        poolPreparedStatements: false
        maxPoolPreparedStatementPerConnectionSize: 20
        # 配置监控统计拦截的filters,去掉后监控界⾯sql⽆法统计,'wall'⽤于防⽕墙
        filters: stat,wall
        # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
        # 合并多个DruidDataSource的监控数据
        #spring.datasource.useGlobalDataSourceStat=true
    

    Bean配置

    package com.fly.coupon_app.config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import com.alibaba.druid.support.http.StatViewServlet;
    import com.alibaba.druid.support.http.WebStatFilter;
    import org.springframework.beans.factory.annotation.Value;
    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.HashMap;
    import java.util.Map;
    
    
    @Configuration
    public class DruidConfig {
    
        @Value("${druid.login.user_name}")
        private String userName;
        @Value("${druid.login.password}")
        private String password;
        /**
         * 必须配置数据源,不然⽆法获取到sql监控,与sql防⽕墙监控
         */
        @Bean(name = "default_databaseSource")
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource druidDataSource() {
            return new DruidDataSource();
        }
        @Bean
        public ServletRegistrationBean druidServlet() {
            ServletRegistrationBean servletRegistrationBean = new
                    ServletRegistrationBean();
            servletRegistrationBean.setServlet(new StatViewServlet());
            servletRegistrationBean.addUrlMappings("/druid/*");
            Map<String, String> initParameters = new HashMap<>();
            initParameters.put("loginUsername", userName);// ⽤户名
            initParameters.put("loginPassword", password);// 密码
            initParameters.put("resetEnable", "false");// 禁⽤HTML⻚⾯上的“Reset All”功能
            servletRegistrationBean.setInitParameters(initParameters);
            return servletRegistrationBean;
        }
        @Bean
        public FilterRegistrationBean filterRegistrationBean() {
            FilterRegistrationBean filterRegistrationBean = new
                    FilterRegistrationBean();
            filterRegistrationBean.setFilter(new WebStatFilter());
            filterRegistrationBean.addUrlPatterns("/*");
            filterRegistrationBean.addInitParameter("exclusions",
                    "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
            return filterRegistrationBean;
        }
    
    }
    

    访问网址:http://localhost:8091/druid/index.html

  • 相关阅读:
    'static' can indeed be used in C++ to create a Static Member Function
    关闭QQ2008迷你首页
    开机无法使用欢迎屏幕
    关于U盘”无法复制:磁盘被写保护…”的解决办法
    SQL企业管理器下servers组下无项目解决方法
    COM+应用程序错误8004E00F COM+ 无法与Microsoft 分布
    安装Ms SQL Server 2005 开发版时出现性能计数器要求安装错误的解决办法
    牛人教你这样用Google
    在K3凭证处理中的部份实用操作
    KIS7.5SP2迷你版、标准版在查询数量金额明细账时提示“发生未知错误,系统当前操作被取消,请与金蝶公司的技术支持机构联系”
  • 原文地址:https://www.cnblogs.com/bigfly277/p/11572815.html
Copyright © 2020-2023  润新知