温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot整合druid及配置依赖的方法是什么

发布时间:2021-12-07 14:04:19 来源:亿速云 阅读:447 作者:iii 栏目:开发技术

本篇内容主要讲解“springboot整合druid及配置依赖的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot整合druid及配置依赖的方法是什么”吧!

Druid简介

Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。

Druid 是阿里巴巴开源平台上一个数据库连接池实现,结合了 C3P0、DBCP 等 DB 池的优点,同时加入了日志监控。

Druid 可以很好的监控 DB 池连接和 SQL 的执行情况,天生就是针对监控而生的 DB 连接池。

Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验。

Spring Boot 2.0 以上默认使用 Hikari 数据源,可以说 Hikari 与 Driud 都是当前 Java Web 上最优秀的数据源,我们来重点介绍 Spring Boot 如何整合 Druid 数据源。

druid 官方文档 : druid/druid-spring-boot-starter at master · alibaba/druid (github.com)

配置依赖

jdbc 依赖、数据库依赖(mysql),druid 依赖

  <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.17</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

基本-配置信息

在 application.yaml 里

spring:
#  数据库
  datasource:
    url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&characterEncoding=utf-8
    username: root
    password: root

做一个简单的查询,测试结果如图:

springboot整合druid及配置依赖的方法是什么

扩展-配置 druid 监控功能

在 spring-datasource 下

配置防火墙、监控页,开启web监控

spring:

#  数据库
  datasource:
    url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&characterEncoding=utf-8
    username: root
    password: root

    # druid 配置
    druid:
      # 配置监控防火墙功能
      filters: stat,wall # 这两个详细参数可通过 filter-xxx 配置
      filter:
        stat:
          enabled: true
          slow-sql-millis: 1000 # 设置 1000ms 为慢查询
          log-slow-sql: true  # 开启慢查询监控
        wall: # 在这里可配置 不允许更新、不允许删除等操作
          enabled: true

      # 开启监控页,并配置账号密码
      stat-view-servlet:
        enabled: true
        login-username: admin
        login-password: admin
        # reset-enable: false # 重置按钮

      # 开启web 监控功能。 urlPattern 和 exclusions 有默认值
      web-stat-filter:
        enabled: true

      # aop-patterns: cc.acdongla.boot.* 配置监控包

启动项目后, http://localhost:8080/druid/ 可进入 druid 的监控页

springboot整合druid及配置依赖的方法是什么

到此,相信大家对“springboot整合druid及配置依赖的方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI