druid是java語言中最好的數(shù)據(jù)庫連接池。druid相比于其他的數(shù)據(jù)庫連接池,有兩大特性:
- 監(jiān)控?cái)?shù)據(jù)庫,有利于分析線上數(shù)據(jù)庫問題
- 更容易擴(kuò)展,同時(shí)也很高效。
今天演示一下spring boot集成druid。
實(shí)戰(zhàn)
1、添加maven依賴。
spring boot版本使用的是1.x的,2.x的版本druid starter還不支持。不過自定義也是沒問題的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!--starter-web 方便我們查看效果--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!--使用mybatis也可以,druid提供的只是連接池--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid-spring-boot-starter</artifactid> <version> 1.1 . 6 </version> </dependency> |
配置druid
2、druid應(yīng)用的配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
server: port: 9011 spring: datasource: type: com.alibaba.druid.pool.druiddatasource driver- class -name: com.mysql.jdbc.driver druid: initial-size: 5 max-active: 10 min-idle: 5 max-wait: 60000 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 validation-query: select 1 validation-query-timeout: 60000 test-on-borrow: false test-on- return : false test- while -idle: true time-between-eviction-runs-millis: 60000 filter: stat: log-slow-sql: true db-type: mysql slow-sql-millis: 2000 stat-view-servlet: login-username: druid login-password: druid allow: 127.0 . 0.1 url-pattern: /druid/* username: root password: 123456 url: jdbc:mysql: //127.0.0.1:3306/test?characterencoding=utf-8 |
通過javabean的配置更靈活一些,我們通過javabean來配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
@configuration public class druidconfig { @bean public jdbctemplate jdbctemplate(){ return new jdbctemplate(druiddatasource()); } // configurationproperties可以直接把應(yīng)用配置的spring.datasource.druid屬性開頭的值注入到druiddatasource中 @configurationproperties (prefix = "spring.datasource.druid" ) @bean (initmethod = "init" ,destroymethod = "close" ) public druiddatasource druiddatasource(){ druiddatasource druiddatasource = new druiddatasource(); // 添加druid的監(jiān)控過濾器,當(dāng)前只演示監(jiān)控的功能,因此只有一個(gè)過濾器,可以實(shí)現(xiàn)多個(gè)過濾器 linkedlist<filter> filterslist = new linkedlist(); filterslist.add(filter()); druiddatasource.setproxyfilters(filterslist); return druiddatasource; } @bean public filter filter(){ statfilter statfilter = new statfilter(); // sql執(zhí)行時(shí)間超過2s種的被判定為慢日志 statfilter.setslowsqlmillis( 2000 ); //顯示慢日志 statfilter.setlogslowsql( true ); //合并sql,有時(shí),一些相同的慢日志過多影響閱讀,開啟合并功能 statfilter.setmergesql( true ); return statfilter; } // 監(jiān)控的面板 @bean public servletregistrationbean servletregistrationbean(){ // 注冊(cè)自己的sevlet return new servletregistrationbean( new statviewservlet(), "/druid/*" ); } } |
3、新建sql執(zhí)行測(cè)試
使用jdbcteplate選取數(shù)據(jù)庫中的數(shù)據(jù),我們只是演示druid的監(jiān)控效果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@restcontroller @springbootapplication public class daoapplication { public static void main(string[] args) { springapplication.run(daoapplication. class ,args); } @autowired jdbctemplate jdbctemplate; @requestmapping ( "/test" ) public list test(){ final list<integer> idlist = new linkedlist<integer>(); jdbctemplate.query( "select * from sh_test1" , new rowcallbackhandler() { @override public void processrow(resultset rs) throws sqlexception { idlist.add(rs.getint( 1 )); } }); return idlist; } } |
運(yùn)行查看效果
5、演示完畢
到這一步,druid已經(jīng)可以在spring boot中使用了,druid提供了很多監(jiān)控的選項(xiàng),文章篇幅有限, 只介紹一下druid集成spring boot的用法。
最后
這篇文章演示了一下druid在springboot中的使用。有關(guān)druid的使用請(qǐng)看下面的參考。
參考
[druid常見用法] (https://github.com/alibaba/druid/wiki/%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jianshu.com/p/d396d2f924c5