欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

cat实时监控-入门demo

程序员文章站 2024-01-07 20:53:10
...

昨天已经搭建好了cat服务端,今天我们来看怎么在一个ssm项目中去用cat(一个小小的demo)

1.背景:

CAT(Central Application Tracking)是由吴其敏(前大众点评首席架构师,现携程架构负责人)主导设计基于Java开发打造的实时应用监控平台,为大众点评网提供了全面的监控服务和决策支持。AT作为大众点评网基础监控组件,它已经在中间件框架(MVC框架,RPC框架,数据库框架,缓存框架等)中得到广泛应用,为点评各业务线提供系统的性能指标、健康状况、基础告警等。


2.为什么要用cat实时监控

  • 线上发布了服务,怎么知道它一切正常,比如发布5台服务器,如何直观了解是否有请求进来,访问一切正常。
  • 当年有一次将线上的库配置到了Beta,这么低级的错误,排错花了一个通宵,十几个人。
  • 某个核心服务挂了,导致大量报错,如何确定到底是哪里出了问题。
  • SOA带来的问题,调用XX服务出问题,很慢,是否可以衡量?
  • 应用程序有性能瓶颈,如何提供一些有效工具发现?


3.demo

cat实时监控-入门demo

cat实时监控-入门demo

cat实时监控-入门demo


demo下载:

cat实时监控-入门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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://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 -->
	<bean
		class="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下建立如下文件:

cat实时监控-入门demo

app.properties:

app.name=demo

client.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>();

	@Override
	public 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;
	}

	@Override
	public 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);
	}

	@Override
	public 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盘,

cat实时监控-入门demo

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>

相关标签: ssm中使用cat

上一篇:

下一篇: