国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Alibaba Druid整合

這篇具有很好參考價值的文章主要介紹了Alibaba Druid整合。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

  • Druid官網(wǎng):https://github.com/alibaba/druid
  • Druid官網(wǎng)文檔(中文):https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

方式一:自定義整合

引入 Druid 的依賴:

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

配置數(shù)據(jù)源、監(jiān)控頁面的 Servlet、Filter

package cn.daniel.springboot2adminstudy.config;


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.sql.SQLException;


/**
 * 描述:  Spring Boot 手動整合 Druid 連接池
 */
// @Deprecated
@Configuration
public class MyDataSourceConfig {

    /**
     * 配置 Druid 數(shù)據(jù)源。(Spring Boot 會在 IoC 容器自動讀取類型為 DataSource 的對象。故這個 bean 注入后,即與Spring Boot整合好了)
     * @return DruidDataSource 數(shù)據(jù)源
     * @throws SQLException
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource",ignoreUnknownFields = false) // 讀取配置文件中的數(shù)據(jù)源信息。Druid會以此建立數(shù)據(jù)庫連接
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setFilters("stat"); // 開啟
        return druidDataSource;
    }

    /**
     * (非必要)開啟前臺監(jiān)控頁面對應的 Servlet,并設置密碼(通過 http://域名/druid/index.html 即可訪問監(jiān)控頁面)
     * 此 Servlet 是由 Druid 提供的 StatViewServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidServlet(){
        ServletRegistrationBean<StatViewServlet> statViewServletServletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        statViewServletServletRegistrationBean.addInitParameter("loginUsername","root");
        statViewServletServletRegistrationBean.addInitParameter("loginPassword","131121");
        return statViewServletServletRegistrationBean;
    }

    /**
     * (非必要)配置Druid的Filter,用于記錄 web 請求記錄。
     * 此 Filter 是由 Druid 提供的 WebStatFilter
     * @return
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> druidFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
        // 除以下路徑的訪問不記錄,其它都會進行記錄
        webStatFilterFilterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        // 記錄 session 時,記錄下 session 中記錄的用戶名
        webStatFilterFilterRegistrationBean.addInitParameter("principalSessionName","userName");
        return webStatFilterFilterRegistrationBean;
    }

}
server:
  port: 80
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost/abc
    username: root
    password: 12345678

整合完成,現(xiàn)在直接使用 Spring IoC 容器中的 DataSource 對象或 JdbcTemplate 即可。

方式二:使用 Druid 官方的 Starter

引入 Druid 官方提供的 Starter(引入此 Starter 就不需要引用 Druid 依賴包了,因為已經(jīng)包含 Druid 的依賴包了):

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

配置數(shù)據(jù)源(或其它的非必要的插件)

server:
  port: 80
spring:
  datasource:
    # JDBC配置:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost/abc
    username: root
    password: 12345678

    # 連接池配置:
    druid:
      initial-size: 2 # 初始化時建立物理連接的個數(shù)。默認0
      max-active: 10 # 最大連接池數(shù)量,默認8
      min-idle: 1 # 最小連接池數(shù)量
      max-wait: 2000 # 獲取連接時最大等待時間,單位毫秒。
      pool-prepared-statements: false # 是否緩存preparedStatement,也就是PSCache。PSCache對支持游標的數(shù)據(jù)庫性能提升巨大,比如說oracle。在mysql下建議關(guān)閉。
      max-pool-prepared-statement-per-connection-size: -1 # 要啟用PSCache,必須配置大于0,當大于0時,poolPreparedStatements自動觸發(fā)修改為true。在Druid中,不會存在Oracle下PSCache占用內(nèi)存過多的問題,可以把這個數(shù)值配置大一些,比如說100
      # ……druid節(jié)點下的其它參數(shù)見官方文檔:https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8

      # 啟用Druid內(nèi)置的Filter,會使用默認的配置??勺远x配置,見下方的各個filter節(jié)點。
      filters: stat,wall


      # StatViewServlet監(jiān)控器。開啟后,訪問http://域名/druid/index.html
      stat-view-servlet:
        enabled: true # 開啟 StatViewServlet,即開啟監(jiān)控功能
        login-username: daniel # 訪問監(jiān)控頁面時登錄的賬號
        login-password: 1234 # 密碼
        url-pattern: /druid/* # Servlet的映射地址,不填寫默認為"/druid/*"。如填寫其它地址,訪問監(jiān)控頁面時,要使用相應的地址
        reset-enable: false # 是否允許重置數(shù)據(jù)(在頁面的重置按鈕)。(停用后,依然會有重置按鈕,但重置后不會真的重置數(shù)據(jù))
        allow: 192.168.1.2,192.168.1.1 # 監(jiān)控頁面訪問白名單。默認為127.0.0.1。與黑名單一樣,支持子網(wǎng)掩碼,如128.242.127.1/24。多個ip用英文逗號分隔
        deny: 18.2.1.3 # 監(jiān)控頁面訪問黑名單


      # 配置 WebStatFilter(StatFilter監(jiān)控器中的Web模板)
      web-stat-filter:
        enabled: true # 開啟 WebStatFilter,即開啟監(jiān)控功能中的 Web 監(jiān)控功能
        url-pattern: /* # 映射地址,即統(tǒng)計指定地址的web請求
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' # 不統(tǒng)計的web請求,如下是不統(tǒng)計靜態(tài)資源及druid監(jiān)控頁面本身的請求
        session-stat-enable: true # 是否啟用session統(tǒng)計
        session-stat-max-count: 1 # session統(tǒng)計的最大個數(shù),默認是1000。當統(tǒng)計超過這個數(shù),只統(tǒng)計最新的
        principal-session-name: userName # 所存用戶信息的serssion參數(shù)名。Druid會依照此參數(shù)名讀取相應session對應的用戶名記錄下來(在監(jiān)控頁面可看到)。如果指定參數(shù)不是基礎數(shù)據(jù)類型,將會自動調(diào)用相應參數(shù)對象的toString方法來取值
        principal-cookie-name: userName # 與上類似,但這是通過Cookie名取到用戶信息
        profile-enable: true # 監(jiān)控單個url調(diào)用的sql列表(試了沒生效,以后需要用再研究)

      filter:
        wall:
          enabled: true  # 開啟SQL防火墻功能
          config:
            select-allow: true # 允許執(zhí)行Select查詢操作
            delete-allow: false # 不允許執(zhí)行delete操作
            create-table-allow: false # 不允許創(chuàng)建表
            # 更多用法,參考官方文檔:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE-wallfilter

檢查整合情況
可通過以下測試方法,查看當前數(shù)據(jù)源

package cn.daniel.springboot2adminstudy;

//import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
@Slf4j
class Springboot2AdminStudyApplicationTests {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() {
      // 執(zhí)行一條sql語句,檢查是否正常
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from adm_employee");
        for (Map<String, Object> map : maps) {
            System.out.println(map);
        }

      // 查看當前數(shù)據(jù)源,如果是Druid連接池,會出現(xiàn)下面兩種提示的其中一種:
      // 1. 當使用Starter整合的:當前數(shù)據(jù)源:com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper
			// 2. 當使用自定義整合時:當前數(shù)據(jù)源:com.alibaba.druid.pool.DruidDataSource
        log.info("當前數(shù)據(jù)源:{}",dataSource.getClass().getName());
    }

}

參考:
https://blog.csdn.net/weixin_45448561/article/details/123624927文章來源地址http://www.zghlxwxcb.cn/news/detail-720325.html

到了這里,關(guān)于Alibaba Druid整合的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • Alibaba Druid整合

    Druid官網(wǎng):https://github.com/alibaba/druid Druid官網(wǎng)文檔(中文):https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98 引入 Druid 的依賴: 配置數(shù)據(jù)源、監(jiān)控頁面的 Servlet、Filter 整合完成,現(xiàn)在直接使用 Spring IoC 容器中的 DataSource 對象或 JdbcTemplate 即可。 引入 Druid 官方提供的

    2024年02月08日
    瀏覽(17)
  • Spring Cloud Alibaba 最新版本(基于Spring Boot 3.1.0)整合完整使用及與各中間件集成

    Spring Cloud Alibaba 最新版本(基于Spring Boot 3.1.0)整合完整使用及與各中間件集成

    目錄 前言 源碼地址 官方中文文檔 使用版本 spring Spring Boot 3.1.0 中間件 使用到的組件與功能 環(huán)境安裝 虛擬機 nexus nacos 集成過程 工程搭建 父工程搭建 子工程 服務集成 nacos集成 配置文件 服務注冊與發(fā)現(xiàn)-discovery 服務注冊 啟動 服務發(fā)現(xiàn) 測試 配置管理-config 新增配置 ?測試

    2024年02月07日
    瀏覽(28)
  • Spring Cloud Alibaba 最新版本(基于Spring Boot 3.1.0)整合完整使用及與各中間件集成
Sleuth+Zipkin集成分布式鏈路追蹤

    Spring Cloud Alibaba 最新版本(基于Spring Boot 3.1.0)整合完整使用及與各中間件集成 Sleuth+Zipkin集成分布式鏈路追蹤

    目錄 前言 源碼地址 官方中文文檔 使用版本 spring Spring Boot 3.1.0 中間件 使用到的組件與功能 環(huán)境安裝 虛擬機 nexus nacos 集成過程 工程搭建 父工程搭建 子工程 服務集成 nacos集成 配置文件 服務注冊與發(fā)現(xiàn)-discovery 服務注冊 啟動 服務發(fā)現(xiàn) 測試 配置管理-config 新增配置 ?測試

    2024年02月12日
    瀏覽(57)
  • Java之SpringCloud Alibaba【七】【Spring Cloud微服務網(wǎng)關(guān)Gateway組件】

    Java之SpringCloud Alibaba【七】【Spring Cloud微服務網(wǎng)關(guān)Gateway組件】

    Java之SpringCloud Alibaba【一】【Nacos一篇文章精通系列】 跳轉(zhuǎn) Java之SpringCloud Alibaba【二】【微服務調(diào)用組件Feign】 跳轉(zhuǎn) Java之SpringCloud Alibaba【三】【微服務Nacos-config配置中心】 跳轉(zhuǎn) Java之SpringCloud Alibaba【四】【微服務 Sentinel服務熔斷】 跳轉(zhuǎn) Java之SpringCloud Alibaba【五】【微服務

    2024年02月06日
    瀏覽(31)
  • 【Spring Cloud Alibaba】Spring Cloud Alibaba 搭建教程

    JDK 1.8+ Maven 3.6+ Spring Boot 2.2.4.RELEASE Spring Cloud Hoxton.SR1 Spring Cloud Alibaba 2.2.0.RELEASE 官網(wǎng)推薦對應版本 本文主要介紹了如何搭建一個 Spring Cloud Alibaba 項目,適合已經(jīng)熟練使用 Spring Boot ,想要學習搭建 Spring Cloud Alibaba 項目的小伙伴。 Spring Cloud Alibaba 為分布式應用程序開發(fā)提供了

    2023年04月18日
    瀏覽(21)
  • Spring Boot整合Druid(druid 和 druid-spring-boot-starter)

    Spring Boot整合Druid(druid 和 druid-spring-boot-starter)

    引言 在現(xiàn)代的Web應用開發(fā)中,高性能的數(shù)據(jù)庫連接池是確保應用穩(wěn)定性和響應性的關(guān)鍵因素之一。Druid是一個開源的高性能數(shù)據(jù)庫連接池,具有強大的監(jiān)控和統(tǒng)計功能,能夠在Spring Boot應用中提供出色的數(shù)據(jù)庫連接管理。本文將研究在Spring Boot中集成Druid連接池的步驟,以及如

    2024年01月19日
    瀏覽(63)
  • Spring Cloud 和Spring Cloud Alibaba

    一、什么是SpringCloud? SpringCloud是基于SpringBoot的一整套實現(xiàn)微服務的框架。他提供了微服務開發(fā)所需的配置管理、服務發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線、全局鎖、決策競選、分布式會話和集群狀態(tài)管理等組件。最重要的是,跟Spring Boot框架一起使用的話,會讓你

    2024年02月12日
    瀏覽(18)
  • Spring Cloud和Spring Cloud Alibaba

    Spring Cloud和Spring Cloud Alibaba

    Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理、服務發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線、一次性令牌、全局鎖、領(lǐng)導層選舉、分布式對話、集群狀態(tài))。分布式系統(tǒng)的協(xié)調(diào)導致了鍋爐板模式,使用Spring Cloud開發(fā)人員可以快速

    2024年04月11日
    瀏覽(24)
  • Spring Cloud Alibaba

    Spring Cloud Alibaba是一個基于Spring Cloud的開源框架,它提供了一系列的微服務解決方案,如服務注冊與發(fā)現(xiàn)、配置中心、消息總線、負載均衡、服務熔斷、限流等。本文將介紹Spring Cloud Alibaba的基本概念和使用方法。 服務注冊與發(fā)現(xiàn)是微服務架構(gòu)中最基本的組件之一,它可以讓

    2024年02月04日
    瀏覽(17)
  • Spring Cloud Alibaba (一)

    Spring Cloud Alibaba (一)

    1 微服務介紹 1.1?系統(tǒng)架構(gòu)演變 隨著互聯(lián)網(wǎng)的發(fā)展,網(wǎng)站應用的規(guī)模也在不斷的擴大,進而導致系統(tǒng)架構(gòu)也在不斷的進行變化。 從互聯(lián)網(wǎng)早起到現(xiàn)在,系統(tǒng)架構(gòu)大體經(jīng)歷了下面幾個過程: 單體應用架構(gòu)---垂直應用架構(gòu)---分布 式架構(gòu)---SOA架構(gòu)---微服務架構(gòu),當然還有悄然興起的

    2024年02月14日
    瀏覽(30)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包