继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Spring Boot2.x 的Druid连接池配置[附带监控]

小草1434
关注TA
已关注
手记 7
粉丝 13
获赞 34

开发环境

Spring Boot 2.x[目前使用2.1.x]
Maven 3.5.x[新版]
Eclipse 4.7+[新版]
lombok[Eclipse插件,安装步骤详见 http://lb-chen.cn:9000/article/10]
STS[Eclipse插件,能够快速构建spring boot 的项目,或者可以使用start.spring.io进行构建]

父依赖【Spring Boot 2.1.x版本】

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.1.RELEASE</version>
</parent>

主要依赖【3个】

<!--Druid 连接池与Spring Boot结合的依赖-->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid-spring-boot-starter</artifactId>
	<version>1.1.10</version>
</dependency>
<!--Spring Boot Web 依赖,Druid中的某些组件还是有依赖Web组件的,毕竟监控是需要页面显示的-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql 依赖【8.0+版本】,当然spring boot也可以连接其他关系型数据库,但是此代码不做其他示例-->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

Spring Boot配置文件配置

#datasource base config
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.url=jdbc:mysql://数据库IP:端口/数据库名称?useUnicode=true&useSSL=false
#annoated driverClassName,have [cj]
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#druid pool standard config
spring.datasource.druid.max-active=30
spring.datasource.druid.initial-size=3
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-wait=12000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=30000
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-open-prepared-statements=30

spring.datasource.druid.validation-query=select 1 from dual
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false

#druid extends config
#druid sql firewall monitor
spring.datasource.druid.filter.wall.enabled=true

#druid sql monitor
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=10000
spring.datasource.druid.filter.stat.merge-sql=true

#druid uri monitor
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*

#druid session monitor
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.profile-enable=true

#druid spring monitor
spring.datasource.druid.aop-patterns=com.xcky.*

#druid login user config
spring.datasource.druid.stat-view-servlet.login-username=root
spring.datasource.druid.stat-view-servlet.login-password=root

基本测试前提:

加上一些基本操作数据库操作的controller,
然后访问项目: http://ip:端口/druid
然后输入上面配置的用户和密码root即可访问

附1:本项目原本的pom依赖【含简单测试例子】

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
	</parent>
	<groupId>com.xcky</groupId>
	<artifactId>shop-druid</artifactId>
	<version>0.1</version>
	<name>shop-druid</name>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

附2:本项目示范代码及代码示例

示例:http://lb-chen.cn:9005/druid 用户和密码都为root

如果监控台没有任何数据,请访问如下地址再刷新控制台即可

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP