为什么80%的码农都做不了架构师?>>>   

步骤如下: 1、首先新建2个model(LogInfo,MonitorInfo)

public class LogInfo implements Serializable {private static final long serialVersionUID = 5289821541255302264L;private String sql;private String sqlParam;private int maxParallel;private long executeMillisTotal;private long executeCount;private long executeErrorCount;private Date maxOccurTime;private long histogram_0_1;private long histogram_1_10;private int histogram_100_1000;private int histogram_1000_10000;private int histogram_10000_100000;private int histogram_100000_1000000;private int histogram_1000000_more;private String lastError;private String lastErrorMessage;private String lastErrorStackTrace;private String lastErrorClass;private Date lastErrorTime;private int histogram_10_100;private long executeMillisMax;public String getSql() {return sql;}public void setSql(String sql) {this.sql = sql;}public String getSqlParam() {return sqlParam;}public void setSqlParam(String sqlParam) {this.sqlParam = sqlParam;}public int getMaxParallel() {return maxParallel;}public void setMaxParallel(int maxParallel) {this.maxParallel = maxParallel;}public long getExecuteMillisTotal() {return executeMillisTotal;}public void setExecuteMillisTotal(long executeMillisTotal) {this.executeMillisTotal = executeMillisTotal;}public long getExecuteCount() {return executeCount;}public void setExecuteCount(long executeCount) {this.executeCount = executeCount;}public long getExecuteErrorCount() {return executeErrorCount;}public void setExecuteErrorCount(long executeErrorCount) {this.executeErrorCount = executeErrorCount;}public Date getMaxOccurTime() {return maxOccurTime;}public void setMaxOccurTime(Date maxOccurTime) {this.maxOccurTime = maxOccurTime;}public long getHistogram_0_1() {return histogram_0_1;}public void setHistogram_0_1(long histogram_0_1) {this.histogram_0_1 = histogram_0_1;}public long getHistogram_1_10() {return histogram_1_10;}public void setHistogram_1_10(long histogram_1_10) {this.histogram_1_10 = histogram_1_10;}public int getHistogram_100_1000() {return histogram_100_1000;}public void setHistogram_100_1000(int histogram_100_1000) {this.histogram_100_1000 = histogram_100_1000;}public int getHistogram_1000_10000() {return histogram_1000_10000;}public void setHistogram_1000_10000(int histogram_1000_10000) {this.histogram_1000_10000 = histogram_1000_10000;}public int getHistogram_10000_100000() {return histogram_10000_100000;}public void setHistogram_10000_100000(int histogram_10000_100000) {this.histogram_10000_100000 = histogram_10000_100000;}public int getHistogram_100000_1000000() {return histogram_100000_1000000;}public void setHistogram_100000_1000000(int histogram_100000_1000000) {this.histogram_100000_1000000 = histogram_100000_1000000;}public int getHistogram_1000000_more() {return histogram_1000000_more;}public void setHistogram_1000000_more(int histogram_1000000_more) {this.histogram_1000000_more = histogram_1000000_more;}public String getLastError() {return lastError;}public void setLastError(String lastError) {this.lastError = lastError;}public String getLastErrorMessage() {return lastErrorMessage;}public void setLastErrorMessage(String lastErrorMessage) {this.lastErrorMessage = lastErrorMessage;}public String getLastErrorStackTrace() {return lastErrorStackTrace;}public void setLastErrorStackTrace(String lastErrorStackTrace) {this.lastErrorStackTrace = lastErrorStackTrace;}public String getLastErrorClass() {return lastErrorClass;}public void setLastErrorClass(String lastErrorClass) {this.lastErrorClass = lastErrorClass;}public Date getLastErrorTime() {return lastErrorTime;}public void setLastErrorTime(Date lastErrorTime) {this.lastErrorTime = lastErrorTime;}public int getHistogram_10_100() {return histogram_10_100;}public void setHistogram_10_100(int histogram_10_100) {this.histogram_10_100 = histogram_10_100;}public long getExecuteMillisMax() {return executeMillisMax;}public void setExecuteMillisMax(long executeMillisMax) {this.executeMillisMax = executeMillisMax;}
}public class MonitorInfo implements Serializable {private static final long serialVersionUID = -2127636421971490585L;private List<LogInfo> LogInfos;private String sysIdentify;public List<LogInfo> getLogInfos() {return LogInfos;}public void setLogInfos(List<LogInfo> logInfos) {LogInfos = logInfos;}public String getSysIdentify() {return sysIdentify;}public void setSysIdentify(String sysIdentify) {this.sysIdentify = sysIdentify;}}

2、model建好之后,自定义一个StatLogger,创建一个抽象类 MonitorDataSourceSql来实现获取监控数据,代码如下:

public abstract class MonitorDataSourceSql extends DruidDataSourceStatLoggerAdapterimplements DruidDataSourceStatLogger {private final static Log logger = LogFactory.getLog(DruidPooledConnection.class);public MonitorDataSourceSql() {this.configFromProperties(System.getProperties());}/* * DruidDataSourceStatValue包含这整个SQL监控数据,按需索取* **/@Overridepublic void log(DruidDataSourceStatValue statValue) {List<JdbcSqlStatValue> sqlList = statValue.getSqlList();if (sqlList != null && sqlList.size() > 0) {List<LogInfo> logInfos = new ArrayList<LogInfo>(sqlList.size());MonitorInfo monitorInfo = new MonitorInfo();JdbcSqlStatValue value = null;Throwable executeThrowable = null;LogInfo info = null;try {for (int i = 0; i < sqlList.size(); i++) {value = sqlList.get(i);info = new LogInfo();info.setHistogram_0_1(value.getHistogram_0_1());info.setHistogram_1_10(value.getHistogram_1_10());info.setHistogram_10_100(value.getHistogram_10_100());info.setHistogram_100_1000(value.getHistogram_100_1000());info.setHistogram_1000_10000(value.getHistogram_1000_10000());info.setHistogram_1000000_more(value.getHistogram_1000000_more());info.setHistogram_100000_1000000(value.getHistogram_100000_1000000());info.setHistogram_10000_100000(value.getHistogram_10000_100000());info.setMaxParallel(value.getConcurrentMax());info.setMaxOccurTime(value.getExecuteNanoSpanMaxOccurTime());info.setSql(value.getSql());info.setSqlParam(value.getLastSlowParameters());info.setExecuteMillisTotal(value.getExecuteMillisTotal());info.setExecuteErrorCount(value.getExecuteErrorCount());info.setExecuteMillisMax(value.getExecuteMillisMax());info.setExecuteCount(value.getExecuteCount());executeThrowable = value.getExecuteErrorLast();if (executeThrowable != null) {info.setLastErrorClass(executeThrowable.getClass().getName());info.setLastErrorMessage(executeThrowable.getMessage());info.setLastErrorStackTrace(Utils.toString(executeThrowable.getStackTrace()));info.setLastErrorTime(value.getExecuteErrorLastTime());}logInfos.add(info);}} catch (Exception e) {e.printStackTrace();logger.error("监控数据转换异常", e);}monitorInfo.setLogInfos(logInfos);monitorInfo.setSysIdentify(getIdentify());sendMonitorLog(monitorInfo);}}/*** 监控数据* **/public abstract void sendMonitorLog(MonitorInfo monitorInfo);public abstract String getIdentify();@Overridepublic void configFromProperties(Properties properties) {super.configFromProperties(properties);}@Overridepublic void setLogger(Log logger) {}@Overridepublic void setLoggerName(String loggerName) {super.setLoggerName(loggerName);}}

3、创建MonitorDataSourceSqlImpl 继承刚才创建的抽象类并引入redis,将监控数据放到redis中,这里的JSON使用的也是阿里巴巴的fastjson

public class MonitorDataSourceSqlImpl extends MonitorDataSourceSql {@Autowiredprivate IRedisService cacheService;@Overridepublic void sendMonitorLog(MonitorInfo monitorInfo) {try {String str = JSONObject.toJSONString(monitorInfo);this.cacheService.set(RedisKey.MONITORLOG, str);} catch (Exception e) {e.printStackTrace();}}@Overridepublic String getIdentify() {return null;}}

4、数据源配置,代码如下:

<bean id="webDataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="${web.jdbcUrl}" /><property name="username" value="${web.user}" /><property name="password" value="${web.password}" /><!-- 配置初始化大小、最小、最大 --><property name="initialSize" value="5" /><property name="minIdle" value="2" /><property name="maxActive" value="200" /><!-- 设置数据库编码 为utf8mb4 --><property name="connectionInitSqls" value="set names utf8mb4;" /><!-- 配置获取连接等待超时的时间 --><property name="maxWait" value="60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 'x'" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 记录sql --><property name="filters" value="config,stat,log4j,mergeStat" /><!-- 慢sql 时间定义 --><property name="connectionProperties"             value="druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=${tgbweb.publicKey}" /> <!-- 每3秒保存日志 --><property name="timeBetweenLogStatsMillis" value="3000" /> <!-- 合并多个DruidDataSource的监控数据 --><!-- <property name="useGlobalDataSourceStat" value="true" /> --> <!-- 定制化日志输出 --><property name="statLogger" ref="myStatLogger" /></bean><bean id="myStatLogger" class="com.taoguba.monitor.sql.impl.MonitorDataSourceSqlImpl"></bean>

5、通过Controller获取数据并返回JSON给前端

@RestController
@RequestMapping(value = "/new/monitor")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class MonitorController extends BaseController{@Autowiredprotected IRedisService cacheService;@GetMapping(value = "/getDataSourceSql")public Result getDataSourceSql() {String data = this.cacheService.get(RedisKey.MONITORLOG);MonitorInfo mon= JSONObject.parseObject(data,MonitorInfo.class);return new Result(mon);}
}

6、JSON数据,前端取到这些数据展示即可(此处暂不展示效果了。。)

{
status: true,
errorCode: 0,
errorMessage: "",
dto: {
sysIdentify: null,
logInfos: [
{
sql: "select count(*) from topi where beFg='Y' and usefulnum > 0 and catD between 2 and 3 and uid=?",
sqlParam: null,
maxParallel: 2,
executeMillisTotal: 3,
executeCount: 2,
executeErrorCount: 0,
maxOccurTime: 1505988865405,
histogram_0_1: 0,
histogram_1_10: 2,
histogram_100_1000: 0,
histogram_1000_10000: 0,
histogram_10000_100000: 0,
histogram_100000_1000000: 0,
histogram_1000000_more: 0,
lastError: null,
lastErrorMessage: null,
lastErrorStackTrace: null,
lastErrorClass: null,
lastErrorTime: null,
histogram_10_100: 0,
executeMillisMax: 1
}]
},
_t: 1505988871072

7、至此微服务项目集成Druid 监控数据 完成收集展示,如有问题 ,欢迎留言指正。

转载于:https://my.oschina.net/dyl226/blog/1541325

Spring/Spring Boot微服务项目 集成Druid 实现监控功能相关推荐

  1. 高级版的 jvisualvm :Spring Boot Admin 监控 Spring Boot 微服务项目

    前奏:先说一下 Java VisualVM Java VisualVM 是一个能够监控 JVM 的 jdk 自带的图形化工具: 在 $JAVA_HOME/bin 目录下,可直接运行它. 要想监控远程服 ...

  2. Spring Cloud Alibaba微服务项目中集成Redis实现分布式事务锁实践

    引言 我们知道同一个进程里面为了解决资源共享而不出现高并发的问题可以通过高并发编程解决,通过给变量添加volatile关键字实现线程间变量可见:通过synchronized关键字修饰代码块.对象或者方 ...

  3. 使用Spring Cloud搭建微服务项目

    什么是Spring Clould Spring Cloud是由Spring提供的一套能够快速搭建微服务架构程序的框架集 框架集表示Spring Cloud不是一个框架,而是很多框架的统称 Spring ...

  4. spring boot微服务项目搭建

    第一章 SpringBoot介绍 1 简介 Spring Boot是一个便捷搭建基于spring工程的脚手架:作用是帮助开发人员快速搭建大型的spring 项目.简化工程的配置和依赖管理:开发人员把时 ...

  5. Spring Boot微服务项目启动错误: 找不到或无法加载主类解决方案

    这个问题其实不考察技术含量,只是看思路和细心程度.简单记录下,希望能帮助一个是一个. 解决方法一:maven clean install方式 可以找到具体服务模块的maven工具如下操作,或者直接执行 ...

  6. 微服务集成cas_Spring Boot + Solr 全文检索微服务简易集成

    点击上方 Java后端,选择 设为星标 优质文章,及时送达 作者:JasonGofen 链接:https://www.jianshu.com/p/95869ade37b3 本文内容主要讲解Solr 7 ...

  7. 《深入理解 Spring Cloud 与微服务构建》第十二章 服务注册和发现 Consul

    <深入理解 Spring Cloud 与微服务构建>第十二章 服务注册和发现 Consul 文章目录 <深入理解 Spring Cloud 与微服务构建>第十二章 服务注册和发 ...

  8. 从0到1手把手搭建spring cloud alibaba 微服务大型应用框架(十五) swagger篇 : gateway 集成swagger 与 knife4j实现在线api文档并嵌入到自己项目内

    背景 我们日常开发中基本都是协同开发的,当然极个别的项目整体前后端都是一个人开发的,当多人协作时,尤其是前后端人员协同开发时 必然会面临着前端需要了解后端api接口的情况,两个选择,提前设计好文档,然 ...

  9. 基于Spring Boot和Spring Cloud实现微服务架构学习--转

    原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...

最新文章

  1. 功能安全-26262(2018) part5
  2. Oracle-知识结构漫谈
  3. Java学习笔记(一)--JDK环境
  4. 衡量 mysql性能状态 参数 详解
  5. Python学习15 正则表达式1
  6. codeforces 136A-C语言解题报告
  7. oracle 重复的记录数,如何确定Oracle数据库表中重复的记录
  8. Iterator(迭代器)
  9. 如何用HTML编写长方体框架,直角梯形长方体形状_html_开发99编程知识库
  10. 3月国内网站流量:腾讯夺第二 360安全中心降至第三
  11. 卧槽!火爆GitHub的算法电子书开放下载了!
  12. 趣图:IT 项目的时间估算
  13. 怎么判断间隙过渡过盈配合_圆柱销有2个标准,选型注意材料和热处理,特别是销孔的配合关系...
  14. 洋葱细胞数字全息显微实验演示
  15. RPG Maker MZ如何导入dlc素材?
  16. html页面如何设置背景,html怎么设置背景
  17. 累计独立访客(UV)不低于 1000是什么意思?如何查看自己小程序的UV数量?
  18. php正则表达式. 123,php正则表达式 - 路人甲123的个人页面 - OSCHINA - 中文开源技术交流社区...
  19. 《清平乐词》三篇——李白
  20. 大型多商户商城系统-功能表

热门文章

  1. chrome 插件 vimium 介绍
  2. MIT自然语言处理第一讲:简介和概述(第一部分)
  3. 程序员如何明智地提出好的问题
  4. 信息系统项目管理师-成本管理知识点
  5. Linux中iptables的用法
  6. 开始学习马哥的linux
  7. 设计模式[20]-Builder
  8. 用dedecms自定义表单创建简易自助预约系统
  9. 客户端,服务器,天气预报
  10. 2010最后一篇:使用PyQt4开发的一个开源小程序QaoBa