SpringBoot(七):集成DataSource与Druid监控配置
绑定DataSource:Spring Boot默认的数据源是:at.jdbc.pool.DataSource,Druid是Java语⾔中最好的数据库连接池,并且能够提供强⼤的监控和扩展功能,⽽且datasource是⼀般项⽬都需要使⽤的功能。因此,这⾥将学习如何使⽤springboot与druid集成datasource。
springboot2.0.1+alibaba druid组件导⼊maven:
1)导⼊alibaba druid组件:
<!--druid 依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
备注:具体请查看官⽹提供的使⽤介绍:github/alibaba/druid/tree/master/druid-spring-boot-starter。
2)导⼊mysql驱动包:
<!--MySQL Driver驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3)导⼊springboot jpa集成组件:
<!-- Spring Boot的JPA依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
此时,l配置内容为:
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
3 xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
4<modelVersion>4.0.0</modelVersion>
5
6<groupId>com.dx</groupId>
7<artifactId>springboot-helloword</artifactId>
8<version>0.0.1-SNAPSHOT</version>
9<packaging>jar</packaging>
10
11<name>springboot-helloword</name>
12<description>Demo project for Spring Boot</description>
13
14<parent>
15<groupId>org.springframework.boot</groupId>
16<artifactId>spring-boot-starter-parent</artifactId>
17<version>2.0.1.RELEASE</version>
18<relativePath/><!-- lookup parent from repository -->
19</parent>
20
21<properties>
22<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23&porting.outputEncoding>UTF-8</porting.outputEncoding>
24<java.version>1.8</java.version>
25</properties>
26
27<dependencies>
28<dependency>
29<groupId>org.springframework.boot</groupId>
30<artifactId>spring-boot-starter-web</artifactId>
31</dependency>
32<!--完成get set toString注册 -->
33<dependency>
34<groupId>org.projectlombok</groupId>
35<artifactId>lombok</artifactId>
36</dependency>
37<!--springboot 热部署⼯具-->
38<dependency>
39<groupId>org.springframework.boot</groupId>
40<artifactId>spring-boot-devtools</artifactId>
41<optional>true</optional>
42</dependency>
43<dependency>
44<groupId>org.springframework.boot</groupId>
45<artifactId>spring-boot-starter-test</artifactId>
46<scope>test</scope>
47</dependency>
48<dependency>
49<groupId>org.springframework.boot</groupId>
50<artifactId>spring-boot-starter-thymeleaf</artifactId>
51</dependency>
52<!--druid 依赖-->
53<dependency>
54<groupId>com.alibaba</groupId>
55<artifactId>druid-spring-boot-starter</artifactId>
56<version>1.1.9</version>
57</dependency>
58<!--MySQL Driver驱动包-->
59<dependency>
60<groupId>mysql</groupId>
61<artifactId>mysql-connector-java</artifactId>
62<scope>runtime</scope>
63</dependency>
64<!-- Spring Boot的JPA依赖包-->
65<dependency>
66<groupId>org.springframework.boot</groupId>
67<artifactId>spring-boot-starter-data-jpa</artifactId>
druid连接池配置详解68</dependency>
69</dependencies>
70
71<build>
72<plugins>
73<plugin>
74<groupId>org.springframework.boot</groupId>
75<artifactId>spring-boot-maven-plugin</artifactId>
76</plugin>
77</plugins>
78</build>
79
80
81</project>
View Code
⾃定义datasource配置项加载:
1)在src/resources/application.properties中添加以下配置信息:
# ⾃定义数据库配置信息
db.username=root
db.password=root
db.url=jdbc:mysql://localhost:3306/springboot_helloword
db.sql.jdbc.Driver
2)修改项⽬的启动⼊⼝,添加DataSource的bean注⼊,及标注⾃定义参数配置注⼊:package ller;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import javax.sql.DataSource;
@SpringBootApplication
public class SpringbootHellowordApplication {
@Bean
@ConfigurationProperties(prefix = "db")
public DataSource dateSource() {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
public static void main(String[] args) {
SpringApplication.run(SpringbootHellowordApplication.class, args);
}
}
这⾥的dateSource() 启到的作⽤就是注⼊datasourcebean,同时加载⾃定义配置项的作⽤。
3)添加测试controller服务HelloWordController.java类:
package ller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.sql.DataSource;
import java.sql.SQLException;
@Controller
public class HelloWordController {
@Autowired
private DataSource dataSource;
@RequestMapping("/index")
@ResponseBody
public String index() throws SQLException {
System.out.Connection());
System.out.println(dataSource);
return "hello spring boot";
}
}
4)测试,在浏览器中访问localhost:8888/index,查看控制台打印信息:
{
CreateTime:"2018-04-09 20:41:41",
ActiveCount:1,
PoolingCount:0,
CreateCount:1,
DestroyCount:0,
CloseCount:4,
ConnectCount:5,
Connections:[
]
}
使⽤springboot集成的datasource配置项:
1)修改datasource参数配置,使⽤系统集成参数配置项:
## ⾃定义数据库配置信息
#db.username=root
#db.password=root
#db.url=jdbc:mysql://localhost:3306/springboot_helloword
#db.sql.jdbc.Driver
#druid datasouce database settings begin
spring.datasource.druid.db-type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.sql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/springboot_helloword?allowMultiQueries=true&autoReconnect=true&characterEncoding=utf-8 spring.datasource.druid.username=root
spring.datasource.druid.password=root
# 下⾯为连接池的补充设置,应⽤到上⾯所有数据源中
# 初始化⼤⼩,最⼩,最⼤
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
# 配置获取连接等待超时的时间
spring.datasource.druid.max-wait=60000
# 配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.st-while-idle=true
spring.st-on-borrow=false
spring.st-on-return=false
# 打开PSCache,并且指定每个连接上PSCache的⼤⼩
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
# 配置监控统计拦截的filters,去掉后监控界⾯sql⽆法统计,'wall'⽤于防⽕墙
spring.datasource.tion-logger-name=stat,wall,log4j
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.t-properties.=Sql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
spring.datasource.druid.use-global-data-source-stat=true
2)在springboot服务启动类不⽤注⼊其他bean和加载参数配置:
package ller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootHellowordApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootHellowordApplication.class, args);
}
}
3)测试服务类HelloWordController.java与上边⼀样即可。
4)测试,在浏览器中访问localhost:8888/index,查看控制台打印信息:
com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3e6c29a1
{
CreateTime:"2018-04-09 21:15:51",
ActiveCount:1,
PoolingCount:4,
CreateCount:5,
DestroyCount:0,
CloseCount:4,
ConnectCount:5,
Connections:[
{ID:1407373927, ConnectTime:"2018-04-09 21:15:51", UseCount:0, LastActiveTime:"2018-04-09 21:15:51"}, {ID:485935020, ConnectTime:"2018-04-09 21:15:51", UseCount:0, LastActiveTime:"2018-04-09 21:15:51"}, {ID:1400896839, ConnectTime:"2018-04-09 21:15:51", UseCount:0, LastActiveTime:"2018-04-09 21:15:51"}, {ID:209844136, ConnectTime:"2018-04-09 21:15:51", UseCount:0, LastActiveTime:"2018-04-09 21:15:51"} ]
}
[
{ID:1407373927,poolStatements:[]},
{ID:485935020,poolStatements:[]},
{ID:1400896839,poolStatements:[]},
{ID:209844136,poolStatements:[]}
]
配置监控统计功能:
1)配置Servlet(监控视图配置):
如下是在SpringBoot项⽬中基于注解的配置,如果是l配置,按规则配置即可。
package com.base.servlet;
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* druid数据源状态监控.
* */
@WebServlet(urlPatterns="/druid/*",
initParams={
@WebInitParam(name="allow",value="192.168.1.72,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="123456"),// 密码
@WebInitParam(name="resetEnable",value="false")// 禁⽤HTML页⾯上的“Reset All”功能
}
)
public class DruidStatViewServlet extends StatViewServlet {
private static final long serialVersionUID = 1L;
}
2)过滤不需要监控的后缀:
package com.base.servlet;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* druid过滤器.
*/
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
initParams = {
@WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略资源
}
)
public class DruidStatFilter extends WebStatFilter {
}
3)测试配置是否成功。
访问⽹址:localhost:8888/druid/index.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论