这篇文章主要介绍了 Spring Boot 使用 Druid 进行维度的统计和监控, 非常不错, 具有参考借鉴价值, 需要的朋友可以参考下
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;
- }
- }
项目监控
然后输入 url 访问, 我的项目访问路径: http://localhost:8087/springbootdemo/druid/login.html, 这个需要自己修改, 我的 Context 配置为 springbootdemo, 端口配置为 8087, 这些可以参考我上一篇博客
通过平台进行监控
来源: http://www.phperz.com/article/18/0323/357282.html