Home > Java > javaTutorial > Spring Boot configuration method java code example through Druid and monitoring

Spring Boot configuration method java code example through Druid and monitoring

Y2J
Release: 2017-05-04 10:06:17
Original
1455 people have browsed it

Druid is the best database connection pool in the Java language and can provide powerful monitoring and expansion functions. The following explains how to configure and use Druid in Spring Boot

The default data source of Spring Boot is: org.apache.tomcat.jdbc.pool.DataSource

Druid is the best in the Java language Database connection pool, and can provide powerful monitoring and expansion functions.

The following explains how to configure and use Druid in Spring Boot

(1) Add Maven dependency (or jar package)\

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.18</version>
</dependency>
Copy after login

2), configure data source related information

# 数据库访问配置
# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
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
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
Copy after login

(3) Configure monitoring statistics function

ConfigurationServlet

The following is the annotation-based configuration in the SpringBoot project, if it is web.xml Configuration, just configure according to the rules.

DruidStatViewServlet:

import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
 * druid数据源状态监控.
 * Created by winner_0715 on 2017/4/19.
 */
@WebServlet(urlPatterns = "/druid/*",
    initParams = {
        // IP白名单 (没有配置或者为空,则允许所有访问)
        @WebInitParam(name = "allow", value = "192.168.1.72,127.0.0.1"),
        // IP黑名单 (存在共同时,deny优先于allow)
        @WebInitParam(name = "deny", value = "192.168.1.73"),
        // 用户名
        @WebInitParam(name = "loginUsername", value = "admin"),
        // 密码
        @WebInitParam(name = "loginPassword", value = "123456"),
        // 禁用HTML页面上的“Reset All”功能
        @WebInitParam(name = "resetEnable", value = "false")
    }
)
public class DruidStatViewServlet extends StatViewServlet {
  private static final long serialVersionUID = 1L;
}
DruidStatFilter:
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
/**
 * druid过滤器.
 * Created by winner_0715 on 2017/4/19.
 */
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
    initParams = {
        // 忽略资源
        @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")
    }
)
public class DruidStatFilter extends WebStatFilter {
}
Copy after login

Finally add annotations to the startup class: @ServletComponentScan enables spring to scan the servlets and filters we wrote ourselves.

Be careful not to forget to add the @ServletComponentScan annotation on SpringBootSampleApplication.java, otherwise it will be 404.

Then Start the project and visit 127.0.0.1:8080/druid/index.html to view the data source and SQL statistics, etc.

(4) Method 2 of configuring the monitoring system:

The monitoring method configured above uses native servlet, filter method, [email protected]� After processing, you will find that our servlet and filter do not have any encoding at all.

Here we will use another way to handle it: use code to register Servlet:

DruidConfiguration:

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;
/**
 * 这样的方式不需要添加注解:@ServletComponentScan
 * Created by winner_0715 on 2017/4/19.
 */
@Configuration
public class DruidConfiguration {
  /**
   * 注册一个StatViewServlet
   *
   * @return
   */
  @Bean
  public ServletRegistrationBean DruidStatViewServle2() {
    //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid2/*");
    //添加初始化参数:initParams
    //白名单:
    servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
    //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
    servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
    //登录查看信息的账号密码.
    servletRegistrationBean.addInitParameter("loginUsername", "admin2");
    servletRegistrationBean.addInitParameter("loginPassword", "123456");
    //是否能够重置数据.
    servletRegistrationBean.addInitParameter("resetEnable", "false");
    return servletRegistrationBean;
  }
  /**
   * 注册一个:filterRegistrationBean
   *
   * @return
   */
  @Bean
  public FilterRegistrationBean druidStatFilter2() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
    //添加过滤规则.
    filterRegistrationBean.addUrlPatterns("/*");
    //添加不需要忽略的格式信息.
    filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
    return filterRegistrationBean;
  }
}
Copy after login

Start the application and you can access: 127.0.0.1:8080/ druid2/index.htmlEnter the account and password: admin2/123456 to access.

The above is the detailed content of Spring Boot configuration method java code example through Druid and monitoring. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template