Skywalking 支持服务警告,即当服务调用异常的时候通知开发者。
准备工作
Spring Cloud Alibaba 2.X 简易集成 Skywalking 实现微服务链路追踪
配置文件路径
${skywalkingPath}/config/alarm-settings.yml
其中自带了一些服务警告的配置,包括服务响应时间、数据库响应时间等
webhooks:
# - http://127.0.0.1/notify/
# - http://127.0.0.1/go-wechat/
- http://127.0.0.1:8612/api/shop/user/notify
通知接口配置
当触发警告规则时,会向指定的接口发送请求,然后可以根据业务需要在接口中添加邮件、短信以及其他 IM 工具的通知。
通知接口
./cloud-alibaba-gateway-filter/src/main/java/com/ljq/demo/springboot/alibaba/gateway/filter/controller/DemoController.java
package com.ljq.demo.springboot.alibaba.gateway.filter.controller;
import cn.hutool.json.JSONUtil;
import com.ljq.demo.springboot.alibaba.gateway.filter.common.api.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* @Description: 测试接口控制层
* @Author: junqiang.lu
* @Date: 2021/10/22
*/
@Slf4j
@RestController
@RequestMapping(value = "/api/shop/user")
public class DemoController {
/**
* 通知
*
* @param obj
* @return
*/
@PostMapping(value = "/notify")
public ResponseEntity<?> notify(@RequestBody Object obj) {
log.warn("/api/user/notify, request param: {}", JSONUtil.toJsonStr(obj));
return ResponseEntity.ok("Skywalking warning notify");
}
}
根据警告规则,其中有一条是在 3-10 分钟内平均服务响应时间超过 1000ms 即触发警告通知
为了方便测试,在服务提供者的接口中添加一定的延时
./cloud-alibaba-server-provider/src/main/java/com/ljq/demo/pringboot/alibaba/server/provider/controller/NacosProviderController.java
package com.ljq.demo.pringboot.alibaba.server.provider.controller;
import com.ljq.demo.pringboot.alibaba.server.provider.model.param.HelloParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* @Description: nacos 服务提供者控制层
* @Author: junqiang.lu
* @Date: 2020/12/1
*/
@Slf4j
@RestController
@RequestMapping("/api/nacos")
public class NacosProviderController {
@Value("${server.port}")
private Integer port;
/**
* hello 方法
*
* @param helloParam
* @return
*/
@GetMapping(value = "/hello", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<String> sayHello(HelloParam helloParam) {
String result = "Hello," + helloParam.getName();
log.info("serverPort: {}", port);
log.info("result: {}", result);
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
}
连续通过微服务请求接口,查看查看 Skywalking 控制台
警告详情
后台警告请求参数
[
{
"scopeId": 2,
"alarmMessage": "Response time of service instance [email protected] of cloud-alibaba-gateway-filter is more than 1000ms in 2 minutes of last 10 minutes",
"tags": [],
"id0": "Y2xvdWQtYWxpYmFiYS1nYXRld2F5LWZpbHRlcg==.1_ODRiNmQzODVlMzMzNGE5NmE1YzkxYTAyNWEzMjBkMzNAMTcyLjE2LjIxLjY2",
"scope": "SERVICE_INSTANCE",
"id1": "",
"name": "[email protected] of cloud-alibaba-gateway-filter",
"ruleName": "service_instance_resp_time_rule",
"startTime": 1637656398030
},
{
"scopeId": 2,
"alarmMessage": "Response time of service instance [email protected] of cloud-alibaba-server-consumer-feign is more than 1000ms in 2 minutes of last 10 minutes",
"tags": [],
"id0": "Y2xvdWQtYWxpYmFiYS1zZXJ2ZXItY29uc3VtZXItZmVpZ24=.1_ZWRkMDI3MTAzZTVhNGJhZGI4OGZhMjQ5ZWI3NmQ1OGVAMTcyLjE2LjIxLjY2",
"scope": "SERVICE_INSTANCE",
"id1": "",
"name": "[email protected] of cloud-alibaba-server-consumer-feign",
"ruleName": "service_instance_resp_time_rule",
"startTime": 1637656398030
},
{
"scopeId": 2,
"alarmMessage": "Response time of service instance [email protected] of cloud-alibaba-server-provider is more than 1000ms in 2 minutes of last 10 minutes",
"tags": [],
"id0": "Y2xvdWQtYWxpYmFiYS1zZXJ2ZXItcHJvdmlkZXI=.1_MmI0OWNmYTk4NmFkNGU0NWJhMjA4NDM4OGEyODA1NWZAMTcyLjE2LjIxLjY2",
"scope": "SERVICE_INSTANCE",
"id1": "",
"name": "[email protected] of cloud-alibaba-server-provider",
"ruleName": "service_instance_resp_time_rule",
"startTime": 1637656398030
},
{
"scopeId": 6,
"alarmMessage": "Response time of endpoint relation User in User to /cloud-alibaba-consumer-feign/api/nacos/consumer/hello in cloud-alibaba-gateway-filter is more than 1000ms in 2 minutes of last 10 minutes",
"tags": [],
"id0": "VXNlcg==.0_VXNlcg==",
"scope": "ENDPOINT_RELATION",
"id1": "Y2xvdWQtYWxpYmFiYS1nYXRld2F5LWZpbHRlcg==.1_L2Nsb3VkLWFsaWJhYmEtY29uc3VtZXItZmVpZ24vYXBpL25hY29zL2NvbnN1bWVyL2hlbGxv",
"name": "User in User to /cloud-alibaba-consumer-feign/api/nacos/consumer/hello in cloud-alibaba-gateway-filter",
"ruleName": "endpoint_relation_resp_time_rule",
"startTime": 1637656398030
},
{
"scopeId": 6,
"alarmMessage": "Response time of endpoint relation SpringCloudGateway/RoutingFilter in cloud-alibaba-gateway-filter to {GET}/api/nacos/consumer/hello in cloud-alibaba-server-consumer-feign is more than 1000ms in 2 minutes of last 10 minutes",
"tags": [],
"id0": "Y2xvdWQtYWxpYmFiYS1nYXRld2F5LWZpbHRlcg==.1_U3ByaW5nQ2xvdWRHYXRld2F5L1JvdXRpbmdGaWx0ZXI=",
"scope": "ENDPOINT_RELATION",
"id1": "Y2xvdWQtYWxpYmFiYS1zZXJ2ZXItY29uc3VtZXItZmVpZ24=.1_e0dFVH0vYXBpL25hY29zL2NvbnN1bWVyL2hlbGxv",
"name": "SpringCloudGateway/RoutingFilter in cloud-alibaba-gateway-filter to {GET}/api/nacos/consumer/hello in cloud-alibaba-server-consumer-feign",
"ruleName": "endpoint_relation_resp_time_rule",
"startTime": 1637656398030
}
]
可以提取其中的字段,加工之后通知给开发者
微服务链路追踪组件 Skywalking 实战
Gtihub 源码地址 : https://github.com/Flying9001/springBootDemo
个人公众号:404Code,分享半个互联网人的技术与思考,感兴趣的可以关注.