SkyWalking 是一个应用性能监控系统,特别为微服务、云原生和基于容器(Docker, Kubernetes, Mesos)体系结构而设计。
除了应用指标监控以外,它还能对分布式调用链路进行追踪。类似功能的组件还有:Zipkin、Pinpoint、CAT等。
上几张图,看看效果,然后再一步一步搭建并使用。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. 架构
2、下载与安装
SkyWalking有两中版本,ES版本和非ES版。
如果我们决定采用ElasticSearch作为存储,那么就下载es版本。
https://skywalking.apache.org/downloads/\https://archive.apache.org/dist/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
[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
java -javaagent:./agent/skywalking-agent.jar -Dspring.profiles.active=dev -Xms512m -Xmx1024m -jar demo-0.0.1-SNAPSHOT.jar
为了使用钉钉机器人通知,接下来,新建一个项目:
<?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 基础就不介绍了,推荐看下这个教程:
可选依赖(不建议引入)
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://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开发手册(嵩山版)》最新发布,速速下载!
觉得不错,别忘了随手点赞+转发哦!