springboot2.x配置druid sql监控

Easter79
• 阅读 648

  后端接口响应慢,通常我们就需要优化代码和sql,如果项目中使用druid连接池,那么我们可以利用其提供的sql监控功能,来帮助我们快速定位慢sql已经sql执行次数等问题,springboot2之后,durid监控配置变的更简单了,不需要额外的代码,只需要添加配置即可。整个项目配置如下:

  依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

  application.yml

spring:
  datasource:
    druid:
      url: jdbc:postgresql://127.0.0.1:5432/test01?characterEncoding=utf-8
      username: admin
      password: 123456
      driver-class-name: org.postgresql.Driver
      initial-size: 1
      max-active: 20
      max-wait: 6000
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=2000
      min-idle: 1
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: select 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      web-stat-filter:
        enabled: true
        url-pattern: "/*"
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-max-count: 1000
        session-stat-enable: true
        profile-enable: true
      stat-view-servlet:
        enabled: true
        url-pattern: "/druid/*"
        login-username: root
        login-password: root
        allow: 127.0.0.1
        reset-enable: true



    type: com.alibaba.druid.pool.DruidDataSource
    url:
    username: admin
    password: 123456
    driver-class-name:
    filters: stat,wall,slf4j
    maxActive: 20
  jpa:
    database: postgresql
    show-sql: true
    properties:
      hibernate:
        temp:
          use_jdbc_metadata_defaults: false

  测试添加的相关类

package com.junlin.druid.Entity;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="newtable")
public class DemoEntity implements Serializable {

    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

    @Column(name="name")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

package com.junlin.druid.Dao;

import com.junlin.druid.Entity.DemoEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface DemoJPA  extends JpaRepository<DemoEntity,Integer> {

    List<DemoEntity> findByName(String name);
}

package com.junlin.druid.Controller;

import com.junlin.druid.Dao.DemoJPA;
import com.junlin.druid.Entity.DemoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class DemoController {


    @Autowired
    private DemoJPA demoJPA;

    @GetMapping("/findName")
    public Object findEntity(String name){
        List<DemoEntity> list = demoJPA.findByName(name);
        return list;
    }
}

  启动项目,访问localhost:8080/druid/,进入登陆页面输入在application.yml配置好的用户和密码(demo中都是root),即可看到监控页面

springboot2.x配置druid sql监控

  访问一下我们准备好的接口,执行几次sql查询,在监控页面点看sql监控页面,就能看到我们的sql执行情况

springboot2.x配置druid sql监控

  其它相关的配置以及页面相关内容可以参考druid官网文档,地址https://github.com/alibaba/druid/wiki/常见问题

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
待兔 待兔
2个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
2年前
sql执行计划与优化
在我们实际工作中大部分人会遇到sql优化的问题,这篇文章主要介绍SQL优化相关。首先我们怎么发现我们的sql执行效率低呢,最简单的方法就是当用户反馈慢的时候我们就会知道哪里可能会有sql效率影响的问题,这里排除其他影响情况,只考虑数据库sql慢的问题。当然这种方式对于我们来说很被动,我们还可以通过什么方式找到有性能问题sql,我们可以通过MySQL的配置文
Easter79 Easter79
2年前
sql注入
反引号是个比较特别的字符,下面记录下怎么利用0x00SQL注入反引号可利用在分隔符及注释作用,不过使用范围只于表名、数据库名、字段名、起别名这些场景,下面具体说下1)表名payload:select\from\users\whereuser\_id1limit0,1;!(https://o
Stella981 Stella981
2年前
SpringBoot 开启Druid监控统计功能教程
Druid数据连接池简介Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。性能好,同时自带监控页面,可以实时监控应用的连接池情况以及其中性能差的sql,方便我们找出应用中连接池方面的问题。Druid是一个JDBC组件,它包括三部分:1.DruidDriver代理
Stella981 Stella981
2年前
Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''
Stella981 Stella981
2年前
Druid连接池简单入门配置
偶尔的机会解释Druid连接池,后起之秀,但是评价不错,另外由于是阿里淘宝使用过的所以还是蛮看好的。Druid集连接池,监控于一体整好复合当前项目的需要,项目是ssh结构,之前是用C3p0的,现在换一个连接池也是很简单的,首先spring配置DataSource,配置如下:<bean id"dataSource" class"co
Stella981 Stella981
2年前
PostgreSQL死锁进程及慢查询处理
1、死锁进程查看:SELECTFROMpg_stat_activityWHEREdatname'数据库名称'andwaitingtrue;pid进程id。2、慢查询SQL:selectdatname,pid,usename,application_name,client_addr,client
Stella981 Stella981
2年前
Ecshop用户中心的收藏列表里显示商品缩略图
1)、修改includes/lib\_clips.php文件将下面代码$sql'SELECTg.goods_id,g.goods_name,g.market_price,g.shop_priceASorg_price,'.修改为$sql'SELECTg.goods_id,g.goods_nam
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k