飞书机器人的使用
使用飞书机器人的原因:
- 现在是微服务的时代,但是微服务的治理和异常监控 也是一个问题.办公软件飞书或者企业微信.都提供了机器人.利用它 就可以实现项目异常监控,比如项目启动关闭通知.或者当出现人工介入的场景,通过机器人发送消息 让我们可以更快的接收到消息,并且比短信通知更节约,邮件提醒更高效和实用.
- 如果是堆栈异常 记得字符串截取.文本长度是有限制的.
监控项目启动/关闭 通知 启动类
package com.zwl.main;
import com.zwl.common.utils.RobotUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.net.InetAddress;
import java.net.UnknownHostException;
@SpringBootApplication(scanBasePackages = {"com.zwl"})
@RestController
@Slf4j
@ComponentScan({"com.zwl"})
@EnableScheduling
public class Application {
@GetMapping("/")
public String index() {
return "G_Link_helps Application Server is running";
}
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext application = SpringApplication.run(Application.class, args);
Environment env = application.getEnvironment();
log.info("\n--------------------------------------------------------------------------------------------------------------------\n\t" +
"Application '{}' is running! Access URLs:\n\t" +
"Local: \t\thttp://localhost:{}/\n\t" +
"docs: \thttp://{}:{}/docs.html" +
"\n--------------------------------------------------------------------------------------------------------------------",
"G_Link_helps Server Application v1.0",
env.getProperty("server.port"),
InetAddress.getLocalHost().getHostAddress(),
env.getProperty("server.port"));
RobotUtils.notice("G_Link_helps 自动助手" + InetAddress.getLocalHost().getHostAddress() + ":" + env.getProperty("server.port") + "启动成功");
// 注册关闭钩子
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
RobotUtils.notice("G_Link_helps 自动助手" + InetAddress.getLocalHost().getHostAddress() + ":" + env.getProperty("server.port") + "应用关闭");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
});
}
}
项目监控工具,通过飞书机器人 发送报警信息或者异常信息工具类
package com.zwl.common.utils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* @program: G_Link_helps
* @description: RobotNoticeUtils
* @author: 单人影
* @create: 2022-08-16 16:37
**/
@Slf4j
public class RobotUtils {
private static RestTemplate restTemplate;
private static final String robotUrl = "飞书的机器人地址";
static {
restTemplate = new RestTemplate();
}
/**
* @Description: 通知
* @Param: [value]
* @return: java.lang.String
* @Author: 单人影
* @Date: 2022/8/17 17:22
*/
public static String notice(String value) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg_type", "text");
Map<String, String> map = new HashMap<>();
map.put("text", "【提醒时间】: " + DateUtils.getNowStr() + "\n【提醒内容】: " + value);
jsonObject.put("content", map);
ResponseEntity<String> postForEntity = null;
try {
postForEntity = restTemplate.postForEntity(robotUrl, jsonObject, String.class);
} catch (Exception e) {
e.printStackTrace();
}
if (postForEntity.getStatusCodeValue() != 200) {
log.error("飞书报警发送失败,请求参数:{},响应参数:{}", value, postForEntity.getBody());
return StringUtils.EMPTY;
}
// 获取http状态码
int statusCodeValue = postForEntity.getStatusCodeValue();
log.info("httpCode-----" + statusCodeValue);
// 获取返回体
String body = postForEntity.getBody();
log.info("body-----{}", body);
log.info("ResponseEntity----" + postForEntity);
return body;
}
}
机器人地址