1. pom.xml 配置
<?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.tongfudun</groupId>
<artifactId>skywalking</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>sky walking</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>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</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>
2. 告警实体类:
package com.tongfudun.skywalking.bean;
import lombok.Data;
import java.io.Serializable;
@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;
}
3. 钉钉机器人消息体:
package com.tongfudun.skywalking.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;
/**
* @author
*/
@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);
}
}
}
4. AlarmController
package com.tongfudun.skywalking.controller;
import com.alibaba.fastjson.JSON;
import com.tongfudun.skywalking.bean.AlarmMessageDTO;
import com.tongfudun.skywalking.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
*/
@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())));
}
}
}
5. application.properties配置
server.port=8100
server.servlet.context-path=/
dingtalk.webhook=https://oapi.dingtalk.com/robot/send?access_token=74e2532ea3fed70d0retergscswew436747c642d4c1bd4bc1de31076d63de33cc
dingtalk.secret=SEC81a6264213b135aerterytry45ec454505c46b81acf3ddf12b97172221b0088d7c
6. 打包
打包成功后再能通外网的服务器上启动java服务,对外服务端口是8100
7. skywalking的配置
alarm-settings.yml 配置增加如下配置请求地址 http://192.168.95.201:8100/skywalking/alarm 注:192.168.95.201为java服务运行服务器主机
webhooks:
- http://192.168.95.201:8100/skywalking/alarm