springboot druid監控


源地址:https://www.jianshu.com/p/be8da0c99d01

-----
##Druid它好在哪:
#####它可以提供數據源、sql防火牆,web應用,URI監控、Session監控,spring監控等。
#####方便進行針對性改造和優化,提高項目性能。
-----
##*如下是實現的具體操作:*

####一、引入依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.28</version>
</dependency>

####二、配置文件(spring.datasource 下的配置)
```
#配置druid監控參數
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
```
####三、三個關鍵類(沒有試是否可以省略)
(1)DruidConfig
```
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;

@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;
}
}
```

(2)DruidFilter
```
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;

@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略資源
}
)
public class DruidFilter extends WebStatFilter {
}
```
(3)DruidServlet
```
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import com.alibaba.druid.support.http.StatViewServlet;

@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。(一般都會加過了)

#####五、看看效果
訪問頁面[http://127.0.0.1:8080/](http://127.0.0.1:8080/){projectName}/druid/index.html就可以訪問監控頁面了,druid已經將數據友好的顯示到頁面上了,包括每條sql執行的次數,執行的時間,最慢時間等等很多詳細信息,供我們參考,找到哪些執行效率低的耗時長的語句進行優化。同時提供數據源、sql防火牆,web應用,URI監控、Session監控,spring監控等。
如圖:可以看到第一個sql執行的慢並且被調用的次數多,
####可以針對地優化一下了。

![image.png](https://upload-images.jianshu.io/upload_images/17375175-c2b6075021634719.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM