欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot使用Druid进行维度的统计和监控

程序员文章站 2024-02-27 18:43:21
druid druid:一款为监控而生的数据库连接池框架,整个项目由数据库连接池、插件框架和sql解析器组成。 druid功能介于powerdrill和dremel之间...

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访问,我的项目访问路径:,这个需要自己修改,我的context配置为springbootdemo,端口配置为8087,这些可以参考我上一篇博客

Spring Boot使用Druid进行维度的统计和监控

通过平台进行监控 

Spring Boot使用Druid进行维度的统计和监控

以上所述是小编给大家介绍的spring boot使用druid进行维度的统计和监控,希望对大家有所帮助