cat实时监控-入门demo
昨天已經(jīng)搭建好了cat服務(wù)端,今天我們來(lái)看怎么在一個(gè)ssm項(xiàng)目中去用cat(一個(gè)小小的demo)
1.背景:
CAT(Central Application Tracking)是由吳其敏(前大眾點(diǎn)評(píng)首席架構(gòu)師,現(xiàn)攜程架構(gòu)負(fù)責(zé)人)主導(dǎo)設(shè)計(jì)基于Java開(kāi)發(fā)打造的實(shí)時(shí)應(yīng)用監(jiān)控平臺(tái),為大眾點(diǎn)評(píng)網(wǎng)提供了全面的監(jiān)控服務(wù)和決策支持。AT作為大眾點(diǎn)評(píng)網(wǎng)基礎(chǔ)監(jiān)控組件,它已經(jīng)在中間件框架(MVC框架,RPC框架,數(shù)據(jù)庫(kù)框架,緩存框架等)中得到廣泛應(yīng)用,為點(diǎn)評(píng)各業(yè)務(wù)線提供系統(tǒng)的性能指標(biāo)、健康狀況、基礎(chǔ)告警等。
2.為什么要用cat實(shí)時(shí)監(jiān)控
- 線上發(fā)布了服務(wù),怎么知道它一切正常,比如發(fā)布5臺(tái)服務(wù)器,如何直觀了解是否有請(qǐng)求進(jìn)來(lái),訪問(wèn)一切正常。
- 當(dāng)年有一次將線上的庫(kù)配置到了Beta,這么低級(jí)的錯(cuò)誤,排錯(cuò)花了一個(gè)通宵,十幾個(gè)人。
- 某個(gè)核心服務(wù)掛了,導(dǎo)致大量報(bào)錯(cuò),如何確定到底是哪里出了問(wèn)題。
- SOA帶來(lái)的問(wèn)題,調(diào)用XX服務(wù)出問(wèn)題,很慢,是否可以衡量?
- 應(yīng)用程序有性能瓶頸,如何提供一些有效工具發(fā)現(xiàn)?
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>寫(xiě)攔截器:
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() : "無(wú)";Transaction t = Cat.newTransaction("View", viewName);pageLocal.set(t);}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {// 請(qǐng)求-頁(yè)面渲染前Transaction pt = pageLocal.get();pt.setStatus(Transaction.SUCCESS);pt.complete();// 總計(jì)Transaction t = tranLocal.get();t.setStatus(Transaction.SUCCESS);t.complete();}}在業(yè)務(wù)邏輯中添加監(jiān)控代碼:
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";} }注意:在項(xiàng)目根目錄下創(chuàng)建如下文件
比如我idea項(xiàng)目在F盤(pán),
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>總結(jié)
以上是生活随笔為你收集整理的cat实时监控-入门demo的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CF1527C Sequence Pai
- 下一篇: 六脚自锁开关引脚图及功能定义