欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot開發(fā)案例之配置Druid數(shù)據(jù)庫連接池的示例

 更新時(shí)間:2018年03月15日 14:39:25   作者:小柒  
本篇文章主要介紹了SpringBoot開發(fā)案例之配置Druid數(shù)據(jù)庫連接池的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前言

好久沒有更新Spring Boot系列文章,你說忙么?也可能是,前段時(shí)間的關(guān)注點(diǎn)也許在其他方面了,最近項(xiàng)目中需要開發(fā)小程序,正好采用Spring Boot實(shí)現(xiàn)一個(gè)后端服務(wù),后面會(huì)把相關(guān)的代碼案例分享出來,不至于大家做小程序后端服務(wù)的時(shí)候一頭霧水。

在Spring Boot下默認(rèn)提供了若干種可用的連接池(dbcp,dbcp2, tomcat, hikari),當(dāng)然并不支持Druid,Druid來自于阿里系的一個(gè)開源連接池,它提供了非常優(yōu)秀的監(jiān)控功能,下面跟大家分享一下如何與Spring Boot集成。

版本環(huán)境

Spring Boot 1.5.2.RELEASE、Druid 1.1.6、JDK1.7

系統(tǒng)集成

添加pom.xml依賴:

<!-- Jpa -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySql -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.6</version>
</dependency>

配置application.properties:

#數(shù)據(jù)源
spring.datasource.url=jdbc:mysql://192.168.1.66:3306/spring_boot?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 初始化大小,最小,最大
spring.datasource.initialSize=1
spring.datasource.minIdle=3
spring.datasource.maxActive=20
# 配置獲取連接等待超時(shí)的時(shí)間
spring.datasource.maxWait=60000
# 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
spring.datasource.minEvictableIdleTimeMillis=30000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打開PSCache,并且指定每個(gè)連接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻
spring.datasource.filters=stat,wall,slf4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

配置yml文件(與上二選一)

spring:
 datasource:
   url: jdbc:mysql://192.168.1.66:3306/spring-boot?useUnicode=true&characterEncoding=utf-8&useSSL=false
   username: root
   password: root
   driver-class-name: com.mysql.jdbc.Driver
   platform: mysql
   type: com.alibaba.druid.pool.DruidDataSource
   # 下面為連接池的補(bǔ)充設(shè)置,應(yīng)用到上面所有數(shù)據(jù)源中
   # 初始化大小,最小,最大
   initialSize: 1
   minIdle: 3
   maxActive: 20
   # 配置獲取連接等待超時(shí)的時(shí)間
   maxWait: 60000
   # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒
   timeBetweenEvictionRunsMillis: 60000
   # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
   minEvictableIdleTimeMillis: 30000
   validationQuery: select 'x'
   testWhileIdle: true
   testOnBorrow: false
   testOnReturn: false
   # 打開PSCache,并且指定每個(gè)連接上PSCache的大小
   poolPreparedStatements: true
   maxPoolPreparedStatementPerConnectionSize: 20
   # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻
   filters: stat,wall,slf4j
   # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
   connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

配置Druid的監(jiān)控統(tǒng)計(jì)功能

import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
 * 阿里數(shù)據(jù)庫連接池 Druid配置 
 * 創(chuàng)建者 柒
 * 創(chuàng)建時(shí)間  2018年3月15日
 */
@Configuration
public class DruidConfiguration {
  private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);
  private static final String DB_PREFIX = "spring.datasource";
  @Bean
  public ServletRegistrationBean druidServlet() {
    logger.info("init Druid Servlet Configuration ");
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    // IP白名單 (沒有配置或者為空,則允許所有訪問)
    servletRegistrationBean.addInitParameter("allow", "");
    // IP黑名單(共同存在時(shí),deny優(yōu)先于allow)
    //servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
    //控制臺(tái)管理用戶
    servletRegistrationBean.addInitParameter("loginUsername", "admin");
    servletRegistrationBean.addInitParameter("loginPassword", "admin");
    //是否能夠重置數(shù)據(jù) 禁用HTML頁面上的“Reset All”功能
    servletRegistrationBean.addInitParameter("resetEnable", "false");
    return servletRegistrationBean;
  }
  @Bean
  public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
    filterRegistrationBean.addUrlPatterns("/*");
    filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    return filterRegistrationBean;
  }
  @ConfigurationProperties(prefix = DB_PREFIX)
  class IDataSourceProperties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
    private int initialSize;
    private int minIdle;
    private int maxActive;
    private int maxWait;
    private int timeBetweenEvictionRunsMillis;
    private int minEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean poolPreparedStatements;
    private int maxPoolPreparedStatementPerConnectionSize;
    private String filters;
    private String connectionProperties;
    @Bean 
    public DataSource dataSource() {
      DruidDataSource datasource = new DruidDataSource();
      datasource.setUrl(url);
      datasource.setUsername(username);
      datasource.setPassword(password);
      datasource.setDriverClassName(driverClassName);
      //configuration
      datasource.setInitialSize(initialSize);
      datasource.setMinIdle(minIdle);
      datasource.setMaxActive(maxActive);
      datasource.setMaxWait(maxWait);
      datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
      datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
      datasource.setValidationQuery(validationQuery);
      datasource.setTestWhileIdle(testWhileIdle);
      datasource.setTestOnBorrow(testOnBorrow);
      datasource.setTestOnReturn(testOnReturn);
      datasource.setPoolPreparedStatements(poolPreparedStatements);
      datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
      try {
        datasource.setFilters(filters);
      } catch (SQLException e) {
        System.err.println("druid configuration initialization filter: " + e);
      }
      datasource.setConnectionProperties(connectionProperties);
      return datasource;
    }
    public String getUrl() {
      return url;
    }
    public void setUrl(String url) {
      this.url = url;
    }
    public String getUsername() {
      return username;
    }
    public void setUsername(String username) {
      this.username = username;
    }
    public String getPassword() {
      return password;
    }
    public void setPassword(String password) {
      this.password = password;
    }
    public String getDriverClassName() {
      return driverClassName;
    }
    public void setDriverClassName(String driverClassName) {
      this.driverClassName = driverClassName;
    }
    public int getInitialSize() {
      return initialSize;
    }
    public void setInitialSize(int initialSize) {
      this.initialSize = initialSize;
    }
    public int getMinIdle() {
      return minIdle;
    }
    public void setMinIdle(int minIdle) {
      this.minIdle = minIdle;
    }
    public int getMaxActive() {
      return maxActive;
    }
    public void setMaxActive(int maxActive) {
      this.maxActive = maxActive;
    }
    public int getMaxWait() {
      return maxWait;
    }
    public void setMaxWait(int maxWait) {
      this.maxWait = maxWait;
    }
    public int getTimeBetweenEvictionRunsMillis() {
      return timeBetweenEvictionRunsMillis;
    }
    public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
      this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }
    public int getMinEvictableIdleTimeMillis() {
      return minEvictableIdleTimeMillis;
    }
    public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
      this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }
    public String getValidationQuery() {
      return validationQuery;
    }
    public void setValidationQuery(String validationQuery) {
      this.validationQuery = validationQuery;
    }
    public boolean isTestWhileIdle() {
      return testWhileIdle;
    }
    public void setTestWhileIdle(boolean testWhileIdle) {
      this.testWhileIdle = testWhileIdle;
    }
    public boolean isTestOnBorrow() {
      return testOnBorrow;
    }
    public void setTestOnBorrow(boolean testOnBorrow) {
      this.testOnBorrow = testOnBorrow;
    }
    public boolean isTestOnReturn() {
      return testOnReturn;
    }
    public void setTestOnReturn(boolean testOnReturn) {
      this.testOnReturn = testOnReturn;
    }
    public boolean isPoolPreparedStatements() {
      return poolPreparedStatements;
    }
    public void setPoolPreparedStatements(boolean poolPreparedStatements) {
      this.poolPreparedStatements = poolPreparedStatements;
    }
    public int getMaxPoolPreparedStatementPerConnectionSize() {
      return maxPoolPreparedStatementPerConnectionSize;
    }
    public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
      this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
    }
    public String getFilters() {
      return filters;
    }
    public void setFilters(String filters) {
      this.filters = filters;
    }
    public String getConnectionProperties() {
      return connectionProperties;
    }
    public void setConnectionProperties(String connectionProperties) {
      this.connectionProperties = connectionProperties;
    }
  }
}

啟動(dòng)應(yīng)用,訪問地址:http://localhost:8080/druid/, 輸入配置的賬號(hào)密碼登錄之后,即可查看數(shù)據(jù)源及SQL統(tǒng)計(jì)等監(jiān)控。效果圖如下:

當(dāng)然,阿里巴巴也提供了Druid的SpringBoot集成版(druid-spring-boot-starter),可參考以下鏈接。

參考:

https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

https://github.com/alibaba/druid/wiki

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 通過Java實(shí)現(xiàn)對(duì)PDF頁面的詳細(xì)設(shè)置

    通過Java實(shí)現(xiàn)對(duì)PDF頁面的詳細(xì)設(shè)置

    這篇文章主要介紹了通過Java實(shí)現(xiàn)對(duì)PDF頁面的詳細(xì)設(shè)置,下面的示例將介紹通過Java編程來對(duì)PDF頁面進(jìn)行個(gè)性化設(shè)置的方法,包括設(shè)置頁面大小、頁邊距、紙張方向、頁面旋轉(zhuǎn)等,需要的朋友可以參考下
    2019-07-07
  • java之scan.next()與scan.nextline()函數(shù)的使用及區(qū)別

    java之scan.next()與scan.nextline()函數(shù)的使用及區(qū)別

    這篇文章主要介紹了java之scan.next()與scan.nextline()函數(shù)的使用及區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 詳談ThreadLocal-單例模式下高并發(fā)線程安全

    詳談ThreadLocal-單例模式下高并發(fā)線程安全

    這篇文章主要介紹了ThreadLocal-單例模式下高并發(fā)線程安全,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java中indexOf()的用法小結(jié)

    Java中indexOf()的用法小結(jié)

    這篇文章主要介紹了Java中indexOf()的用法小結(jié),indexOf()有四種方法,用來查找某個(gè)字符或字符串的位置,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • feign post參數(shù)對(duì)象不加@RequestBody的使用說明

    feign post參數(shù)對(duì)象不加@RequestBody的使用說明

    這篇文章主要介紹了feign post參數(shù)對(duì)象不加@RequestBody的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 詳解Springboot中的異步、定時(shí)、郵件任務(wù)

    詳解Springboot中的異步、定時(shí)、郵件任務(wù)

    這篇文章主要介紹了Springboot中的異步、定時(shí)、郵件任務(wù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • mybatis同一張表多次連接查詢相同列賦值問題小結(jié)

    mybatis同一張表多次連接查詢相同列賦值問題小結(jié)

    這篇文章主要介紹了mybatis同一張表多次連接查詢相同列賦值問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下
    2017-01-01
  • 解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題

    解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題

    這篇文章主要介紹了解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring Cloud Stream異常處理過程解析

    Spring Cloud Stream異常處理過程解析

    這篇文章主要介紹了Spring Cloud Stream異常處理過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Java移位運(yùn)算符詳解實(shí)例(小結(jié))

    Java移位運(yùn)算符詳解實(shí)例(小結(jié))

    這篇文章主要介紹了Java移位運(yùn)算符詳解實(shí)例(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12

最新評(píng)論