首页 > 其他分享 >SpringBoot-整合Druid

SpringBoot-整合Druid

时间:2022-09-05 14:44:25浏览次数:59  
标签:stat SpringBoot druid Druid bean 整合 initParameters new true

1.添加jar包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.8</version>
</dependency>

2.配置文件

#数据源基本信息
spring:
  datasource:
    druid:
      username: root
      password: 123456
      url: jdbc:mysql://localhost:3306/daily?useSSl=ture&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
      driver-class-name: com.mysql.cj.jdbc.Driver

#连接池属性
      initial-size: 15
      max-active: 100
      min-idle: 15
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      test-on-borrow: false
      test-on-return: false
      test-while-idle: true
      validation-query: SELECT 1
      validation-query-timeout: 1000
      keep-alive: true
      remove-abandoned: true
      remove-abandoned-timeout: 180
      log-abandoned: true
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat,wall,slf4j
      use-global-data-source-stat: true
      maxOpenPreparedStatements: 100
      connect-properties.mergeSql: true
      connect-properties.slowSqlMillis: 5000

      # 配置DruidStatFilter
      web-stat-filter:
        enabled: true
        url-pattern: "/*"
        exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
      # 配置DruidStatViewServlet
      stat-view-servlet:
        url-pattern: "/druid/*"
        # IP白名单(没有配置或者为空,则允许所有访问)
        allow: 127.0.0.1
        # IP黑名单 (存在共同时,deny优先于allow)
        deny: 192.168.0.1
        #  禁用HTML页面上的“Reset All”功能
        reset-enable: false
        # 登录名
        login-username: admin
        # 登录密码
        login-password: 123456
        # 新版需要配置这个属性才能访问监控页面
        enabled: true

3.如果使用的是以下这个jar包

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

4.配置文件

spring:
  datasource:
    username: root
    password: root
#    假如时区报错,url中添加
#    url:  jdbc:mysql://localhost:3306/mybatis?ServerTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    url:  jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    #SpringBoot默认是不注入这些的,需要自己绑定
    #druid数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000    #60秒
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许报错,java.lang.ClassNotFoundException: org.apache.Log4j.Properity
    #则导入log4j 依赖就行
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionoProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

5.配置类

@Configuration
public class DruidConfig {

    //绑定配置文件,return的DruidDataSource就能使用配置的属性
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource DruidDataSource(){
        return new DruidDataSource();
    }

    //因为springboot内置了servlet容器,所有没有web。xml 替代方法就是将ServletRegistrationBean 注册进spring容器,
    //监控需要用到Servlet,所以通过ServletRegistrationBean注册,  需要什么Servlet就在参数中new
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //固定写法, /druid/*为访问druid监控页面的路径
        ServletRegistrationBean<StatViewServlet> bean=new  ServletRegistrationBean<>( new StatViewServlet(),"/druid/*");

        //后台需要有人登录,配置账号密码
        HashMap<String, String> initParameters = new HashMap<>();

        //增加配置,key是固定的,value自定义
        //在 StatViewServlet 的父类中可以查看
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","111");

        //允许谁访问         参数为空表示允许所有人可以访问  localhost 只允许本机访问
        initParameters.put("allow","");

        //禁止谁访问
        initParameters.put("deny","192.168.6.51");

        bean.setInitParameters(initParameters);   //设置初始化参数
        return bean;
    }

        //注册过滤器
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(new WebStatFilter());
        HashMap<String, String> initParameters = new HashMap<>();
        initParameters.put("exclusions","*.js,*.css,/druid/*");

        bean.setInitParameters(initParameters);

//        FilterRegistrationBean bean = new FilterRegistrationBean();
//        bean.setFilter(new WebStatFilter());
//        HashMap<String, String> initParameters = new HashMap<>();
//        //这些东西不进行过滤
//        initParameters.put("exclusions","*.js,*.css,/druid/*");
//        //过滤哪些请求
//        bean.setInitParameters(initParameters);
        return bean;
    }
}

标签:stat,SpringBoot,druid,Druid,bean,整合,initParameters,new,true
From: https://www.cnblogs.com/ggzs/p/16657946.html

相关文章

  • 14.Springboot多环境配置2
    1.主配置文件application.ymlspring:profiles:active:@profile.active@#需要在pom文件中指定变量#active:pro#include:mvcgroup:"pro......
  • springboot聚合项目搭建
    springboot聚合项目搭建1、简介1.1、什么是聚合项目?一个项目中包含多个子项目的项目。结构:|-父模块---|子模块1---|子模块2---|子模块31.2、聚合项目有什么......
  • springboot Invalid bound statement (not found): com.xx.dao.%Dao.login
    解决方法:需要注意一下application.xml配置文件的MyBatis的配置的mapper-locations的路径参考的这篇博客:(133条消息)Invalidboundstatement(notfound):com.exampl......
  • springboot配置swagger2线上文档
     1、先上项目配置好的swagger2的ui界面:  2、需要swagger2的这两个包:<!--swagger2包--><dependency><groupId>io.springfox</g......
  • SpringBoot+mybatis项目 配置控制台打印sql语句
    @SpringBoot+mybatis项目配置控制台打印sql语句前几天在做项目的过程中,使用的持久层框架是mybatis,在mapper.xml中自己写sql,当时写完了自己的业务代码,测试时候一直觉得数......
  • IDEA设置springBoot启动类快捷键
    publicstaticvoidmain(String[]args){SpringApplication.run($name$.class,args);}......
  • Springboot2.x 使用 nacos 实现配置管理
    参考https://nacos.io/zh-cn/https://blog.csdn.net/weixin_43871678/article/details/121628460环境环境版本操作windows10JDK11Springboot2.......
  • springboot简单使用(4)
    1.9第九章Thymeleaf模版1.9.1认识ThymeleafThymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发模板引擎是一个技术名词,是跨领域跨平台的概念,在Java语......
  • SpringBoot集成Dubbo和Zookeeper
    15、SpringBoot集成Dubbo和Zookeeper15.1、分布式理论什么是分布式系统?在《分布式系统原理与范型》一书中有如下定义:“分布式系统是若干独立计算机的集合,这些计算机对......
  • Docker基础知识 (8) - 使用 Docker 部署 SpringBoot + MariaDB(MySQL)项目
    本文在“ Docker基础知识(7)-使用Docker部署SpringBoot项目”里的SpringbootWebDocker项目的基础上,添加JDBC、MariaDB和MyBatis相关依赖包和数据库操作代......