springboot+druid(使⽤druid-spring-boot-starter配置)参考:
1.基础配置:
添加依赖:
<project xmlns="/POM/4.0.0"
xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>alibaba-druid-test</groupId>
<artifactId>alibaba-druid-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 继承默认值为Spring Boot -->
<parent>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 2.0.3.RELEASE </version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
<!-- 添加Web应⽤程序的典型依赖项 -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mysql 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<!-- springboot热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
</dependencies>
<!-- 打包为可执⾏jar -->
<build>
<plugins>
<plugin>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
#Druid Spring Boot Starter 配置属性的名称完全遵照 Druid,你可以通过 Spring Boot 配置⽂件来配置Druid数据库连接池和监控,如果没有配置则使⽤默认值。
#JDBC 配置
#spring.datasource.druid.url= jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf-8&useSSL=false
#spring.datasource.druid.username= root
#spring.datasource.druid.password=123456
#spring.datasource.druid.driver-class-name= sql.jdbc.Driver
>>>>##等同以上配置>>>>>#
spring.datasource.url= jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username= root
spring.datasource.password=123456
spring.datasource.driver-class-name= sql.jdbc.Driver
到这⾥就配置完了,没错配置完了,就是这么骚,接下来运⾏项⽬检查下:
2.监控配置:
#配置监控统计拦截的filters,去掉后监控界⾯sql⽆法统计,'wall'⽤于防⽕墙
spring.datasource.druid.filters= stat,wall
>##监控配置
# WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
spring.datasource.abled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.lusions=/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=10
spring.datasource.druid.web-stat-filter.principal-session-name=session_name
spring.datasource.druid.web-stat-filter.principal-cookie-name=cookie_name
spring.datasource.druid.web-stat-filter.profile-enable=
# StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置默认false
spring.datasource.abled=true
# 配置DruidStatViewServlet
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
#  禁⽤HTML页⾯上的“Reset All”功能
spring.datasource.set-enable=false
spring.datasource.druid.stat-view-servlet.login-username=admin #监控页⾯登录的⽤户名
spring.datasource.druid.stat-view-servlet.login-password=123456 #监控页⾯登录的密码
#IP⽩名单(没有配置或者为空,则允许所有访问)
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1,192.168.0.119
#IP⿊名单 (存在共同时,deny优先于allow)
spring.datasource.druid.stat-view-servlet.deny=
#Spring监控配置,说明请参考Druid Github Wiki,配置_Druid和Spring关联监控配置
spring.datasource.druid.aop-patterns= com.lcf.service.*
登录监控页⾯:
更多配置:
application.properties:
#Druid Spring Boot Starter 配置属性的名称完全遵照 Druid,你可以通过 Spring Boot 配置⽂件来配置Druid数据库连接池和监控,如果没有配置则使⽤默认值。
#JDBC 配置
#spring.datasource.druid.url= jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf-8&useSSL=false
#spring.datasource.druid.username= root
#spring.datasource.druid.password= 123456
#spring.datasource.druid.driver-class-name= sql.jdbc.Driver
>>>>##等同以上配置>>>>>#
spring.datasource.url= jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username= root
spring.datasource.password= 123456
spring.datasource.driver-class-name= sql.jdbc.Driver
#pe=com.alibaba.druid.pool.DruidDataSource
#连接池配置(可选)
# 连接池的配置信息初始化⼤⼩,最⼩,最⼤
spring.datasource.druid.name=testDruidDataSource
#spring.datasource.druid.initial-size=5
#spring.datasource.druid.max-active=20
#spring.datasource.druid.min-idle=5
# 配置获取连接等待超时的时间
#spring.datasource.druid.max-wait= 60000
#spring.datasource.druid.pool-prepared-statements=
#spring.datasource.druid.max-pool-prepared-statement-per-connection-size=
##spring.datasource.druid.max-open-prepared-statements= #和上⾯的等价
#spring.datasource.druid.validation-query=
#spring.datasource.druid.validation-query=
#spring.datasource.druid.validation-query-timeout=
#spring.st-on-borrow=
#spring.st-on-return=
#spring.st-while-idle=
#spring.datasource.druid.time-between-eviction-runs-millis=
#spring.datasource.druid.min-evictable-idle-time-millis=
#spring.datasource.druid.max-evictable-idle-time-millis=
#配置监控统计拦截的filters,去掉后监控界⾯sql⽆法统计,'wall'⽤于防⽕墙
spring.datasource.druid.filters= stat,wall
>##监控配置
# WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
spring.datasource.abled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.lusions=/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=10
spring.datasource.druid.web-stat-filter.principal-session-name=session_name
spring.datasource.druid.web-stat-filter.principal-cookie-name=cookie_name
spring.datasource.druid.web-stat-filter.profile-enable=
# StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置默认false
spring.datasource.abled=true
# 配置DruidStatViewServlet
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
#  禁⽤HTML页⾯上的“Reset All”功能
spring.datasource.set-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
#IP⽩名单(没有配置或者为空,则允许所有访问)
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1,192.168.0.119
#IP⿊名单 (存在共同时,deny优先于allow)
spring.datasource.druid.stat-view-servlet.deny=
#Spring监控配置,说明请参考Druid Github Wiki,配置_Druid和Spring关联监控配置
spring.datasource.druid.aop-patterns= com.lcf.service.*
#⽇志级别
#=debug
#热部署⽣效
abled=true
#------------------------------mybatis------------------------------
mybatis.mapper-locations=classpath:mapper/**.xml
#server.port=8888
#t-path=/lichenfei
当然如果你不喜欢阿⾥的监控页⾯,你也可以⾃⼰⾃定义,⼀下是部分监控api,更多api请看DruidStatService源码package me.ffs.ller;
import org.apachemons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.alibaba.druid.support.http.stat.WebAppStatManager;
import com.alibaba.druid.support.spring.stat.SpringStatManager;
import com.github.gserv.servmons.util.JsonMapper;
/
**
* @author : lichenfei
* @date : 2020年1⽉9⽇
* @time : 下午4:46:54
*
*/
@RestController
@RequestMapping("b/druid")
public class DruidStatController {
private static final Logger logger = Logger(DruidStatController.class);
@GetMapping("stat/{who}")
public String druidStat(@PathVariable("who") String who){
// DruidStatManagerFacade#getDataSourceStatDataList 该⽅法可以获取所有数据源的监控数据,除此之外// DruidStatManagerFacade 还提供了⼀些其他⽅法,你可以按需选择使⽤。
String result ="";
try{
logger.info(">>>开始查询druid监控数据....");
DruidStatManagerFacade instance = Instance();
if(StringUtils.isBlank(who)){
result ="";
}else{
switch(who){
case"sql.json":// SQL监控
result = SqlStatDataList(null));
break;
case"weburi.json":// webUrl监控
result = Instance().getURIStatData());
break;
case"datasource.json"://数据源监控
result = DataSourceStatDataList());
break;
case"spring.json":// spring-method监控
result = Instance().getMethodStatData());
break;
default:
result ="";
break;
}
}
}catch(Exception e){
logger.info(">>>查询监控数据出现异常, e : {}", e);
springboot aopresult ="";
}
return result;
}
}
开启以下配置即可:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论