1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

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

        共 14334字,需瀏覽 29分鐘

         ·

        2021-04-25 13:09

          作者 |  蒲公英不是夢(mèng)

        來源 |  urlify.cn/ARFnqq

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

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

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

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

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

        資源準(zhǔn)備及版本說明

        編程工具: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項(xiàng)目

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

        配置pom.xml

        druidpom依賴有兩個(gè)版本,一個(gè)需要編寫配置文件,一個(gè)是自動(dòng)配置的,這里選擇自動(dòng)配置版本

        <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                                       # 獲取連接時(shí)的最大等待時(shí)間
              min-evictable-idle-time-millis: 300000                # 一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
              time-between-eviction-runs-millis: 60000              # 多久才進(jìn)行一次檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒
              filters: stat,wall                                    # 配置擴(kuò)展插件:stat-監(jiān)控統(tǒng)計(jì),log4j-日志,wall-防火墻(防止SQL注入),去掉后,監(jiān)控界面的sql無法統(tǒng)計(jì)
              validation-query: SELECT 1                            # 檢測(cè)連接是否有效的 SQL語句,為空時(shí)以下三個(gè)配置均無效
              test-on-borrow: true                                  # 申請(qǐng)連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效,默認(rèn)true,開啟后會(huì)降低性能
              test-on-return: true                                  # 歸還連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效,默認(rèn)false,開啟后會(huì)降低性能
              test-while-idle: true                                 # 申請(qǐng)連接時(shí)如果空閑時(shí)間大于timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測(cè)連接是否有效,默認(rèn)false,建議開啟,不影響性能
              stat-view-servlet:
                enabled: true                                       # 是否開啟 StatViewServlet
                allow: 127.0.0.1                                    # 訪問監(jiān)控頁面 白名單,默認(rèn)127.0.0.1
                deny: 192.168.56.1                                  # 訪問監(jiān)控頁面 黑名單
                login-username: admin                               # 訪問監(jiān)控頁面 登陸賬號(hào)
                login-password: admin                               # 訪問監(jiān)控頁面 登陸密碼
              filter:
                stat:
                  enabled: true                                     # 是否開啟 FilterStat,默認(rèn)true
                  log-slow-sql: true                                # 是否開啟 慢SQL 記錄,默認(rèn)false
                  slow-sql-millis: 5000                             # 慢 SQL 的標(biāo)準(zhǔn),默認(rèn) 3000,單位:毫秒
                  merge-sql: false                                  # 合并多個(gè)連接池的監(jiān)控?cái)?shù)據(jù),默認(rèn)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)控中心

        啟動(dòng)項(xiàng)目:

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

        如果有配置登錄賬號(hào)密碼,則需要進(jìn)行登錄:

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

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

        最近給大家找了  JVM學(xué)習(xí)視頻


        資源,怎么領(lǐng)?。?/span>


        掃二維碼,加我微信,回復(fù):JVM

         注意,不要亂回復(fù) 

        沒錯(cuò),不是機(jī)器人
        記得一定要等待,等待才有好東西



        瀏覽 50
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            性XXX欧美老妇506070 | 热久久中文字幕电影 | 把女人到嗷嗷嗷叫视频 | 美国免费毛片基地 | 裸体动态图| 嗯啊使劲插 | 日日摸夜夜添夜夜添特色大片 | 小坏蛋轻点阿受不了国产免费看 | 丁香婷婷综合久久 | 比利时xxxx性hd极品 |