<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          springboot項目整合druid數(shù)據(jù)庫連接池

          共 14350字,需瀏覽 29分鐘

           ·

          2021-04-25 10:10

          點擊上方藍色字體,選擇“標星公眾號”

          優(yōu)質(zhì)文章,第一時間送達

            作者 |  蒲公英不是夢

          來源 |  urlify.cn/ARFnqq

          76套java從入門到精通實戰(zhàn)課程分享

          Druid連接池是阿里巴巴開源的數(shù)據(jù)庫連接池項目,后來貢獻給Apache開源;

          Druid的作用是負責分配、管理和釋放數(shù)據(jù)庫連接,它允許應用程序重復使用一個現(xiàn)有的數(shù)據(jù)庫連接,而不是再重新建立一個;

          Druid連接池內(nèi)置強大的監(jiān)控功能,其中的StatFilter功能,能采集非常完備的連接池執(zhí)行信息,方便進行監(jiān)控,而監(jiān)控特性不影響性能。

          Druid連接池內(nèi)置了一個監(jiān)控頁面,提供了非常完備的監(jiān)控信息,可以快速診斷系統(tǒng)的瓶頸。

          SpringBoot 1.x版本默認使用的的tomcat的jdbc連接池,由于jdbc性能,穩(wěn)定性,監(jiān)控能力都不不太好,所以SpringBoot 2.x版本后 默認連接池已經(jīng)替換成了HikariCP,HikariCP性能強、速度快、口碑好、代碼少和穩(wěn)定,暫時不推薦替換成成其他連接池。
          這里記錄springboot項目整合druid數(shù)據(jù)庫連接池中間件:

          資源準備及版本說明

          編程工具:IDEA

          JDK版本:1.8

          Maven版本:Apache Maven 3.6.3

          springboot版本:2.4.4

          mybatis版本:1.3.2

          mysql版本:5.1.48

          druid版本:1.1.21

          創(chuàng)建mavem項目

          通過IDEA創(chuàng)建很便捷,參考《IDEA創(chuàng)建SpringBoot的maven項目》,springboot項目整合mybatis參考《springboot項目整合mybatis》。

          配置pom.xml

          druidpom依賴有兩個版本,一個需要編寫配置文件,一個是自動配置的,這里選擇自動配置版本

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

          完整pom.xml配置如下:

          <?xml version="1.0" encoding="UTF-8"?>
          <project xmlns="http://maven.apache.org/POM/4.0.0"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
              <modelVersion>4.0.0</modelVersion>
              <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>2.4.4</version>
                  <relativePath/> <!-- lookup parent from repository -->
              </parent>
              <groupId>org.example</groupId>
              <artifactId>springboot-druid</artifactId>
              <version>1.0-SNAPSHOT</version>

              <properties>
                  <java.version>1.8</java.version>
                  <mybatis.version>1.3.2</mybatis.version>
                  <mysql.version>5.1.48</mysql.version>
                  <druid.version>1.1.9</druid.version>
              </properties>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-test</artifactId>
                      <scope>test</scope>
                  </dependency>
                  <dependency>
                      <groupId>org.mybatis.spring.boot</groupId>
                      <artifactId>mybatis-spring-boot-starter</artifactId>
                      <version>${mybatis.version}</version>
                  </dependency>
                  <dependency>
                      <groupId>mysql</groupId>
                      <artifactId>mysql-connector-java</artifactId>
                      <version>${mysql.version}</version>
                  </dependency>
          <!--        <dependency>-->
          <!--            <groupId>com.alibaba</groupId>-->
          <!--            <artifactId>druid</artifactId>-->
          <!--            <version>${druid.version}</version>-->
          <!--        </dependency>-->
                  <dependency>
                      <groupId>com.alibaba</groupId>
                      <artifactId>druid-spring-boot-starter</artifactId>
                      <version>1.1.21</version>
                  </dependency>
                  <dependency>
                      <groupId>org.projectlombok</groupId>
                      <artifactId>lombok</artifactId>
                      <optional>true</optional>
                  </dependency>
              </dependencies>
              <build>
                  <plugins>
                      <plugin>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-maven-plugin</artifactId>
                      </plugin>
                  </plugins>
              </build>
          </project>

          配置application.yml

          application.yml配置文件中需要配置druid的相關(guān)信息

          配置說明如下:

          完整application.yml配置如下:

          server:
            port: 8888
          spring:
            application:
              name: springboot-druid
            datasource:
              username: root
              password: 123456
              url: jdbc:mysql://localhost:3306/spring-boot-test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
              driver-class-name: com.mysql.jdbc.Driver
              type: com.alibaba.druid.pool.DruidDataSource            # 數(shù)據(jù)庫連接池類別
              druid:
                initial-size: 5                                       # 初始化大小
                min-idle: 10                                          # 最小連接數(shù)
                max-active: 20                                        # 最大連接數(shù)
                max-wait: 60000                                       # 獲取連接時的最大等待時間
                min-evictable-idle-time-millis: 300000                # 一個連接在池中最小生存的時間,單位是毫秒
                time-between-eviction-runs-millis: 60000              # 多久才進行一次檢測需要關(guān)閉的空閑連接,單位是毫秒
                filters: stat,wall                                    # 配置擴展插件:stat-監(jiān)控統(tǒng)計,log4j-日志,wall-防火墻(防止SQL注入),去掉后,監(jiān)控界面的sql無法統(tǒng)計
                validation-query: SELECT 1                            # 檢測連接是否有效的 SQL語句,為空時以下三個配置均無效
                test-on-borrow: true                                  # 申請連接時執(zhí)行validationQuery檢測連接是否有效,默認true,開啟后會降低性能
                test-on-return: true                                  # 歸還連接時執(zhí)行validationQuery檢測連接是否有效,默認false,開啟后會降低性能
                test-while-idle: true                                 # 申請連接時如果空閑時間大于timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測連接是否有效,默認false,建議開啟,不影響性能
                stat-view-servlet:
                  enabled: true                                       # 是否開啟 StatViewServlet
                  allow: 127.0.0.1                                    # 訪問監(jiān)控頁面 白名單,默認127.0.0.1
                  deny: 192.168.56.1                                  # 訪問監(jiān)控頁面 黑名單
                  login-username: admin                               # 訪問監(jiān)控頁面 登陸賬號
                  login-password: admin                               # 訪問監(jiān)控頁面 登陸密碼
                filter:
                  stat:
                    enabled: true                                     # 是否開啟 FilterStat,默認true
                    log-slow-sql: true                                # 是否開啟 慢SQL 記錄,默認false
                    slow-sql-millis: 5000                             # 慢 SQL 的標準,默認 3000,單位:毫秒
                    merge-sql: false                                  # 合并多個連接池的監(jiān)控數(shù)據(jù),默認false

          # mybatis配置
          mybatis:
            mapper-locations: classpath:mapper/*Mapper.xml
            type-aliases-package: com.dandelion.model

          # 輸出sql語句日志
          logging:
            level:
              com:
                springboot:
                  dao: debug

          訪問druid監(jiān)控中心

          啟動項目:

          在瀏覽器中輸入http://IP:端口號/druid/index.html訪問監(jiān)控中心

          如果有配置登錄賬號密碼,則需要進行登錄:

          定義測試接口查詢數(shù)據(jù)庫:

          監(jiān)控中心記錄訪問情況







          粉絲福利:Java從入門到入土學習路線圖

          ??????

          ??長按上方微信二維碼 2 秒


          感謝點贊支持下哈 

          瀏覽 68
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  蔡依林上位骑乘 | 18禁黄网站网址免费入口 | 人人色人人摸 | 色婷婷国产精品免费视频 | 天天日天天综合 |