JAVA全系列 教程
3762个小节阅读:7088.5k
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
SkyWalking是一个开源的分布式系统追踪和性能监控工具。除了提供实时的系统性能监控和分析功能外,SkyWalking还提供了告警功能,用于监控系统的指标数据,并在数据超过预设阈值时触发告警。
Skywalking默认支持7中通知:
web、grpc、微信、钉钉、飞书、华为weLink、slack
xxxxxxxxxx
#告警配置文件: alarm-settings.yml
service_response_time_rule
#指定的规则
metrics-name service_resp_time
op">"
# 阈值
threshold 1 # 单位毫秒
# 多久检查一次当前的指标数据是否符合告警规则
period5
# 达到多少次告警后,发送告警消息
count1
# 告警消息内容
message 服务 name 最近5分钟以内响应时间超过了1ms
1.下列不属于Skywalking告警通知的是____。
A 钉钉
B 邮件
C 微信
D 抖音
2.Skywalking发送告警的基本原理是每隔一段时间_____Skywalking-collector收集到的链路追踪的数据。
A 轮询
B 定时
C 记录
D 以上都是错误
1=>D 2=>A
xxxxxxxxxx
rules
# Rule unique name, must be ended with `_rule`.
service_resp_time_rule
metrics-name service_resp_time
op">"
threshold1000
period10
count3
silence-period5
message Response time of service name is more than 1000ms in 3 minutes of last 10 minutes.
service_sla_rule
# Metrics value need to be long, double or int
metrics-name service_sla
op"<"
threshold8000
# The length of time to evaluate the metrics
period10
# How many times after the metrics match the condition, will trigger alarm
count2
# How many times of checks, the alarm keeps silence after alarm triggered, default as same as period.
silence-period3
message Successful rate of service name is lower than 80% in 2 minutes of last 10 minutes
service_p90_sla_rule
# Metrics value need to be long, double or int
metrics-name service_p90
op">"
threshold1000
period10
count3
silence-period5
message 90% response time of service name is more than 1000ms in 3 minutes of last 10 minutes
service_instance_resp_time_rule
metrics-name service_instance_resp_time
op">"
threshold1000
period10
count2
silence-period5
message Response time of service instance name is more than 1000ms in 2 minutes of last 10 minutes
# Active endpoint related metrics alarm will cost more memory than service and service instance metrics alarm.
# Because the number of endpoint is much more than service and instance.
#
# endpoint_avg_rule:
# metrics-name: endpoint_avg
# op: ">"
# threshold: 1000
# period: 10
# count: 2
# silence-period: 5
# message: Response time of endpoint {name} is more than 1000ms in 2 minutes of last 10 minutes
webhooks
# - http://127.0.0.1/notify/
# - http://127.0.0.1/go-wechat/
以上文件定义了默认的4种规则
- 最近3分钟内服务的平均响应时间超过1秒
- 最近2分钟服务成功率低于80%
- 最近3分钟90%服务响应时间超过1秒
- 最近2分钟内服务实例的平均响应时间超过1秒 规则中的参数属性如下
编写告警功能接口来进行测试。
xxxxxxxxxx
//每次调用睡眠1.5秒,模拟超时的报警
"/timeout") (
public String timeout(){
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "timeout";
}
修改Skywalking配置文件。vim alarm-settings.yml
xxxxxxxxxx
# vim config/alarm-settings.yml
service_response_time_rule
metrics-name service_resp_time
op">"
threshold1
period1
count1
message 服务 name 最近1分钟以内响应时间超过了1ms
温馨提示:
注意预警属性对其。
属性 | 含义 |
---|---|
metrics-name | oal脚本中的度量名称 |
threshold | 阈值,与metrics-name和下面的比较符号相匹配 |
op | 比较操作符,可以设定>,<,= |
period | 多久检查一次当前的指标数据是否符合告警规则,单位分钟 |
count | 达到多少次后,发送告警消息 |
silence-period | 在多久之内,忽略相同的告警消息 |
message | 告警消息内容 |
include-names | 本规则告警生效的服务列表 |
1.自定义Skywalking告警规则该count参数含义是____。
A 告警阈值
B 多久检查一次当前的指标数据是否符合告警规则
C 告警消息内容
D 达到多少次告警后,发送告警消息
2.自定义Skywalking告警规则该threshold参数含义是____。
A 告警阈值
B 多久检查一次当前的指标数据是否符合告警规则
C 告警消息内容
D 达到多少次告警后,发送告警消息
1=>B 2=>A
Webhok可以简单理解为是一种Web层面的回调机制。告警就是一个事件,当事件发生时Skywalking会主动调用一个配置好的接口,这个接口就是所谓的Webhook;
注意:
Skywalking的告警消息会通过借HTTP请求进行发送,请求方法为POST (Content-Type 为application/json。其JSON数据实基于List<org.apache.skywalking.oap.server.core.alarm.AlarmMessage>进行序列化的。
xxxxxxxxxx
[{
"scopeId": 1,
"scope": "SERVICE",
"name": "serviceA",
"id0": "12",
"id1": "",
"ruleName": "service_resp_time_rule",
"alarmMessage": "alarmMessage xxxx",
"startTime": 1560524171000
}]
xxxxxxxxxx
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
https://github.com/apache/skywalking/blob/v8.5.0/docs/en/setup/backend/backend-alarm.md
xxxxxxxxxx
import lombok.Data;
public class AlarmMessageDto {
private int scopeId;
private String scope;
private String name;
private String id0;
private String id1;
private String ruleName;
private String alarmMessage;
private List<Tag> tags;
private long startTime;
private transient int period;
private transient boolean onlyAsCondition;
public static class Tag{
private String key;
private String value;
}
}
xxxxxxxxxx
/**
* 订单机器人通知的
*/
"/alarm") (
public void sendDinding( List<AlarmMessageDto> alarmMessageDtoList) {
StringBuilder builder = new StringBuilder();
alarmMessageDtoList.forEach(info -> {
builder.append("\nscopeId:").append(info.getScopeId())
.append("\nScope实体:").append(info.getScope())
.append("\n告警消息:").append(info.getAlarmMessage())
.append("\n告警规则:").append(info.getRuleName())
.append("\n\n------------------------\n\n");
});
}
alarm-settings.yml 增加alarm接口
1.Webhok可以简单理解为是一种Web层面的_____机制。
A 回调
B 请求
C 告警
D 以上都是错误
1=>A
访问阿里云https://www.aliyun.com/,完成登录
进入短信服务
开通短信服务
购买短信条数
购买完成进入阿里云短信控制台https://dysms.console.aliyun.com/overview
绑定测试手机号
点击调用API发送短信,可以看到发送短信的JAVA代码。
申请阿里云秘钥,该秘钥在发送短信时会作为参数传入
xxxxxxxxxx
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
xxxxxxxxxx
/**
* 短信服务
*/
public interface MessageService {
/**
* 发送短信
* @param phoneNumber 手机号
* @param code 验证码
* @return 返回结果
*/
BaseResult sendMessage(String phoneNumber,String code);
}
xxxxxxxxxx
public class MessageServiceImpl implements MessageService {
"${message.accessKeyId}") (
private String accessKeyId;
"${message.accessKeySecret}") (
private String accessKeySecret;
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
private Client createClient(String accessKeyId, String accessKeySecret) {
Config config = new Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new Client(config);
}
public BaseResult sendMessage(String phoneNumber,String code) {
Client client = createClient(accessKeyId, accessKeySecret);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName("阿里云短信测试")
.setTemplateCode("SMS_154950909")
.setPhoneNumbers(phoneNumber)
.setTemplateParam("{\"code\":\""+code+"\"}");
RuntimeOptions runtime = new RuntimeOptions();
// 复制代码运行请自行打印API的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
SendSmsResponseBody body = sendSmsResponse.getBody();
if ("OK".equals(body.getCode())){
return new BaseResult(200,body.getCode(),body.getMessage());
}else{
return new BaseResult(500,body.getCode(),body.getMessage());
}
}
}
SMTP 协议全称为 Simple Mail Transfer Protocol,译作简单邮件传输协议,它定义了邮件客户端软件与 SMTP 服务器之间,以及 SMTP 服务器与 SMTP 服务器之间的通信规则。
所以在使用springboot发送邮件之前,要开启POP3和SMTP协议,需要获得邮件服务器的授权码,这里以qq邮箱为例,展示获取授权码的过程:pkzljvxjbzvgbeci
在账户的下面有一个开启SMTP协议的开关并进行密码验证:
成功后会出现
xxxxxxxxxx
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
xxxxxxxxxx
spring
mail
# 配置 SMTP 服务器地址
host smtp.qq.com
# 发送者邮箱
username 877910962@qq.com
# 配置密码,注意不是真正的密码,而是刚刚申请到的授权码
password pkzljvxjbzvgbeci
# 默认的邮件编码为UTF-8
default-encoding UTF-8
properties
mail
smtp
#需要验证用户名密码
authtrue
starttls
# 设置为配置SMTP连接的属性。要使用STARTTLS,必须设置以下属性
enabletrue
requiredtrue
注意:
- 126邮箱SMTP服务器地址:smtp.126.com,端口号:465或者994
- 163邮箱SMTP服务器地址:smtp.163.com,端口号:465或者994
- yeah邮箱SMTP服务器地址:smtp.yeah.net,端口号:465或者994
- qq邮箱SMTP服务器地址:smtp.qq.com,端口号465或587
xxxxxxxxxx
"sendMail") (
public void sendEmail( List<AlarmMessage> alarmMessages) {
alarmMessages.forEach(info->{
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
// 发件人
simpleMailMessage.setFrom("877910962@qq.com");
// 收件人
simpleMailMessage.setTo("877910962@qq.com");
// 邮件主题
simpleMailMessage.setSubject(info.getScope());
// 邮件内容
simpleMailMessage.setText(info.getAlarmMessage());
javaMailSender.send(simpleMailMessage);
});
}
1.简单的邮件协议_____。
A RPC
B HTTP
C TCP
D SMTP
1=>D