Spring boot druid 的配置使用

依赖加入

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

  

YML

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 12345678
    # druid 配置
    dbType: mysql   # 指定数据库类型 mysql
    initialSize: 5  # 启动初始化连接数量
    minIdle: 5      # 最小空闲连接
    maxActive: 20   # 最大连接数量(包含使用中的和空闲的)
    maxWait: 60000  # 最大连接等待时间 ,超出时间报错
    timeBetweenEvictionRunsMillis: 60000  # 设置执行一次连接回收器的时间
    minEvictableIdleTimeMillis: 300000   # 设置时间: 该时间内没有任何操作的空闲连接会被回收
    validationQuery: select ‘x‘         # 验证连接有效性的sql
    testWhileIdle: true             # 空闲时校验
    testOnBorrow: false  # 使用中是否校验有效性
    testOnReturn: false  # 归还连接池时是否校验
    poolPreparedStatements: false  # mysql 不推荐打开预处理连接池
    filters: stat,wall,logback  #设置过滤器 stat用于接收状态,wall防止sql注入,logback说明使用logback进行日志输出
    userGlobalataSourceStat: true  # 统计所有数据源状态
    connectionProperties: druid.stat.mergSql=true;druid.stat.slowSqlMillis=500  # sql合并统计 设置慢sql时间为500,超过500 会有记录提示

 入口程序配置自定义Bean

   // 初始化druidDataSource对象
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){

        return  new DruidDataSource();
    }

    // 注册后台监控界面
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        // 绑定后台监控界面的路径  为localhost/druid
        ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,String>map=new HashMap<>();
        // 设置后台界面的用户名
        map.put("loginUsername","guoxw");
        //设置后台界面密码
        map.put("loginPassword","123456");
        // 设置那些ip允许访问," " 为所有
        map.put("allow","");
        // 不允许该ip访问
        map.put("deny","33.32.43.123");
        bean.setInitParameters(map);
        return bean;

    }

    // 监听获取应用的数据,filter用于收集数据,servlet用于数据展示

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean bean=new FilterRegistrationBean();
        // 设置过滤器
        bean.setFilter(new WebStatFilter());
        // 对所有请求进行过滤捕捉监听
        bean.addUrlPatterns("/*");
        Map<String,String>map=new HashMap<>();
        // 排除 . png  .js 的监听  用于排除一些静态资源访问
        map.put("exclusions","*.png,*.js");
        bean.setInitParameters(map);
        return bean;

    }

  

进入配置的地址 http://localhost:8080/druid

 

原文地址:https://www.cnblogs.com/galibujianbusana/p/11144904.html

时间: 2024-08-30 12:53:11

Spring boot druid 的配置使用的相关文章

Spring Boot 全局异常配置

Spring Boot 全局异常配置,处理异常控制器需要和发生异常的方法在一个类中.使用 ControllerAdvice 注解 package com.li.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; import

Spring Boot SSL [https]配置例子

前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server.port=8443 server.ssl.key-alias=selfsigned_localhost_sslserver server.ssl.key-password=changeit server.ssl.key-store=classpath:ssl-server.jks server.ss

Spring Boot 外部化配置(二) - @ConfigurationProperties 、@EnableConfigurationProperties

目录 3.外部化配置的核心 3.2 @ConfigurationProperties 3.2.1 注册 Properties 配置类 3.2.2 绑定配置属性 3.1.3 ConfigurationPropertiesAutoConfiguration 4.总结 3.外部化配置的核心 ????????接着上一章,<Spring Boot 外部化配置(一)> 3.2 @ConfigurationProperties 众所周知,当 Spring Boot 集成外部组件后,就可在 propertie

Spring Boot+Druid进行监控

Druid Spring Boot Spring Boot使用Druid监控 maven配置 applicationproperties配置 方式一原生的servlet和filter方式 方式二使用代码注册Servlet和Filter 项目监控 Druid Druid:一款为监控而生的数据库连接池框架,整个项目由数据库连接池.插件框架和SQL解析器组成. Druid功能介于PowerDrill和Dremel之间,它几乎实现了Dremel的所有功能,并且从PowerDrill吸收一些有趣的数据格式

Spring Boot + MyBatis + Pagehelper 配置多数据源

前言: 本文为springboot结合mybatis配置多数据源,在项目当中很多情况是使用主从数据源来读写分离,还有就是操作多库,本文介绍如何一个项目同时使用2个数据源. 也希望大家带着思考去学习!博主是最近才学的配置写成博文分享心得和技巧,文中有不足的欢迎留言指正,谢谢! 思考: 1.如果从传统的单数据源转换为多数据源,以前使用boot只用导包写配置文件boot会帮我们自动配置,如果不用自动配置我们改怎么配呢? 2.怎么结合mybatis分页插件一起使用呢? .................

spring boot 多数据源配置(多种数据库)

最近一段时间在使用spring boot开发项目,其中有一个项目用到了多数据源的配置,网上的资料还是不太多,走了好多才找到一个合适的,把自己写的分享一下,做个笔记,以后也许有用,第一次写博客,不好勿喷!! 首先介绍下我的业务场景,此项目用到了两种数据库,一个是mysql,另一个是sqlserver, 首先第一步需要在application.yml中将多数据源的配置信息进行配置, mysql数据源: spring: datasource: driverClassName: com.mysql.jd

Spring Boot项目属性配置

接着上面的入门教程,我们来学习下Spring Boot的项目属性配置. 1.配置项目内置属性 属性配置主要是在application.properties文件里配置的(编写时有自动提示)这里我们将server的端口变为8888,路径加上HelloWorld: 在DeomApplication.java的页面时点击运行按钮,打开浏览器输入:http://localhost:8888/HelloWorld/hello 此时,控制台的输出信息也可以看到端口变成8888了: 之前的url已无效: 更改后

小菜鸟学习spring boot --接管spring boot的web配置

菜鸟新来,大神勿喷,些许醍醐,感激涕零.因为 我总是装幽默,是因为我想让自己快乐. spring boot提供的spring mvc 不符合自己的需求,自己则可以编写一个控制类 加上 @EnableWebMvc注解 来自己控制mvc配置. spring boot提供的spring mvc 既需要保留,又需要添加自己的配置的时候,可以自定义一个WebMvcConfigureAdapter,而不需要使用EnableWebMvc注解: 1 @Configuration 2 public class W

Spring Boot Learning(日志配置)

支持日志框架:Java Util Logging, Log4J2 and Logback,默认是使用logback配置方式:默认配置文件配置和引用外部配置文件配置 一. 默认配置文件配置(不建议使用:不够灵活,对log4j2等不够友好)# 日志文件名,比如:roncoo.log,或者是 /var/log/roncoo.loglogging.file=roncoo.log # 日志级别配置,比如: logging.level.org.springframework=DEBUGlogging.lev