共计 9237 个字符,预计需要花费 24 分钟才能阅读完成。
SkyWalking 是一个利用性能监控零碎,特地为微服务、云原生和基于容器(Docker, Kubernetes, Mesos)体系结构而设计。除了利用指标监控以外,它还能对分布式调用链路进行追踪。相似性能的组件还有:Zipkin、Pinpoint、CAT 等。
上几张图,看看成果,而后再一步一步搭建并应用:
概念与架构
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 容许用户理解服务和端点之间的拓扑关系,查看每个服务 / 服务实例 / 端点的度量,并设置警报规定。
架构
SkyWalking 逻辑上分为四个局部:Probes(探针), Platform backend(平台后端), Storage(存储), UI。
这个构造就很清晰了,探针就是 Agent 负责采集数据并上报给服务端,服务端对数据进行解决和存储,UI 负责展现。
下载与装置
SkyWalking 有两中版本,ES 版本和非 ES 版。如果咱们决定采纳 ElasticSearch 作为存储,那么就下载 es 版本。
- https://skywalking.apache.org…
- https://archive.apache.org/di…
- 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/sky…
装置 ElasticSearch
https://www.elastic.co/guide/…
# 启动
./bin/elasticsearch -d -p pid
# 进行
pkill -F pid
ElasticSearch7.x 须要 Java 11 以上的版本,然而如果你设置了环境变量 JAVA_HOME 的话,它会用你本人的 Java 版本
通常,启动过程中会报以下三个谬误:
[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
解决办法:在 /etc/security/limits.conf 文件中追加以下内容:
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
可通过以下四个命令查看批改后果:
ulimit -Hn
ulimit -Sn
ulimit -Hu
ulimit -Su
批改 /etc/sysctl.conf 文件,追加以下内容:
vm.max_map_count=262144
批改 es 配置文件 elasticsearch.yml 勾销正文,保留一个节点
cluster.initial_master_nodes: ["node-1"]
为了可能 ip:port 形式拜访,还需批改网络配置
network.host: 0.0.0.0
批改完是这样的:
至此,ElasticSearch 算是启动胜利了,一个节点还不够,这里用三个节点搭建一个集群
- 192.168.100.14 config/elasticsearch.yml
cluster.name: my-monitor
node.name: node-1
network.host: 192.168.100.14
http.port: 9200
discovery.seed_hosts: ["192.168.100.14:9300", "192.168.100.15:9300", "192.168.100.19:9300"]
cluster.initial_master_nodes: ["node-1"]
192.168.100.15 config/elasticsearch.yml
cluster.name: my-monitor
node.name: node-2
network.host: 192.168.100.15
http.port: 9200
discovery.seed_hosts: ["192.168.100.14:9300", "192.168.100.15:9300", "192.168.100.19:9300"]
cluster.initial_master_nodes: ["node-1"]
192.168.100.19 config/elasticsearch.yml
cluster.name: my-monitor
node.name: node-3
network.host: 192.168.100.19
http.port: 9200
discovery.seed_hosts: ["192.168.100.14:9300", "192.168.100.15:9300", "192.168.100.19:9300"]
cluster.initial_master_nodes: ["node-1"]
同时,倡议批改三个节点 config/jvm.options
-Xms2g
-Xmx2g
顺次启动三个节点
pkill -F pid
./bin/elasticsearch -d -p pid
接下来,批改 skywalking 下 config/application.yml 中配置 es 地址即可
storage:
selector: ${SW_STORAGE:elasticsearch7}
elasticsearch7:
nameSpace: ${SW_NAMESPACE:""}
clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:192.168.100.14:9200,192.168.100.15:9200,192.168.100.19:9200}
装置 Agent
https://github.com/apache/sky…
将 agent 目录拷贝至各服务所在的机器上
scp -r ./agent chengjs@192.168.100.12:~/
这里,我将它拷贝至各个服务目录下
plugins 是探针用到各种插件,SkyWalking 插件都是即插即用的,能够把 optional-plugins 中的插件放到 plugins 中,批改 agent/config/agent.config 配置文件,也能够通过命令行参数指定。
次要是配置服务名称和后端服务地址
agent.service_name=${SW_AGENT_NAME:user-center}
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:192.168.100.17:11800}
当然,也能够通过环境变量或零碎属性的形式来设置,例如:
export SW_AGENT_COLLECTOR_BACKEND_SERVICES=127.0.0.1:11800
最初,在服务启动的时候用命令行参数 -javaagent 来指定探针
java -javaagent:/path/to/skywalking-agent/skywalking-agent.jar -jar yourApp.jar
例如:
java -javaagent:./agent/skywalking-agent.jar -Dspring.profiles.active=dev -Xms512m -Xmx1024m -jar demo-0.0.1-SNAPSHOT.jar
启动服务
批改 webapp/webapp.yml 文件,更改端口号及后端服务地址
server:
port: 9000
collector:
path: /graphql
ribbon:
ReadTimeout: 10000
# Point to all backend's restHost:restPort, split by ,
listOfServers: 127.0.0.1:12800
启动服务
bin/startup.sh
或者别离顺次启动
bin/oapService.sh
bin/webappService.sh
查看 logs 目录下的日志文件,看是否启动胜利,浏览器拜访 http://127.0.0.1:9000。
告警
编辑 alarm-settings.yml 设置告警规定和告诉
https://github.com/apache/skywalking/blob/v8.2.0/docs/en/setup/backend/backend-alarm.md
重点说下告警告诉
为了应用钉钉机器人告诉,接下来,新建一个我的项目
<?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>
可选依赖(不倡议引入)
<dependency
<groupId>org.apache.skywalking</groupId>
<artifactId>server-core</artifactId>
<version>8.2.0</version>
</dependency>
定义告警音讯实体类
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 + "×tamp=" + 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())));
}
}
}
作者:废物大师兄
原文:https://www.cnblogs.com/cjsbl…