当前位置: 首页 > 编程笔记 >

Spring Cloud Admin健康检查 邮件、钉钉群通知的实现

白侯林
2023-03-14
本文向大家介绍Spring Cloud Admin健康检查 邮件、钉钉群通知的实现,包括了Spring Cloud Admin健康检查 邮件、钉钉群通知的实现的使用技巧和注意事项,需要的朋友参考一下

本文主要介绍了Spring Cloud Admin的使用,分享给大家,具体如下:

源码地址:https://github.com/muxiaonong/Spring-Cloud/tree/master/cloudadmin

Admin 简介

官方文档:What is Spring Boot Admin?

SpringBootAdmin是一个用于管理和监控SpringBoot微服务的社区项目,可以使用客户端注册或者Eureka服务发现向服务端提供监控信息。

注意,服务端相当于提供UI界面,实际的监控信息由客户端Actuator提供

通过SpringBootAdmin,你可以通过华丽大气的界面访问到整个微服务需要的监控信息,例如服务健康检查信息、CPU、内存、操作系统信息等等

本篇文章使用SpringBoot 2.3.3.RELEASE、SpringCloud Hoxton.SR6、SpringBoot Admin 2.2.3版本,此外,服务注册中心采用eureka

一、SpringCloud使用SpringBoot Admin

1.1 创建一个SpringBoot项目,命名为admin-test,引入如下依赖

<!-- Admin 服务 -->
 <dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-starter-server</artifactId>
   <version>2.2.1</version>
 </dependency>
 <!-- Admin 界面 -->
 <dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-server-ui</artifactId>
   <version>2.2.1</version>
 </dependency>

1.2 启动类

@SpringBootApplication
@EnableAdminServer
public class AdminTestApplication {

  public static void main(String[] args) {
    SpringApplication.run(AdminTestApplication.class, args);
  }
  
 }

1.3 配置文件

spring.application.name=admin-test

management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# spring cloud access&secret config
alibaba.cloud.access-key=****
alibaba.cloud.secret-key=****

1.4 启动项目

输入项目地址:http://localhost:8080/applications

二、配置邮件通知

2.1 pom

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.2 邮件配置

spring.mail.host=smtp.qq.com
spring.mail.username=单纯QQ号
spring.mail.password=授权码
spring.mail.properties.mail.smpt.auth=true
spring.mail.properties.mail.smpt.starttls.enable=true
spring.mail.properties.mail.smpt.starttls.required=true

#收件邮箱
spring.boot.admin.notify.mail.to=xxxx@qq.com
# 发件邮箱
spring.boot.admin.notify.mail.from= xxxx@qq.com

2.3 QQ邮箱设置

找到自己的QQ邮箱

QQ邮箱 》 设置 》 账户 》红框处获取 授权码


我们将 consumer 服务下线后,

接着我们就收到了邮件通知,告诉我们服务关闭了

三、发送钉钉群通知

找到群里面的 群设置 》 智能群助手 》 添加机器人


注意:这里的自定义关键词一定要和项目的关键字匹配


获取 Webhook 到项目中,这个是后面要使用到的


启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;

@SpringBootApplication
@EnableAdminServer
public class AdminApplication {

	public static void main(String[] args) {
		SpringApplication.run(AdminApplication.class, args);
	}
	  @Bean
	  public DingDingNotifier dingDingNotifier(InstanceRepository repository) {
	    return new DingDingNotifier(repository);
	  }
}

通知类:

import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import reactor.core.publisher.Mono;

public class DingDingNotifier extends AbstractStatusChangeNotifier {
	public DingDingNotifier(InstanceRepository repository) {
    super(repository);
  }
  @Override
  protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
    String serviceName = instance.getRegistration().getName();
    String serviceUrl = instance.getRegistration().getServiceUrl();
    String status = instance.getStatusInfo().getStatus();
    Map<String, Object> details = instance.getStatusInfo().getDetails();
    StringBuilder str = new StringBuilder();
    str.append("服务预警 : 【" + serviceName + "】");
    str.append("【服务地址】" + serviceUrl);
    str.append("【状态】" + status);
    str.append("【详情】" + JSONObject.toJSONString(details));
    return Mono.fromRunnable(() -> {
      DingDingMessageUtil.sendTextMessage(str.toString());
    });
  }
}

发送工具类

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSONObject;

public class DingDingMessageUtil {
	public static String access_token = "Token";
  public static void sendTextMessage(String msg) {
    try {
      Message message = new Message();
      message.setMsgtype("text");
      message.setText(new MessageInfo(msg));
      URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
      // 建立 http 连接
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setUseCaches(false);
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Charset", "UTF-8");
      conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
      conn.connect();
      OutputStream out = conn.getOutputStream();
      String textMessage = JSONObject.toJSONString(message);
      byte[] data = textMessage.getBytes();
      out.write(data);
      out.flush();
      out.close();
      InputStream in = conn.getInputStream();
      byte[] data1 = new byte[in.available()];
      in.read(data1);
      System.out.println(new String(data1));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

消息类:

public class Message {
	private String msgtype;
  private MessageInfo text;
  public String getMsgtype() {
    return msgtype;
  }
  public void setMsgtype(String msgtype) {
    this.msgtype = msgtype;
  }
  public MessageInfo getText() {
    return text;
  }
  public void setText(MessageInfo text) {
    this.text = text;
  }
}
public class MessageInfo {
  private String content;
  public MessageInfo(String content) {
    this.content = content;
  }
  public String getContent() {
    return content;
  }
  public void setContent(String content) {
    this.content = content;
  }
}

我们下线一个服务后,就可以看到钉钉群就发了消息的通知

同时,当我们启动服务的时候,也会有消息通知我们服务启动了


四 总结

上面就是我们对admin 健康检查的实际应用,在企业中一般会有短信通知+钉钉群通知和邮件,感兴趣的小伙伴可以去试试看,还是挺好玩的,还有一个就是微信通知,在服务号 模板消息感兴趣的小伙伴可以自行去研究看看,大家加油~

到此这篇关于Spring Cloud Admin健康检查 邮件、钉钉群通知的实现的文章就介绍到这了,更多相关Spring Cloud Admin 通知内容请搜索小牛知识库以前的文章或继续浏览下面的相关文章希望大家以后多多支持小牛知识库!

 类似资料:
  • 钉邮是钉钉的邮件功能模块,管理员可通过管理后台统一绑定企业邮箱(以企业自己的域名为后缀名的工作邮箱)及配置员工邮箱账号,让企业员工方便地在钉钉上收发工作邮件。 绑定企业邮箱 已有企业邮箱 阿里企业邮箱 进入钉邮,点击「绑定阿里企业邮箱」 绑定邮箱后缀 绑定邮件已发送到postmaster@企业域名 登录企业域名,打开绑定邮件,点击「绑定」 其他企业邮箱 进入钉邮,点击「绑定其他企业邮箱」 输入邮箱

  • SOFABoot 为 Spring Boot 的健康检查能力增加了 Readiness Check 的能力。如果你需要使用 SOFA 中间件,那么建议使用 SOFABoot 的健康检查能力的扩展,来更优雅的上线应用实例 引入健康检查扩展 要引入 SOFABoot 的健康检查能力的扩展,只需要引入以下的 Starter 即可: <dependency> <groupId>com.alipay

  • 感谢 windNight 给的PR 增加了 钉钉通知功能 默认该功能是关闭的,需要打开开关 1. 第一种方式可以在工程的配置文件启用钉钉通知功能 2.第二种方式可以在全局动态参数里面配置 如何在全局动态参数里面配置开启钉钉通知开关 Hangfire.HttpJob 支持job运行成功or失败发送钉钉机器人通知 字段名称 备注 SendSuccess 这个httpjob请求无异常的时候是否发送通知

  • 配置零门槛,有线组网&无线组网快速扩容,钉钉免密一键连网安全便捷。 安装说明 包材产品清单 包材产品清单 包材产品清单 安装方式 桌面平放 挂墙 挂墙 吸顶 吸顶 扎绳捆绑 扎绳捆绑 配置说明 配置第一台C1 仅需4步5分钟:连接WiFi-打开钉钉-绑定团队-设置网络名称。 注意:仅支持通过手机进行配置。 通电和连网 手机连接”DingTalk”开头的WiFi 打开钉钉,按引导绑定团队和设置网络名

  • 1.自我介绍 2面向对象介绍一下 3.面向过程介绍 4.面向过程的场景及缺点 5.线程池作用(说了句避免线程创建的开销) 6.线程创建开销很大吗,需要哪些开销?线程竞争如何解决 7.c++11新特性有哪些 8.lambda表达式的捕获方式及lambda的一个使用场景 9.实习的工作及简单问了点问题,没为难我 10.项目或者实习哪个的收获最大 11.makefile构建多个c++程序你会如何设计提示

  • 健康检查配置概述。 filter.http.HealthCheck filter.http.HealthCheck proto { "pass_through_mode": "{...}", "endpoint": "...", "cache_time": "{...}" } pass_through_mode (BoolValue, REQUIRED) 指定过滤器是否在传递模式下运