一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - SpringBoot集成阿里巴巴Druid監控的示例代碼

SpringBoot集成阿里巴巴Druid監控的示例代碼

2021-04-21 14:54dalaoyang Java教程

這篇文章主要介紹了SpringBoot集成阿里巴巴Druid監控的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

druid是阿里巴巴開源的數據庫連接池,提供了優秀的對數據庫操作的監控功能,本文要講解一下springboot項目怎么集成druid。

本文在基于jpa的項目下開發,首先在pom文件中額外加入druid依賴,pom文件如下:

?
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?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>
 
 <groupid>com.dalaoyang</groupid>
 <artifactid>springboot_druid</artifactid>
 <version>0.0.1-snapshot</version>
 <packaging>jar</packaging>
 
 <name>springboot_druid</name>
 <description>springboot_druid</description>
 
 <parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>1.5.12.release</version>
  <relativepath/> <!-- lookup parent from repository -->
 </parent>
 
 <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-data-jpa</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-web</artifactid>
  </dependency>
 
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-devtools</artifactid>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupid>mysql</groupid>
   <artifactid>mysql-connector-java</artifactid>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-test</artifactid>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupid>com.alibaba</groupid>
   <artifactid>druid</artifactid>
   <version>1.0.28</version>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-maven-plugin</artifactid>
   </plugin>
  </plugins>
 </build>
</project>

application.properties上半段和整合jpa一點沒變,下面加入了一些druid的配置,如果對druid的配置有什么不理解的,可以去網上查一下。(這篇文章我覺得寫的很好,傳送門)

?
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
42
43
44
45
46
47
48
49
50
51
#端口號
server.port=8888
 
##validate 加載hibernate時,驗證創建數據庫表結構
##create 每次加載hibernate,重新創建數據庫表結構,這就是導致數據庫表數據丟失的原因。
##create-drop  加載hibernate時創建,退出是刪除表結構
##update     加載hibernate自動更新數據庫結構
##validate 啟動時驗證表的結構,不會創建表
##none 啟動時不做任何操作
spring.jpa.hibernate.ddl-auto=create
 
##控制臺打印sql
spring.jpa.show-sql=true
 
##數據庫配置
##數據庫地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterencoding=utf8&usessl=false
##數據庫用戶名
spring.datasource.username=root
##數據庫密碼
spring.datasource.password=root
##數據庫驅動
spring.datasource.driver-class-name=com.mysql.jdbc.driver
 
 
#這里是不同的
#使用druid的話 需要多配置一個屬性spring.datasource.type
spring.datasource.type=com.alibaba.druid.pool.druiddatasource
 
 
# 連接池的配置信息
# 初始化大小,最小,最大
spring.datasource.initialsize=5
spring.datasource.minidle=5
spring.datasource.maxactive=20
# 配置獲取連接等待超時的時間
spring.datasource.maxwait=60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
spring.datasource.timebetweenevictionrunsmillis=60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
spring.datasource.minevictableidletimemillis=300000
spring.datasource.validationquery=select 1 from dual
spring.datasource.testwhileidle=true
spring.datasource.testonborrow=false
spring.datasource.testonreturn=false
# 打開pscache,并且指定每個連接上pscache的大小
spring.datasource.poolpreparedstatements=true
spring.datasource.maxpoolpreparedstatementperconnectionsize=20
# 配置監控統計攔截的filters,去掉后監控界面sql無法統計,'wall'用于防火墻
spring.datasource.filters=stat,wall,log4j
# 通過connectproperties屬性來打開mergesql功能;慢sql記錄

然后在項目中加入druidconfig,簡單講解一下,這個配置文件主要是加載application.properties的配置,代碼如下:

?
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.dalaoyang.config;
import java.sql.sqlexception;
import javax.sql.datasource;
import org.apache.log4j.logger;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.primary;
import com.alibaba.druid.pool.druiddatasource;
/**
 * @author dalaoyang
 * @description
 * @project springboot_learn
 * @package com.dalaoyang.config
 * @email [email protected]
 * @date 2018/4/12
 */
@configuration
public class druidconfig {
 private logger logger = logger.getlogger(this.getclass());
 
 @value("${spring.datasource.url}")
 private string dburl;
 
 @value("${spring.datasource.username}")
 private string username;
 
 @value("${spring.datasource.password}")
 private string password;
 
 @value("${spring.datasource.driver-class-name}")
 private string driverclassname;
 
 @value("${spring.datasource.initialsize}")
 private int initialsize;
 
 @value("${spring.datasource.minidle}")
 private int minidle;
 
 @value("${spring.datasource.maxactive}")
 private int maxactive;
 
 @value("${spring.datasource.maxwait}")
 private int maxwait;
 
 @value("${spring.datasource.timebetweenevictionrunsmillis}")
 private int timebetweenevictionrunsmillis;
 
 @value("${spring.datasource.minevictableidletimemillis}")
 private int minevictableidletimemillis;
 
 @value("${spring.datasource.validationquery}")
 private string validationquery;
 
 @value("${spring.datasource.testwhileidle}")
 private boolean testwhileidle;
 
 @value("${spring.datasource.testonborrow}")
 private boolean testonborrow;
 
 @value("${spring.datasource.testonreturn}")
 private boolean testonreturn;
 
 @value("${spring.datasource.poolpreparedstatements}")
 private boolean poolpreparedstatements;
 
 @value("${spring.datasource.maxpoolpreparedstatementperconnectionsize}")
 private int maxpoolpreparedstatementperconnectionsize;
 
 @value("${spring.datasource.filters}")
 private string filters;
 
 @value("{spring.datasource.connectionproperties}")
 private string connectionproperties;
 
 @bean
 @primary //主數據源
 public datasource datasource(){
  druiddatasource datasource = new druiddatasource();
  datasource.seturl(this.dburl);
  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) {
   logger.error("druid configuration exception", e);
  }
  datasource.setconnectionproperties(connectionproperties);
  return datasource;
 }
}

然后創建druidfilter,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.dalaoyang.filter;
import javax.servlet.annotation.webfilter;
import javax.servlet.annotation.webinitparam;
import com.alibaba.druid.support.http.webstatfilter;
/**
 * @author dalaoyang
 * @description
 * @project springboot_learn
 * @package com.dalaoyang.filter
 * @email [email protected]
 * @date 2018/4/12
 */
@webfilter(filtername="druidwebstatfilter",urlpatterns="/*",
  initparams={
    @webinitparam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略資源
  }
)
public class druidfilter extends webstatfilter {
}

新建druidservlet,在類上面加注解@webservlet,其中配置了登錄druid監控頁面的賬號密碼,白名單黑名單之類的配置,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.dalaoyang.servlet;
import javax.servlet.annotation.webinitparam;
import javax.servlet.annotation.webservlet;
import com.alibaba.druid.support.http.statviewservlet;
/**
 * @author dalaoyang
 * @description
 * @project springboot_learn
 * @package com.dalaoyang.servlet
 * @email [email protected]
 * @date 2018/4/12
 */
@webservlet(urlpatterns="/druid/*",
  initparams={
    @webinitparam(name="allow",value=""),// ip白名單(沒有配置或者為空,則允許所有訪問)
    @webinitparam(name="deny",value=""),// ip黑名單 (deny優先于allow)
    @webinitparam(name="loginusername",value="admin"),// 登錄druid管理頁面用戶名
    @webinitparam(name="loginpassword",value="admin")// 登錄druid管理頁面密碼
  })
public class druidservlet extends statviewservlet {
 
}

然后在啟動類加入注解@servletcomponentscan,讓項目掃描到servlet,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.dalaoyang;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.web.servlet.servletcomponentscan;
@springbootapplication
// 啟動類必須加入@servletcomponentscan注解,否則無法掃描到servlet
@servletcomponentscan
public class springbootdruidapplication {
 public static void main(string[] args) {
  springapplication.run(springbootdruidapplication.class, args);
 }
}

剩余的就是和整合jpa一樣的entity(實體類),repository(數據操作層),controller(測試使用的controller),直接展示代碼。

city

?
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.dalaoyang.entity;
import javax.persistence.*;
/**
 * @author dalaoyang
 * @description
 * @project springboot_learn
 * @package com.dalaoyang.entity
 * @email [email protected]
 * @date 2018/4/7
 */
@entity
@table(name="city")
public class city {
 @id
 @generatedvalue(strategy=generationtype.auto)
 private int cityid;
 private string cityname;
 private string cityintroduce;
 
 public city(int cityid, string cityname, string cityintroduce) {
  this.cityid = cityid;
  this.cityname = cityname;
  this.cityintroduce = cityintroduce;
 }
 
 public city(string cityname, string cityintroduce) {
  this.cityname = cityname;
  this.cityintroduce = cityintroduce;
 }
 
 public city() {
 }
 
 public int getcityid() {
  return cityid;
 }
 
 public void setcityid(int cityid) {
  this.cityid = cityid;
 }
 
 public string getcityname() {
  return cityname;
 }
 
 public void setcityname(string cityname) {
  this.cityname = cityname;
 }
 
 public string getcityintroduce() {
  return cityintroduce;
 }
 
 public void setcityintroduce(string cityintroduce) {
  this.cityintroduce = cityintroduce;
 }
}

cityrepository

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.dalaoyang.repository;
import com.dalaoyang.entity.city;
import org.springframework.data.jpa.repository.jparepository;
 
/**
 * @author dalaoyang
 * @description
 * @project springboot_learn
 * @package com.dalaoyang.repository
 * @email [email protected]
 * @date 2018/4/7
 */
public interface cityrepository extends jparepository<city,integer> {
}

citycontroller

?
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
42
43
44
45
46
47
48
package com.dalaoyang.controller;
import com.dalaoyang.entity.city;
import com.dalaoyang.repository.cityrepository;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
 * @author dalaoyang
 * @description
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email [email protected]
 * @date 2018/4/7
 */
@restcontroller
public class citycontroller {
 @autowired
 private cityrepository cityrepository;
 
 //http://localhost:8888/savecity?cityname=北京&cityintroduce=中國首都
 @getmapping(value = "savecity")
 public string savecity(string cityname,string cityintroduce){
  city city = new city(cityname,cityintroduce);
  cityrepository.save(city);
  return "success";
 }
 
 //http://localhost:8888/deletecity?cityid=2
 @getmapping(value = "deletecity")
 public string deletecity(int cityid){
  cityrepository.delete(cityid);
  return "success";
 }
 
 //http://localhost:8888/updatecity?cityid=3&cityname=沈陽&cityintroduce=遼寧省省會
 @getmapping(value = "updatecity")
 public string updatecity(int cityid,string cityname,string cityintroduce){
  city city = new city(cityid,cityname,cityintroduce);
  cityrepository.save(city);
  return "success";
 }
 //http://localhost:8888/getcitybyid?cityid=3
 @getmapping(value = "getcitybyid")
 public city getcitybyid(int cityid){
  city city = cityrepository.findone(cityid);
  return city;
 }
}

然后啟動項目,可以看到控制臺已經創建了city表。

SpringBoot集成阿里巴巴Druid監控的示例代碼

然后訪問http://localhost:8888/druid,可以看到如下圖:

SpringBoot集成阿里巴巴Druid監控的示例代碼

輸入賬號密碼admin,admin,如下圖

SpringBoot集成阿里巴巴Druid監控的示例代碼

然后這時我們可以訪問http://localhost:8888/savecity?cityname=北京&cityintroduce=中國首都

然后點擊導航上面的sql監控,如下圖,

SpringBoot集成阿里巴巴Druid監控的示例代碼

從上圖可以看到啟動項目創建表的sql已經剛剛執行的sql。到這里整合已經完成了。

源碼下載 :https://gitee.com/dalaoyang/springboot_learn

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://juejin.im/post/5aceec94f265da2395315d68

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男人狂躁女人gif动态图 | 日本一道本中文字幕 | aaa级黄色片| 嫩草影院国产 | 暖暖高清日本在线 | 国产精品免费网站 | 99亚洲| 婷婷色天使在线视频观看 | 精品久久综合一区二区 | 91麻豆精品国产自产在线 | 无人在线观看免费高清视频播放 | 精品视频 九九九 | 四虎永久网址在线观看 | xnxx18美女| 韩国一区二区三区 | 四虎在线免费播放 | 操大肥b | 亚洲国产精品一区二区久久 | 国产欧美一区二区精品久久久 | 国产99在线a视频 | 女教师系列三上悠亚在线观看 | 日韩v| 95在线观看精品视频 | 小草观看免费高清视频 | 精品亚洲综合在线第一区 | 黑人巨荃大战乌克兰美女 | 无限韩国视频免费播放 | 成人网欧美亚洲影视图片 | 亚洲欧美日韩精品久久亚洲区 | 成人蜜桃网 | 挺进白嫩老师下面视频 | 日产2021免费一二三四区 | 情侣奴伺候女王第2部分小说 | 娇小XXXXX第一次出血 | 艹艹逼| fistingvideos头交尿眼 | 草莓视频榴莲视频 | 91国内在线国内在线播放 | 岛国在线播放v片免费 | 叛佛 作者满栀小说免费阅读 | 3d动漫被吸乳羞羞 |