• (二)SpringBoot整合常用框架Druid连接池


    一,在Pom.xml文件加入依赖

    找到<dependencies></dependencies>标签,在标签中添加Druid依赖

    <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>druid</artifactId>
       <version>1.0.29</version>
    </dependency>
    

      

    二:配置数据源连接池信息

    在application.properties文件中添加如下配置

    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    #连接池配置
    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
    # 打开PSCache,并且指定每个连接上PSCache的大小
    spring.datasource.poolPreparedStatements=true
    spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    spring.datasource.filters=stat,wall,slf4j
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    

      

    三:添加配置文件

    在core→configurer下创建DruidDataSourceConfigurer.java 和DruidMonitorConfigurer.java

    DruidDataSourceConfigurer

    package com.example.demo.core.configurer;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.sql.DataSource;
    import java.sql.SQLException;
    
    /**
     * @author 
     * @Description:
     * @time 2018/4/18 15:16
     */
    @Configuration
    public class DruidDataSourceConfigurer {
    
        private Logger logger = LoggerFactory.getLogger(DruidDataSourceConfigurer.class);
    
        @Value("${spring.datasource.url}")
        private String dbUrl;
    
        @Value("${spring.datasource.username}")
        private String username;
    
        @Value("${spring.datasource.password}")
        private String password;
    
        @Value("${spring.datasource.driverClassName}")
        private String driverClassName;
    
        @Value("${spring.datasource.initialSize}")
        private int initialSize;
    
        @Value("${spring.datasource.minIdle}")
        private int minIdle;
    
        @Value("${spring.datasource.maxActive}")
        private int maxActive;
    
        @Value("${spring.datasource.maxWait}")
        private int maxWait;
    
        @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
        private int timeBetweenEvictionRunsMillis;
    
        @Value("${spring.datasource.minEvictableIdleTimeMillis}")
        private int minEvictableIdleTimeMillis;
    
        @Value("${spring.datasource.validationQuery}")
        private String validationQuery;
    
        @Value("${spring.datasource.testWhileIdle}")
        private boolean testWhileIdle;
    
        @Value("${spring.datasource.testOnBorrow}")
        private boolean testOnBorrow;
    
        @Value("${spring.datasource.testOnReturn}")
        private boolean testOnReturn;
    
        @Value("${spring.datasource.poolPreparedStatements}")
        private boolean poolPreparedStatements;
    
        @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
        private int maxPoolPreparedStatementPerConnectionSize;
    
        @Value("${spring.datasource.filters}")
        private String filters;
    
        @Value("{spring.datasource.connectionProperties}")
        private String connectionProperties;
    
        @Bean
        public DataSource getDataSource() {
            DruidDataSource datasource = new DruidDataSource();
            datasource.setUrl(this.dbUrl);
            datasource.setUsername(username);
            datasource.setPassword(password);
            datasource.setDriverClassName(driverClassName);
    
            //configuration
            datasource.setInitialSize(initialSize);
            datasource.setMinIdle(minIdle);
            datasource.setMaxActive(maxActive);
            datasource.setMaxWait(maxWait);
            datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            datasource.setValidationQuery(validationQuery);
            datasource.setTestWhileIdle(testWhileIdle);
            datasource.setTestOnBorrow(testOnBorrow);
            datasource.setTestOnReturn(testOnReturn);
            datasource.setPoolPreparedStatements(poolPreparedStatements);
            datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
            try {
                datasource.setFilters(filters);
            } catch (SQLException e) {
                logger.error("druid configuration initialization filter", e);
            }
            datasource.setConnectionProperties(connectionProperties);
    
            return datasource;
        }
    }
    

      

    DruidMonitorConfigurer

    package com.example.demo.core.configurer;
    
    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.support.http.StatViewServlet;
    import com.alibaba.druid.support.http.WebStatFilter;
    
    /**
     * @author 
     * @Description:Druid监控配置
     * @time 2018/4/18 15:30
     */
    @Configuration
    public class DruidMonitorConfigurer {
    
        /**
         * 注册ServletRegistrationBean
         * @return
         */
        @Bean
        public ServletRegistrationBean registrationBean() {
            ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
            /** 初始化参数配置,initParams**/
            //白名单
            bean.addInitParameter("allow", "127.0.0.1");//多个ip逗号隔开
            //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
            //bean.addInitParameter("deny", "192.168.1.110");
            //登录查看信息的账号密码.
            bean.addInitParameter("loginUsername", "admin");
            bean.addInitParameter("loginPassword", "123456");
            //是否能够重置数据.
            bean.addInitParameter("resetEnable", "false");
            return bean;
        }
    
        /**
         * 注册FilterRegistrationBean
         * @return
         */
        @Bean
        public FilterRegistrationBean druidStatFilter() {
            FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
            //添加过滤规则.
            bean.addUrlPatterns("/*");
            //添加不需要忽略的格式信息.
            bean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
            return bean;
        }
    
    }
    

      

    四:启动服务器,访问数据连接池管理页面

    打开浏览器,输入http://localhost:8080/druid    

  • 相关阅读:
    协方差矩阵
    SLAM中的关键帧是什么?有什么用?如何选择关键帧?
    EKF算法与非线性优化算法的比较
    LC217 存在重复元素
    LC42 接雨水
    LC20 有效的括号
    LC3 无重复最长子串
    LC4 寻找两个有序数组的中位数
    ubuntu16.04下安装g2o
    小米 各版本手机系统包 线刷包 卡刷包
  • 原文地址:https://www.cnblogs.com/yui66/p/9615682.html
Copyright © 2020-2023  润新知