Spring Boot使用Druid進行維度的統計和監控
阿新 • • 發佈:2019-02-15
文章目錄
##Druid##
Druid:一款為監控而生的資料庫連線池框架,整個專案由資料庫連線池、外掛框架和SQL解析器組成。
Druid功能介於PowerDrill和Dremel之間,它幾乎實現了Dremel的所有功能,並且從PowerDrill吸收一些有趣的資料格式。Druid允許以類似Dremel和PowerDrill的方式進行單表查詢,同時還增加了一些新特性,如為區域性巢狀資料結構提供列式儲存格式、為快速過濾做索引、實時攝取和查詢、高容錯的分散式體系架構等。
##Spring Boot##
spring框架作為JavaEE框架領域的一款重要的開源框架,在企業應用開發中有著很重要的作用,同時Spring框架及其子框架很多,所以知識量很廣。
Spring Boot:一款Spring框架的子框架,也可以叫微框架,是2014年推出的一款使Spring框架開發變得容易的框架。學過Spring框架的都知識,Spring框架難以避免地需要配置不少XMl,而使用Spring Boot框架的話,就可以使用註解開發,極大地簡化基於Spring框架的開發。Spring Boot充分利用了JavaConfig的配置模式以及“約定優於配置”的理念,能夠極大的簡化基於Spring MVC的Web應用和REST服務開發。
然後本部落格介紹基於IDEA編輯器的Spring Boot專案建立和部署。
##Spring Boot使用Druid監控##
###maven配置###
要配置spring Boot實現一個Demo的話,只要加入spring-boot-starter(核心模組)和spring-boot-starter-web(因為這個一個Web專案),可以參考我的配置,這裡使用了Spring Boot熱部署,需要去github上搜索jar:springloaded-1.2.4.RELEASE.jar,然後下載放在專案的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 #資料庫訪問配置 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 #資料來源配置,初始化大小、最小、最大 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記錄 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
###方式一原生的servlet和filter方式###
編寫Servlet類:
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白名單 (沒有配置或者為空,則允許所有訪問) @WebInitParam(name = "deny", value = "192.168.1.73"),// IP黑名單 (存在共同時,deny優先於allow) @WebInitParam(name = "loginUsername", value = "admin"),// 使用者名稱 @WebInitParam(name = "loginPassword", value = "123"),// 密碼 @WebInitParam(name = "resetEnable", value = "false")// 禁用HTML頁面上的“Reset All”功能) } ) public class DruidStatViewServlet extends StatViewServlet{ }
Filter類:
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啟動類裡設定Servlet自動掃描,不然會出現404頁面找不到錯誤,使用@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);
}
}
###方式二使用程式碼註冊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 {
/**
* 註冊ServletRegistrationBean
* @return
*/
@Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid1/*");
/** 初始化引數配置,initParams**/
//白名單
bean.addInitParameter("allow", "127.0.0.1");
//IP黑名單 (存在共同時,deny優先於allow) : 如果滿足deny的話提示:Sorry, you are not permitted to view this page.
bean.addInitParameter("deny", "192.168.1.73");
//登入檢視資訊的賬號密碼.
bean.addInitParameter("loginUsername", "admin2");
bean.addInitParameter("loginPassword", "123");
//是否能夠重置資料.
bean.addInitParameter("resetEnable", "false");
return bean;
}
/**
* 註冊FilterRegistrationBean
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
//新增過濾規則.
bean.addUrlPatterns("/*");
//新增不需要忽略的格式資訊.
bean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
return bean;
}
}