手记

Spring Boot集成 Druid 监控连接池【附带xml配置】

Spring Boot集成 Druid 监控连接池(5步骤)
1.项目依赖包构建
2.增加druid的spring监控配置
3.修改项目配置文件(application.properties)
4.增加druid配置类
5.在启动类上增加druid的spring监控配置文件引用的注解

具体步骤详情如下:
步骤1:
POM中的主要依赖

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.1.9</version>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

步骤2:
在项目的resource目录下(推荐)放置druid-spring.xml文件,当然直接放在src上也可以,xml内容如下,当然写成Java Bean进行对应配置也是可以的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 配置_Druid和Spring关联监控配置 -->
    <bean id="druid-stat-interceptor"
        class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>
    <!-- 方法名正则匹配拦截配置 -->
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
        scope="prototype">
        <property name="patterns">
            <list>
            	<!--此处根据实际项目的包名更改-->
                <value>com.beini.*</value>
            </list>
        </property>
    </bean>
    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="druid-stat-interceptor"
	pointcut-ref="druid-stat-pointcut" />
    </aop:config>
</beans>

步骤3:
修改项目配置文件(application.properties):

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shop2?useUnicode=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=round&useSSL=false&rewriteBatchedStatements=true&socketTimeout=30000&connectTimeout=3000
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource

#druid standard config
spring.datasource.maxActive=30
spring.datasource.initialSize=3
spring.datasource.minIdle=3
spring.datasource.maxWait=120000
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

#druid extends config
spring.datasource.filters=stat,wall,log4j
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
spring.datasource.logSlowSql=true

步骤4:
新建以下连接池配置类(建议配置类所在目录为项目启动类下的直接子包下):
/Druid配置类/

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
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;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidConfig {
	@Value("${spring.datasource.url}")
	private String dbUrl;
	@Value("${spring.datasource.username}")
	private String username;
	@Value("${spring.datasource.password}")
	private String password;
	@Value("${spring.datasource.driver-class-name}")
	private String driverClassName;
	@Value("${spring.datasource.initialSize}")
	private int initialSize;
	@Value("${spring.datasource.minIdle}")
	private int minIdle;
	@Value("${spring.datasource.maxActive}")
	private int maxActive;
	@Value("${spring.datasource.maxWait}")
	private int maxWait;
	@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;
	@Value("${spring.datasource.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;
	@Value("${spring.datasource.validationQuery}")
	private String validationQuery;
	@Value("${spring.datasource.testWhileIdle}")
	private boolean testWhileIdle;
	@Value("${spring.datasource.testOnBorrow}")
	private boolean testOnBorrow;
	@Value("${spring.datasource.testOnReturn}")
	private boolean testOnReturn;
	@Value("${spring.datasource.poolPreparedStatements}")
	private boolean poolPreparedStatements;
	@Value("${spring.datasource.filters}")
	private String filters;
	@Value("${spring.datasource.logSlowSql}")
	private String logSlowSql;

	@Bean
	// @Primary 注解作用是当程序选择dataSource时选择被注解的这个
	@Primary
	public DataSource dataSource() {
		DruidDataSource datasource = new DruidDataSource();
		datasource.setUrl(dbUrl);
		datasource.setUsername(username);
		datasource.setPassword(password);
		datasource.setDriverClassName(driverClassName);
		datasource.setInitialSize(initialSize);
		datasource.setMinIdle(minIdle);
		datasource.setMaxActive(maxActive);
		datasource.setMaxWait(maxWait);
		datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		datasource.setValidationQuery(validationQuery);
		datasource.setTestWhileIdle(testWhileIdle);
		datasource.setTestOnBorrow(testOnBorrow);
		datasource.setTestOnReturn(testOnReturn);
		datasource.setPoolPreparedStatements(poolPreparedStatements);
		try {
			datasource.setFilters(filters);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return datasource;
	}

	@Bean
	public ServletRegistrationBean druidServlet() {
		ServletRegistrationBean reg = new ServletRegistrationBean();
		reg.setServlet(new StatViewServlet());
		reg.addUrlMappings("/druid/*");
		//注意此处正是设置druid的登录用户名和密码的地方,因为此处username和password的变量值都为root,故登录的账号和密码都为root
		reg.addInitParameter("loginUsername", username);
		reg.addInitParameter("loginPassword", password);
		reg.addInitParameter("logSlowSql", logSlowSql);
		return reg;
	}

	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		filterRegistrationBean.addUrlPatterns("/*");
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		filterRegistrationBean.addInitParameter("profileEnable", "true");
		return filterRegistrationBean;
	}
}

步骤5:
在springboot的启动类中加上如下注解:

@ImportResource(locations = { "classpath:druid-spring.xml" })

最后,如果在项目启动之后,在项目的首页访问的基础上(首页URL为: http://localhost:8080/),
在URL后缀加上druid/即可看到druid的登录窗口,其登录密码和账号与连接本地的mysql相同。
如本篇文章的账号和密码都是 “root”。

祝观看此文章的慕课君们学习愉快!
后续会推出properties+Java Bean方式配置,慕课君们可以关注我查看更多文章哦!
示例代码下载处

3人推荐
随时随地看视频
慕课网APP

热门评论

在此非常感谢曾经推荐此篇文章的同学!

另外为了让大家快速上手本篇文章涉及到的知识点,本人之前使用了aliyun的drds开发的一个基本项目【做了删减,但是可以运行使用】,可以直接访问github

https://github.com/XiaoCao1434/beini-drds.git


喜欢的话,可以+start哦!

查看全部评论