• SpringBoot学习笔记(5)Druid连接池


    包结构

    pom文件

         <!--自定义连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.10</version>
            </dependency>
    
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    

    application.yml

    spring:
    datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.0.157:3306/db1
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    # 数据源其他配置
    #初始化大小,最小,最大
    initialSize: 5
    minIdle: 5
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

    配置druid数据源连接池

    @Configuration
    public class DruidConfig {
    
        @ConfigurationProperties(prefix = "spring.datasource")
        @Bean
        public DataSource druid(){
            return new DruidDataSource();
        }
    
        //配置Druid的监控
        //1.配置一个管理后台servlet
        @Bean
        public ServletRegistrationBean statViewServlet(){
            ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    
            HashMap<String, String> param = new HashMap<String, String>();
            param.put("loginUsername", "admin");//账号
            param.put("loginPassword", "admin");//密码
            param.put("allow","");//默认允许所有
            param.put("deny","");//不允许的黑名单ip
    
            bean.setInitParameters(param);
            return bean;
        }
    
    
    
        //配置一个web监控的filter
        @Bean
        public FilterRegistrationBean webStatFilter(){
            FilterRegistrationBean bean = new FilterRegistrationBean();
            bean.setFilter(new WebStatFilter());
    
            HashMap<String, String> param = new HashMap<String, String>();
            param.put("exclusions", "*.js,*.css,/druid/");
            bean.setInitParameters(param);
            bean.setUrlPatterns(Arrays.asList("/*"));
            return bean;
        }
    }
    

    打开 http://localhost:8080/druid

  • 相关阅读:
    分页精度
    abp zero core 启动vue项目
    swagger 配置错误
    .net core 3.0配置跨域
    .net core 3.0 swagger
    .net core 3.0一个记录request和respose的中间件
    .net Core3.0 +Nlog+Sqlserver
    .net core 3.0+unit of work (一)
    .NetCore 3.0迁移遇到的各种问题
    open xml 导出excel遇到的问题
  • 原文地址:https://www.cnblogs.com/mengY/p/11731938.html
Copyright © 2020-2023  润新知