SpringBoot-Druid相关配置

简介: SpringBoot-Druid相关配置

1.首先导入Druid依赖



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


2.配置数据源



创建application.yml文件

spring:
  datasource:
    username: root
    password: 1234
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    type: com.alibaba.druid.pool.DruidDataSource
  #SpringBoot默认是不注入这些的,需要自己绑定
  #druid数据源专有配置
  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,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


3.创建Druid配置类



package com.peng.confing;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }
    @Bean
    public ServletRegistrationBean a(){
        ServletRegistrationBean bean=new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        //账号密码
        HashMap initParameters = new HashMap<>();
        initParameters.put("loginUsername","admin");//loginUsername固定的
        initParameters.put("loginPassword","123456");//loginPassword固定的
        //允许谁可以访问
        initParameters.put("allow","");//允许谁可以访问
        //禁止谁initParameters.put("peng","192.168.1.1")//
        bean.setInitParameters(initParameters);//设置初始化参数
        return bean;
    }
    @Bean
    public FilterRegistrationBean webFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        HashMap initParameters = new HashMap<>();
        initParameters.put("exclusions","*.js,*.css,/druid/*");
        bean.setInitParameters(initParameters);
        return bean;
    }
}


4.使用 @Autowired后springboot会自动注入数据源



package com.peng;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
@SpringBootTest
class SpringbootDruidApplicationTests {
    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() {
    }
}


5.启动浏览器访问http://localhost:8080/druid/登录设置的账号密码就会进入后台页面



image.png

相关文章
|
安全 数据处理 开发工具
第三方SDK合规浅析
近日,工信部通报了22款APP、SDK存在侵害用户权益行为。通报超半数的原因违反了《个保法》最小化收集的原则,包括超范围收集个人信息,APP强制、频繁、过度索取权限。
400 1
|
负载均衡 Java 微服务
OpenFeign原来是这么基于Ribbon来实现负载均衡的
大家好,我是三友~~ 前面我已经剖析了OpenFeign的动态代理生成原理和Ribbon的运行原理,这篇文章来继续剖析SpringCloud组件原理,来看一看OpenFeign是如何基于Ribbon来实现负载均衡的,两组件是如何协同工作的。
OpenFeign原来是这么基于Ribbon来实现负载均衡的
|
3月前
|
存储 运维 数据可视化
Jaeger,一个链路追踪神器!
在微服务架构中,一次请求可能经过多个服务节点,带来复杂的调用关系。如何追踪请求全链路、快速定位问题、优化性能,成为开发与运维的关键挑战。链路追踪(Tracing)技术应运而生,而 Jaeger 作为业界主流的开源分布式链路追踪系统,提供了强大的支持。本文将带你全面了解 Jaeger 的核心概念、架构原理、使用方式及实际项目中的落地方法,助你快速掌握链路追踪技术,提升系统的可观测性与稳定性。
766 2
Jaeger,一个链路追踪神器!
|
6月前
|
监控 安全 Linux
AlmaLinux 9.6 正式版发布 - RHEL 二进制兼容免费发行版
AlmaLinux 9.6 正式版发布 - RHEL 二进制兼容免费发行版
355 0
AlmaLinux 9.6 正式版发布 - RHEL 二进制兼容免费发行版
|
11月前
|
SQL 关系型数据库 API
HarmonyOs开发:关系型数据库封装之增删改查
每个方法都预留了多种调用方式,比如使用callback异步回调或者使用Promise异步回调,亦或者同步执行,大家在使用的过程中,可以根据自身业务需要进行选择性调用,也分别暴露了成功和失败的方法,可以针对性的判断在执行的过程中是否执行成功。
382 13
|
Java 编译器 数据库连接
Cause java.sql.SQLDataException Unsupported conversion from LONG to java.sql.Timestamp
Cause java.sql.SQLDataException Unsupported conversion from LONG to java.sql.Timestamp
1431 0
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
1109 6
|
存储 人工智能 自然语言处理
AI Agent框架(LLM Agent):LLM驱动的智能体如何引领行业变革,应用探索与未来展望
【7月更文挑战第2天】AI Agent框架(LLM Agent):LLM驱动的智能体如何引领行业变革,应用探索与未来展望
AI Agent框架(LLM Agent):LLM驱动的智能体如何引领行业变革,应用探索与未来展望
|
SQL 监控 druid
SpringBoot配置Druid
SpringBoot配置Druid
|
存储 负载均衡 关系型数据库