這篇文章給大家介紹SpringBoot2 中怎么使用Druid連接池,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、做網(wǎng)站、宏偉網(wǎng)絡(luò)推廣、成都小程序開(kāi)發(fā)、宏偉網(wǎng)絡(luò)營(yíng)銷、宏偉企業(yè)策劃、宏偉品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供宏偉建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:aaarwkj.com
Druid連接池是阿里巴巴開(kāi)源的數(shù)據(jù)庫(kù)連接池項(xiàng)目。Druid連接池為監(jiān)控而生,內(nèi)置強(qiáng)大的監(jiān)控功能,監(jiān)控特性不影響性能。功能強(qiáng)大,能防SQL注入,內(nèi)置Loging能診斷Hack應(yīng)用行為。
Druid連接池是阿里巴巴內(nèi)部唯一使用的連接池,在內(nèi)部數(shù)據(jù)庫(kù)相關(guān)中間件TDDL/DRDS 都內(nèi)置使用強(qiáng)依賴了Druid連接池,經(jīng)過(guò)阿里內(nèi)部數(shù)千上萬(wàn)的系統(tǒng)大規(guī)模驗(yàn)證,經(jīng)過(guò)歷年雙十一超大規(guī)模并發(fā)驗(yàn)證。
穩(wěn)定性特性,阿里巴巴的業(yè)務(wù)驗(yàn)證 完備的監(jiān)控信息,快速診斷系統(tǒng)的瓶頸 內(nèi)置了WallFilter 提供防SQL注入功能
<!-- 數(shù)據(jù)庫(kù)依賴 --> <dependency> <groupId>MySQL</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.13</version> </dependency> <!-- JDBC 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
spring: application: # 應(yīng)用名稱 name: node07-boot-druid datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/data_one?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false username: root password: 123 initial-size: 10 max-active: 100 min-idle: 10 max-wait: 60000 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 max-evictable-idle-time-millis: 60000 validation-query: SELECT 1 FROM DUAL # validation-query-timeout: 5000 test-on-borrow: false test-on-return: false test-while-idle: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #filters: #配置多個(gè)英文逗號(hào)分隔(統(tǒng)計(jì),sql注入,log4j過(guò)濾) filters: stat,wall stat-view-servlet: enabled: true url-pattern: /druid/*
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; 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 org.springframework.jdbc.core.JdbcTemplate; /** * Druid數(shù)據(jù)庫(kù)連接池配置文件 */ @Configuration public class DruidConfig { private static final Logger logger = LoggerFactory.getLogger(DruidConfig.class); @Value("${spring.datasource.druid.url}") private String dbUrl; @Value("${spring.datasource.druid.username}") private String username; @Value("${spring.datasource.druid.password}") private String password; @Value("${spring.datasource.druid.driverClassName}") private String driverClassName; @Value("${spring.datasource.druid.initial-size}") private int initialSize; @Value("${spring.datasource.druid.max-active}") private int maxActive; @Value("${spring.datasource.druid.min-idle}") private int minIdle; @Value("${spring.datasource.druid.max-wait}") private int maxWait; @Value("${spring.datasource.druid.pool-prepared-statements}") private boolean poolPreparedStatements; @Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.druid.time-between-eviction-runs-millis}") private int timeBetweenEvictionRunsMillis; @Value("${spring.datasource.druid.min-evictable-idle-time-millis}") private int minEvictableIdleTimeMillis; @Value("${spring.datasource.druid.max-evictable-idle-time-millis}") private int maxEvictableIdleTimeMillis; @Value("${spring.datasource.druid.validation-query}") private String validationQuery; @Value("${spring.datasource.druid.test-while-idle}") private boolean testWhileIdle; @Value("${spring.datasource.druid.test-on-borrow}") private boolean testOnBorrow; @Value("${spring.datasource.druid.test-on-return}") private boolean testOnReturn; @Value("${spring.datasource.druid.filters}") private String filters; @Value("{spring.datasource.druid.connection-properties}") private String connectionProperties; /** * Druid 連接池配置 */ @Bean //聲明其為Bean實(shí)例 public DruidDataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setMaxEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { datasource.setFilters(filters); } catch (Exception e) { logger.error("druid configuration initialization filter", e); } datasource.setConnectionProperties(connectionProperties); return datasource; } /** * JDBC操作配置 */ @Bean(name = "dataOneTemplate") public JdbcTemplate jdbcTemplate (@Autowired DruidDataSource dataSource){ return new JdbcTemplate(dataSource) ; } /** * 配置 Druid 監(jiān)控界面 */ @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean srb = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //設(shè)置控制臺(tái)管理用戶 srb.addInitParameter("loginUsername","root"); srb.addInitParameter("loginPassword","root"); //是否可以重置數(shù)據(jù) srb.addInitParameter("resetEnable","false"); return srb; } @Bean public FilterRegistrationBean statFilter(){ //創(chuàng)建過(guò)濾器 FilterRegistrationBean frb = new FilterRegistrationBean(new WebStatFilter()); //設(shè)置過(guò)濾器過(guò)濾路徑 frb.addUrlPatterns("/*"); //忽略過(guò)濾的形式 frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return frb; } }
@RestController public class DruidController { private static final Logger LOG = LoggerFactory.getLogger(DruidController.class); @Resource private JdbcTemplate jdbcTemplate ; @RequestMapping("/druidData") public String druidData (){ String sql = "SELECT COUNT(1) FROM d_phone" ; Integer countOne = jdbcTemplate.queryForObject(sql,Integer.class) ; // countOne==2 LOG.info("countOne=="+countOne); return "success" ; } }
完成一次數(shù)據(jù)請(qǐng)求后,訪問(wèn)如下鏈接。
http://localhost:8007/druid 輸入配置的用戶名和密碼: root root
主要展示鏈接數(shù)據(jù)庫(kù)的基礎(chǔ)信息。
連接池配置的各項(xiàng)詳細(xì)屬性,可以參考這里查看,無(wú)需再?gòu)木W(wǎng)上查找。
所有執(zhí)行的SQL,都會(huì)在這里被監(jiān)控到,且會(huì)有SQL執(zhí)行的詳細(xì)計(jì)劃。
關(guān)于SpringBoot2 中怎么使用Druid連接池就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
名稱欄目:SpringBoot2中怎么使用Druid連接池
文章出自:http://aaarwkj.com/article0/gjcdoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站收錄、企業(yè)網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站排名、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)