Introduction to the

CAT is a real-time and nearly full monitoring system, which focuses on the monitoring of Java applications and basically connects to all the core applications of Meituan Shanghai. At present, it is widely used in middleware (MVC, RPC, database, cache, etc.) framework, providing system performance indicators, health status, monitoring alarms, etc., for each business line of Meituan.

advantage

  • Real-time processing: The value of information diminishes over time, especially during incident processing.
  • Full data: Collects full indicator data for in-depth analysis of fault cases.
  • High availability: Fault recovery and fault location require high availability monitoring.
  • Fault tolerance: A fault does not affect the normal running of services and is transparent to services.
  • High throughput: The collection of massive monitoring data requires high throughput capacity.
  • Scalable: supports distributed, cross-IDC deployment and horizontal expansion of the monitoring system.

Open Source product comparison

Quick learning

Local deployment

Step 1: Deploy Tomcat

Prepare a tomcat and modify server. XML in the Tomcat conf directory to prevent Garbled Characters in Chinese.

Connector port="8080" protocol="HTTP/1.1" URIEncoding=" UTF-8 "connectionTimeout="20000" redirectPort="8443" /><! URIEncoding=" utF-8 "-->
Copy the code

Step 2: Program for /data/ directory specific read and write permissions (critical)

  • Linux

  • The /data/ directory can be read and written. If the /data/ directory cannot be written, you are advised to use the Linux soft link to connect it to a fixed directory that can be written.

  • This directory holds the necessary configuration files for CAT and the runtime data store directory.

  • CAT supports the CAT_HOME environment variable, and the default path can be modified using JVM parameters.

mkdir /data
chmod -R 777 /data/
Copy the code
  • Windows

You can read and write /data/ Appdatas /cat and /data/applogs/cat on the program running disk. For example, if the CAT service runs in Tomcat on disk E, you must have read and write permissions on E :/data/ AppDatas /cat and E :/data/applogs/cat.

Step 3: configure the/data/appdatas/cat/client. The XML ($CAT_HOME/client. The XML)

<?xml version="1.0" encoding="utf-8"? >
<config mode="client">
    <servers>
        <server ip="127.0.0.1" port="2280" http-port="8080"/>
    </servers>
</config>
Copy the code

The effect of this configuration file is that all clients need an address pointing to the CAT server.

Step 4: Install the CAT database

Download the source code package: cat codeload.github.com/dianping/ca… After decompression, database script file for the script/CatApplication SQL

mysql -uroot -Dcat < CatApplication.sql
Copy the code

Step 5: configure/data/appdatas/cat/datasources. XML ($CAT_HOME/datasources. XML)

<?xml version="1.0" encoding="utf-8"? >

<data-sources>
	<data-source id="cat">
		<maximum-pool-size>3</maximum-pool-size>
		<connection-timeout>1s</connection-timeout>
		<idle-timeout>10m</idle-timeout>
		<statement-cache-size>1000</statement-cache-size>
		<properties>
			<driver>com.mysql.jdbc.Driver</driver>
			<url><! [CDATA [JDBC: mysql: / / 127.0.0.1:3306 / cat]] ></url>  <! -- Please replace with real database URL and Port -->
			<user>root</user>  <! -- Replace with real database user name -->
			<password>root</password>  <! -- Please replace with real database password -->
			<connectionProperties><! [CDATA[useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&socketTimeout=120000]]></connectionProperties>
		</properties>
	</data-source>
</data-sources>
Copy the code

Step 6: War packaging

Official download: unidal.org/nexus/servi…

Change the name to cat.war for deployment. Note that this war uses JDK8

Step 7: WAR deployment

  • Deploy the WAR package packaged in the previous step to the local Tomcat Webapps.
  • Start tomcat, open a console URL, http://127.0.0.1:8080/cat/s/config? Op =routerConfigUpdate Default user name: admin Default password: admin.
  • Configure client routing.
<?xml version="1.0" encoding="utf-8"? >
<router-config backup-server="Your native IP (do not use 127.0.0.1)" backup-server-port="2280">
   <default-server id="Your native IP (do not use 127.0.0.1)" weight="1.0" port="2280" enable="true"/>
   <network-policy id="default" title="Default" block="false" server-group="default_group">
   </network-policy>
   <server-group id="default_group" title="default-group">
      <group-server id="Your native IP (do not use 127.0.0.1)"/>
   </server-group>
   <domain id="cat">
      <group id="default">
         <server id="Your native IP (do not use 127.0.0.1)" port="2280" weight="1.0"/>
      </group>
   </domain>
</router-config>
Copy the code

Submitted after the restart tomcat, visit http://127.0.0.1:8080/cat, appear the following interface, set up successfully.

test

Install the jar package

Go to the lib/ Java /jar directory of the CAT source package and install the cat-client-3.0.0.jar package to the local Maven repository.

MVN install: install-file-dgroupid =com.dianping. cat-dartifactid =cat-client Dversion= 3.0.0-dpackaging =jar - Dfile = cat - the client - 3.0.0. JarCopy the code

Create a project

To create a Springboot project, the key code is as follows.

pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.dianping.cat</groupId>
    <artifactId>cat-client</artifactId>
    <version>3.0.0</version>
</dependency>
Copy the code

app.properties

Create a SRC /main/resources/ meta-INF /app.properties file in your project and add the following:

app.name={appkey}
Copy the code

The appKey contains only letters, digits, underscores (_), and hyphens (-).

application.yml

server:
  port: 8760

spring:
  application:
    name: cat-simple
Copy the code

Start the class

@SpringBootApplication
@RestController
public class CatSimpleApplication {


    public static void main(String[] args) {
        SpringApplication.run( CatSimpleApplication.class, args );
    }

    @PostMapping("/hi")
    public String hi(HttpServletRequest request){
        String url = request.getRequestURL().toString();

        // Create a Transaction
        Transaction transaction = Cat.newTransaction( "URL", url );
        try {
            // Handle business
            myBusiness();
            // Set the state
            transaction.setStatus(Transaction.SUCCESS);
        } catch (Exception e) {
            // Set the error status
            transaction.setStatus(e);
            // Record error information
            Cat.logError(e);
        } finally {
            / / end the Transaction
            transaction.complete();
        }

        return "hello";
    }

    @PostMapping("/error")
    public String error(HttpServletRequest request){
        String url = request.getRequestURL().toString();

        // Create a Transaction
        Transaction transaction = Cat.newTransaction( "URL", url );
        try {
            // Handle business
            int i = 1 / 0;
            // Set the state
            transaction.setStatus(Transaction.SUCCESS);
        } catch (Exception e) {
            // Set the error status
            transaction.setStatus(e);
            // Record error information
            Cat.logError(e);
        } finally {
            / / end the Transaction
            transaction.complete();
        }

        return "500";
    }

    private void myBusiness(a) {
        // Simulate the time of business processing
        try {
            Thread.sleep( 500 );
        } catch(InterruptedException e) { e.printStackTrace(); }}}Copy the code

Request to http://localhost:8760/hi

curl -X POST  http://localhost:8760/hi
Copy the code

Request to http://localhost:8760/error

curl -X POST  http://localhost:8760/error
Copy the code

Viewing Monitoring Information

Go to the CAT console, click the Transaction button, and then click All to see which clients are available, as shown below:

Click the client cat-simple, as shown in the figure below:

As shown in the figure above, you can clearly see the total number of requests (TATAL), the mean (AVG), the maximum/minimum (Max /min), and the standard deviation (STD), etc. The others are more intuitive. The standard deviation is a little more complicated, so you can deduce how to do the incremental calculation yourself. The set of operations, such as 95 lines (representing the completion time of 95% of requests), 999 lines (representing 99.9% of requests)

Click “Log View” to View error information, as shown in the picture:

The source code

Github.com/gf-huanchup…

reference

Github.com/dianping/ca…

Please scan the code or search the wechat public number “Programmer Guoguo” to follow me, pay attention to surprise ~