Spring Boot使用Druid進(jìn)行維度的統(tǒng)計(jì)和監(jiān)控
Druid
Druid:一款為監(jiān)控而生的數(shù)據(jù)庫(kù)連接池框架,整個(gè)項(xiàng)目由數(shù)據(jù)庫(kù)連接池、插件框架和SQL解析器組成。
Druid功能介于PowerDrill和Dremel之間,它幾乎實(shí)現(xiàn)了Dremel的所有功能,并且從PowerDrill吸收一些有趣的數(shù)據(jù)格式。Druid允許以類(lèi)似Dremel和PowerDrill的方式進(jìn)行單表查詢(xún),同時(shí)還增加了一些新特性,如為局部嵌套數(shù)據(jù)結(jié)構(gòu)提供列式存儲(chǔ)格式、為快速過(guò)濾做索引、實(shí)時(shí)攝取和查詢(xún)、高容錯(cuò)的分布式體系架構(gòu)等。
Spring Boot
spring框架作為JavaEE框架領(lǐng)域的一款重要的開(kāi)源框架,在企業(yè)應(yīng)用開(kāi)發(fā)中有著很重要的作用,同時(shí)Spring框架及其子框架很多,所以知識(shí)量很廣。
Spring Boot:一款Spring框架的子框架,也可以叫微框架,是2014年推出的一款使Spring框架開(kāi)發(fā)變得容易的框架。學(xué)過(guò)Spring框架的都知識(shí),Spring框架難以避免地需要配置不少XMl,而使用Spring Boot框架的話(huà),就可以使用注解開(kāi)發(fā),極大地簡(jiǎn)化基于Spring框架的開(kāi)發(fā)。
Spring Boot充分利用了JavaConfig的配置模式以及“約定優(yōu)于配置”的理念,能夠極大的簡(jiǎn)化基于Spring MVC的Web應(yīng)用和REST服務(wù)開(kāi)發(fā)。
然后通過(guò)本文給大家介紹基于IDEA編輯器的Spring Boot項(xiàng)目創(chuàng)建和部署。
Spring Boot使用Druid監(jiān)控
maven配置
要配置spring Boot實(shí)現(xiàn)一個(gè)Demo的話(huà),只要加入spring-boot-starter(核心模塊)和spring-boot-starter-web(因?yàn)檫@個(gè)一個(gè)Web項(xiàng)目),可以參考我的配置,這里使用了Spring Boot熱部署,需要去github上搜索jar:springloaded-1.2.4.RELEASE.jar,然后下載放在項(xiàng)目的lib文件夾里,可以參考我的配置
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<druid.version>1.0.24</druid.version>
<mysql.version>5.1.27</mysql.version>
<spring-boot-admin.version>1.4.5</spring-boot-admin.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
<dependencies>
<!--springloaded hot deploy -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/springloaded-1.2.5.RELEASE.jar</systemPath>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application.properties配置
server.context-path=/springbootdemo #數(shù)據(jù)庫(kù)訪問(wèn)配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/myblog spring.datasource.username=root spring.datasource.password=root #數(shù)據(jù)源配置,初始化大小、最小、最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 #連接等待超時(shí)時(shí)間 spring.datasource.maxWait=60000 #配置隔多久進(jìn)行一次檢測(cè)(檢測(cè)可以關(guān)閉的空閑連接) spring.datasource.timeBetweenEvictionRunsMillis=60000 #配置連接在池中的最小生存時(shí)間 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻 spring.datasource.filters=stat,wall,log4j # 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能;慢SQL記錄 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
方式一原生的servlet和filter方式
編寫(xiě)Servlet類(lèi):
package com.example.web.servlet;
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* Created by Administrator on 2017/4/28.
*/
@WebServlet(urlPatterns = "/druid/*",
initParams = {
@WebInitParam(name = "allow", value = "192.168.10.25,127.0.0.1"),// IP白名單 (沒(méi)有配置或者為空,則允許所有訪問(wèn))
@WebInitParam(name = "deny", value = "192.168.1.73"),// IP黑名單 (存在共同時(shí),deny優(yōu)先于allow)
@WebInitParam(name = "loginUsername", value = "admin"),// 用戶(hù)名
@WebInitParam(name = "loginPassword", value = "123"),// 密碼
@WebInitParam(name = "resetEnable", value = "false")// 禁用HTML頁(yè)面上的“Reset All”功能)
}
)
public class DruidStatViewServlet extends StatViewServlet{
}
Filter類(lèi):
package com.example.web.filter;
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
/**
* Created by Administrator on 2017/4/28.
*/
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略資源
}
)
public class DruidWebStatFilter extends WebStatFilter{
}
然后,需要在Spring Boot啟動(dòng)類(lèi)里設(shè)置Servlet自動(dòng)掃描,不然會(huì)出現(xiàn)404頁(yè)面找不到錯(cuò)誤,使用
@ServletComponentScan注解
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@ServletComponentScan
@EnableAsync
public class Application implements EmbeddedServletContainerCustomizer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
configurableEmbeddedServletContainer.setPort(8087);
}
}
方式二使用代碼注冊(cè)Servlet和Filter
package com.example.config;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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;
/**
* Created by Administrator on 2017/4/28.
*/
@Configuration
public class DruidConfiguration {
/**
* 注冊(cè)ServletRegistrationBean
* @return
*/
@Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid1/*");
/** 初始化參數(shù)配置,initParams**/
//白名單
bean.addInitParameter("allow", "127.0.0.1");
//IP黑名單 (存在共同時(shí),deny優(yōu)先于allow) : 如果滿(mǎn)足deny的話(huà)提示:Sorry, you are not permitted to view this page.
bean.addInitParameter("deny", "192.168.1.73");
//登錄查看信息的賬號(hào)密碼.
bean.addInitParameter("loginUsername", "admin2");
bean.addInitParameter("loginPassword", "123");
//是否能夠重置數(shù)據(jù).
bean.addInitParameter("resetEnable", "false");
return bean;
}
/**
* 注冊(cè)FilterRegistrationBean
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
//添加過(guò)濾規(guī)則.
bean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
bean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
return bean;
}
}
項(xiàng)目監(jiān)控
然后輸入url訪問(wèn),我的項(xiàng)目訪問(wèn)路徑:http://localhost:8087/springbootdemo/druid/login.html,這個(gè)需要自己修改,我的Context配置為springbootdemo,端口配置為8087,這些可以參考我上一篇博客

通過(guò)平臺(tái)進(jìn)行監(jiān)控

以上所述是小編給大家介紹的Spring Boot使用Druid進(jìn)行維度的統(tǒng)計(jì)和監(jiān)控,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例
- 使用SpringBoot簡(jiǎn)單了解Druid的監(jiān)控系統(tǒng)的配置方法
- spring boot基于DRUID實(shí)現(xiàn)數(shù)據(jù)源監(jiān)控過(guò)程解析
- eBay 打造基于 Apache Druid 的大數(shù)據(jù)實(shí)時(shí)監(jiān)控系統(tǒng)
- SpringBoot集成阿里巴巴Druid監(jiān)控的示例代碼
- Druid基本配置及內(nèi)置監(jiān)控使用_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- Spring Boot使用Druid和監(jiān)控配置方法
- Druid監(jiān)控分布式實(shí)現(xiàn)過(guò)程解析
相關(guān)文章
解決idea爆紅 cant resolve symbol String的問(wèn)題解析
連著出差幾個(gè)禮拜沒(méi)有使用idea開(kāi)發(fā)工具,突然一天打開(kāi)電腦發(fā)現(xiàn)idea里的代碼全部爆紅,懵逼不如所措,很多朋友建議我按住Alt+回車(chē)設(shè)置jdk就能解決,但是仍然報(bào)錯(cuò),經(jīng)過(guò)幾個(gè)小時(shí)的倒騰最終解決,遇到此問(wèn)題的朋友參考下本文吧2021-06-06
詳解springcloud 基于feign的服務(wù)接口的統(tǒng)一hystrix降級(jí)處理
這篇文章主要介紹了詳解springcloud 基于feign的服務(wù)接口的統(tǒng)一hystrix降級(jí)處理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Spring Security使用Lambda DSL配置流程詳解
Spring Security 5.2 對(duì) Lambda DSL 語(yǔ)法的增強(qiáng),允許使用lambda配置HttpSecurity、ServerHttpSecurity,重要提醒,之前的配置方法仍然有效。lambda的添加旨在提供更大的靈活性,但是用法是可選的。讓我們看一下HttpSecurity的lambda配置與以前的配置樣式相比2023-02-02
Java?數(shù)據(jù)庫(kù)連接池DBPool?介紹
這篇文章主要給大家分享了Java?數(shù)據(jù)庫(kù)連接池DBPool?介紹,<BR>DBPool是一個(gè)高效的易配置的數(shù)據(jù)庫(kù)連接池。它除了支持連接池應(yīng)有的功能之外,還包括了一個(gè)對(duì)象池使你能夠開(kāi)發(fā)一個(gè)滿(mǎn)足自已需求的數(shù)據(jù)庫(kù)連接池,下面一起來(lái)看看文章內(nèi)容的詳細(xì)介紹吧,需要的朋友可以參考一下2021-11-11
Java中數(shù)組復(fù)制的三種方式小結(jié)
在Java中,數(shù)組復(fù)制是一種常見(jiàn)的操作,它允許開(kāi)發(fā)人員在不修改原始數(shù)組的情況下創(chuàng)建一個(gè)新的數(shù)組,本文就來(lái)介紹三種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Java數(shù)據(jù)庫(kù)連接_jdbc-odbc橋連接方式(詳解)
下面小編就為大家?guī)?lái)一篇Java數(shù)據(jù)庫(kù)連接_jdbc-odbc橋連接方式(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
基于Protobuf動(dòng)態(tài)解析在Java中的應(yīng)用 包含例子程序
下面小編就為大家?guī)?lái)一篇基于Protobuf動(dòng)態(tài)解析在Java中的應(yīng)用 包含例子程序。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

