zoukankan      html  css  js  c++  java
  • (二)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    

  • 相关阅读:
    非阻塞式NIO 小案例(模拟聊天室)
    网络通信小案例,服务端接收成功要给客户端一个反馈(阻塞式)
    阻塞式网络通信小案例:
    NIO的非阻塞式网络通信
    字符编码
    使用分散(Scatter)与聚集(Gather)来实现文件的复制
    使用通道之间的数据传输(效果,也是实现文件的复制)
    创建直接缓存区完成文件的复制
    C++预处理详解
    C++的学习资源
  • 原文地址:https://www.cnblogs.com/yui66/p/9615682.html
Copyright © 2011-2022 走看看