cat实时监控-入门demo
生活随笔
收集整理的這篇文章主要介紹了
cat实时监控-入门demo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
昨天已經搭建好了cat服務端,今天我們來看怎么在一個ssm項目中去用cat(一個小小的demo)
1.背景:
CAT(Central Application Tracking)是由吳其敏(前大眾點評首席架構師,現攜程架構負責人)主導設計基于Java開發打造的實時應用監控平臺,為大眾點評網提供了全面的監控服務和決策支持。AT作為大眾點評網基礎監控組件,它已經在中間件框架(MVC框架,RPC框架,數據庫框架,緩存框架等)中得到廣泛應用,為點評各業務線提供系統的性能指標、健康狀況、基礎告警等。
2.為什么要用cat實時監控
- 線上發布了服務,怎么知道它一切正常,比如發布5臺服務器,如何直觀了解是否有請求進來,訪問一切正常。
- 當年有一次將線上的庫配置到了Beta,這么低級的錯誤,排錯花了一個通宵,十幾個人。
- 某個核心服務掛了,導致大量報錯,如何確定到底是哪里出了問題。
- SOA帶來的問題,調用XX服務出問題,很慢,是否可以衡量?
- 應用程序有性能瓶頸,如何提供一些有效工具發現?
3.demo
demo下載:
https://download.csdn.net/download/m0_37499059/10375430
核心代碼如下:
pom.xml
<?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><groupId>com.cxx.demo</groupId><artifactId>cat</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>com.dianping.cat</groupId><artifactId>cat-core</artifactId><version>2.0.0</version></dependency><dependency><groupId>com.dianping.cat</groupId><artifactId>cat-client</artifactId><version>2.0.0</version></dependency><dependency><groupId>com.dianping.cat</groupId><artifactId>cat-consumer</artifactId><version>2.0.0</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.14</version></dependency><!--spring mvc--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.0.9.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.0.9.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.0.9.RELEASE</version></dependency><!--jsp--><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version><type>jar</type></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version><scope>provided</scope></dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-io</artifactId><version>1.3.2</version></dependency></dependencies><build><plugins><!--配置tomcat插件--><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat6-maven-plugin</artifactId><configuration><path>/</path><port>8081</port></configuration></plugin></plugins></build> </project>spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><!-- scan the package and the sub package --><context:component-scan base-package="com.k12ct.demo" /><!-- don't handle the static resource --><mvc:default-servlet-handler /><!-- if you use annotation you must configure following setting --><mvc:annotation-driven /><!-- configure the InternalResourceViewResolver --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!-- 前綴 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 后綴 --><property name="suffix" value=".jsp" /></bean><!-- 配置攔截器 --><mvc:interceptors><mvc:interceptor><mvc:mapping path="/**" /><bean class="com.k12ct.demo.CatInterceptor"></bean></mvc:interceptor></mvc:interceptors> </beans>web.xml
<?xml version="1.0"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><display-name>class-service</display-name><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><!-- <load-on-startup>1</load-on-startup> --></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app>在resources下建立如下文件:
app.properties:
app.name=democlient.xml
<config mode="client"><domain id="demo" /> </config>寫攔截器:
package com.k12ct.demo;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;import com.dianping.cat.Cat; import com.dianping.cat.message.Message; import com.dianping.cat.message.Transaction;public class CatInterceptor implements HandlerInterceptor {private ThreadLocal<Transaction> tranLocal = new ThreadLocal<Transaction>();private ThreadLocal<Transaction> pageLocal = new ThreadLocal<Transaction>();@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {String uri = request.getRequestURI();Transaction t = Cat.newTransaction("URL", uri);Cat.logEvent("URL.Method", request.getMethod(), Message.SUCCESS, request.getRequestURL().toString());Cat.logEvent("URL.Host", request.getMethod(), Message.SUCCESS, request.getRemoteHost());tranLocal.set(t);return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {String viewName = modelAndView != null ? modelAndView.getViewName() : "無";Transaction t = Cat.newTransaction("View", viewName);pageLocal.set(t);}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {// 請求-頁面渲染前Transaction pt = pageLocal.get();pt.setStatus(Transaction.SUCCESS);pt.complete();// 總計Transaction t = tranLocal.get();t.setStatus(Transaction.SUCCESS);t.complete();}}在業務邏輯中添加監控代碼:
package com.k12ct.demo;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;import com.dianping.cat.Cat; import com.dianping.cat.message.Event; import com.dianping.cat.message.Transaction; @Controller @RequestMapping("/mvc") public class HelloController {@RequestMapping("/hello")public String hello(){ String pageName = "helloworld";String serverIp = "localhost";double amount = 0;Transaction t = Cat.newTransaction("URL", pageName);try {Cat.logEvent("URL.Server", serverIp, Event.SUCCESS, "ip="+ serverIp + "&...");Cat.logMetricForCount("PayCount");Cat.logMetricForSum("PayAmont", amount);t.setStatus(Transaction.SUCCESS);} catch (Exception e) {e.printStackTrace();t.setStatus(e);} finally {t.complete();}return "hello";} }注意:在項目根目錄下創建如下文件
比如我idea項目在F盤,
client.xml
<?xml version="1.0" encoding="utf-8"?><config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd"><servers><!-- Local mode for development --><server ip="127.0.0.1" port="2280" http-port="2280" /><!-- If under production environment, put actual server address as list. --><!-- <server ip="192.168.7.71" port="2280" /> <server ip="192.168.7.72" port="2280" /> --></servers> </config>總結
以上是生活随笔為你收集整理的cat实时监控-入门demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CF1527C Sequence Pai
- 下一篇: 六脚自锁开关引脚图及功能定义