天天看點

最牛逼的性能監控系統,SkyWalking 集強大于一身!

SkyWalking 是一個應用性能監控系統,特别為微服務、雲原生和基于容器(Docker, Kubernetes, Mesos)體系結構而設計。

除了應用名額監控以外,它還能對分布式調用鍊路進行追蹤。類似功能的元件還有:Zipkin、Pinpoint、CAT等。

上幾張圖,看看效果,然後再一步一步搭建并使用。

最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!

1、概念與架構

SkyWalking是一個開源監控平台,用于從服務和雲原生基礎設施收集、分析、聚合和可視化資料。SkyWalking提供了一種簡單的方法來維護分布式系統的清晰視圖,甚至可以跨雲檢視。它是一種現代APM,專門為雲原生、基于容器的分布式系統設計。

SkyWalking從三個次元對應用進行監視:service(服務), service instance(執行個體), endpoint(端點)

服務和執行個體就不多說了,端點是服務中的某個路徑或者說URI

SkyWalking allows users to understand the topology relationship between Services and Endpoints, to view the metrics of every Service/Service Instance/Endpoint and to set alarm rules.

SkyWalking允許使用者了解服務和端點之間的拓撲關系,檢視每個服務/服務執行個體/端點的度量,并設定警報規則。

1.1. 架構

最牛逼的性能監控系統,SkyWalking 集強大于一身!

2、下載下傳與安裝

SkyWalking有兩中版本,ES版本和非ES版。

如果我們決定采用ElasticSearch作為存儲,那麼就下載下傳es版本。

https://skywalking.apache.org/downloads/\ https://archive.apache.org/dist/skywalking/
最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!

agent目錄将來要拷貝到各服務所在機器上用作探針

bin目錄是服務啟動腳本

config目錄是配置檔案

oap-libs目錄是oap服務運作所需的jar包

webapp目錄是web服務運作所需的jar包

接下來,要選擇存儲了,支援的存儲有:

H2

ElasticSearch 6, 7

MySQL

TiDB

InfluxDB

作為監控系統,首先排除H2和MySQL,這裡推薦InfluxDB,它本身就是時序資料庫,非常适合這種場景

但是InfluxDB我不是很熟悉,是以這裡先用ElasticSearch7

https://github.com/apache/skywalking/blob/master/docs/en/setup/backend/backend-storage.md 2.1. 安裝ElasticSearch https://www.elastic.co/guide/en/elasticsearch/reference/7.10/targz.html
最牛逼的性能監控系統,SkyWalking 集強大于一身!

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[3]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured      
最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!
java -javaagent:./agent/skywalking-agent.jar -Dspring.profiles.active=dev -Xms512m -Xmx1024m -jar demo-0.0.1-SNAPSHOT.jar      
最牛逼的性能監控系統,SkyWalking 集強大于一身!
最牛逼的性能監控系統,SkyWalking 集強大于一身!

為了使用釘釘機器人通知,接下來,建立一個項目:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wt.monitor</groupId>
    <artifactId>skywalking-alarm</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>skywalking-alarm</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>1.0.1</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>      

Spring Boot 基礎就不介紹了,推薦看下這個教程:

https://github.com/javastacks/spring-boot-best-practice

可選依賴(不建議引入)

最牛逼的性能監控系統,SkyWalking 集強大于一身!
package com.wt.monitor.skywalking.alarm.domain;

import lombok.Data;

import java.io.Serializable;

/**
 * @author ChengJianSheng
 * @date 2020/12/1
 */
@Data
public class AlarmMessageDTO implements Serializable {

    private int scopeId;

    private String scope;

    /**
     * Target scope entity name
     */
    private String name;

    private String id0;

    private String id1;

    private String ruleName;

    /**
     * Alarm text message
     */
    private String alarmMessage;

    /**
     * Alarm time measured in milliseconds
     */
    private long startTime;

}      

發送釘釘機器人消息:

package com.wt.monitor.skywalking.alarm.service;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
 * @author ChengJianSheng
 * @data 2020/12/1
 */
@Slf4j
@Service
public class DingTalkAlarmService {

    @Value("${dingtalk.webhook}")
    private String webhook;
    @Value("${dingtalk.secret}")
    private String secret;

    public void sendMessage(String content) {
        try {
            Long timestamp = System.currentTimeMillis();
            String stringToSign = timestamp + "\n" + secret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");

            String serverUrl = webhook + "&timestamp=" + timestamp + "&sign=" + sign;
            DingTalkClient client = new DefaultDingTalkClient(serverUrl);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent(content);
            request.setText(text);

            client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }
    }
}      

AlarmController.java

package com.wt.monitor.skywalking.alarm.controller;

import com.alibaba.fastjson.JSON;
import com.wt.monitor.skywalking.alarm.domain.AlarmMessageDTO;
import com.wt.monitor.skywalking.alarm.service.DingTalkAlarmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.MessageFormat;
import java.util.List;

/**
 * @author ChengJianSheng
 * @date 2020/12/1
 */
@Slf4j
@RestController
@RequestMapping("/skywalking")
public class AlarmController {

    @Autowired
    private DingTalkAlarmService dingTalkAlarmService;

    @PostMapping("/alarm")
    public void alarm(@RequestBody List<AlarmMessageDTO> alarmMessageDTOList) {
       log.info("收到告警資訊: {}", JSON.toJSONString(alarmMessageDTOList));
       if (null != alarmMessageDTOList) {
           alarmMessageDTOList.forEach(e->dingTalkAlarmService.sendMessage(MessageFormat.format("-----來自SkyWalking的告警-----\n【名稱】: {0}\n【消息】: {1}\n", e.getName(), e.getAlarmMessage())));
       }
    }
}      
最牛逼的性能監控系統,SkyWalking 集強大于一身!

參考文檔:

https://skywalking.apache.org/\ https://skywalking.apache.org/zh/\ https://github.com/apache/skywalking/tree/v8.2.0/docs\ https://archive.apache.org/dist/\ https://www.elastic.co/guide/en/elasticsearch/reference/master/index.html https://www.elastic.co/guide/en/elasticsearch/reference/7.10/modules-discovery-bootstrap-cluster.html\ https://www.elastic.co/guide/en/elasticsearch/reference/7.10/modules-discovery-hosts-providers.html

最後,感謝閱讀~ 近期熱文推薦:

1.Java 15 正式釋出, 14 個新特性,重新整理你的認知!!

2.終于靠開源項目弄到 IntelliJ IDEA 激活碼了,真香!

3.我用 Java 8 寫了一段邏輯,同僚直呼看不懂,你試試看。。

4.吊打 Tomcat ,Undertow 性能很炸!!

5.《Java開發手冊(嵩山版)》最新釋出,速速下載下傳!

覺得不錯,别忘了随手點贊+轉發哦!