Spring Cloud之——SpringBootAdmin监控程序

osc_eoqljui5 2020-11-10 08:52:15
java Spring Boot Spring Cloud Eureka consul


简介

Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul)发现。 UI是的AngularJs应用程序,展示Spring Boot Admin Client的Actuator端点上的一些监控。常见的功能或者监控如下:

  • 显示健康状况
  • 显示详细信息,例如
    JVM和内存指标
    micrometer.io指标
    数据源指标
    缓存指标



  • 显示构建信息编号
  • 关注并下载日志文件
  • 查看jvm系统和环境属性
  • 查看Spring Boot配置属性
  • 支持Spring Cloud的postable / env-和/ refresh-endpoint
  • 轻松的日志级管理
  • 与JMX-beans交互
  • 查看线程转储
  • 查看http跟踪
  • 查看auditevents
  • 查看http-endpoints
  • 查看计划任务
  • 查看和删除活动会话(使用spring-session)
  • 查看Flyway / Liquibase数据库迁移
  • 下载heapdump
  • 状态变更通知(通过电子邮件,Slack,Hipchat,…)
  • 状态更改的事件日志(非持久性)

引用Pom


<!-- 发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--暴露各种指标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- SpringBootAdmin -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.3</version>
</dependency>
<!-- 服务注册 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y

Yml文件

spring:
application:
name: spring-boot-admin
main:
allow-bean-definition-overriding: true
boot:
admin:
discovery:
ignored-services: consul
#监控通知邮箱
notify:
mail:
from: 邮箱账号
to: 接收邮箱账号
#配置邮箱
mail:
host: smtp.163.com
username: 邮箱账号
password: 授权码
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
#QQ邮箱必须加
ssl:
enable: true
cloud:
consul:
#配置consul服务器的host
host: localhost
#配置端口
port: 8500
discovery:
#配置健康检查时间间隔
health-check-interval: 15s
#配置实例id
instance-id: ${
spring.application.name}:${
vcap.application.instance_id:${
spring.application.instance_id:${
random.value}}}
#配置服务注册
register: true
#表示注册时使用ip而不是hostname
prefer-ip-address: true
#健康检查失败多长时间取消注册
health-check-critical-timeout: 30s

全局配置

新建全局配置类:MonkeyConfiguration

package com.admin.admin;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
/**
* @Author: LailaiMonkey
* @Description:
* @Date:Created in 2020-09-29 15:03
* @Modified By:
*/
@Configuration
public class MonkeyConfiguration {

@Bean
public MonkeyCompositeNotifier monkeyCompositeNotifier(InstanceRepository instanceRepository,
JavaMailSender mailSender) {

return new MonkeyCompositeNotifier(instanceRepository, mailSender);
}
}
新建消息模型:MessageBody
```java
package com.admin.admin.sendMessage.model;
/**
* @Author: LailaiMonkey
* @Description:
* @Date:Created in 2020-11-06 15:48
* @Modified By:
*/
public class MessageBody {

/**
* 内容
*/
private String text;
/**
* 标题
*/
private String title;
public String getText() {

return text;
}
public void setText(String text) {

this.text = text;
}
public String getTitle() {

return title;
}
public void setTitle(String title) {

this.title = title;
}
}

新建发送消息类:MonkeyCompositeNotifier

package com.admin.admin;
import com.admin.admin.sendMessage.SendAllMessage;
import com.admin.admin.sendMessage.model.MessageBody;
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.InstanceDeregisteredEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.domain.values.Registration;
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import reactor.core.publisher.Mono;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
/**
* @Author: LailaiMonkey
* @Description:
* @Date:Created in 2020-09-29 15:07
* @Modified By:
*/
public class MonkeyCompositeNotifier extends AbstractEventNotifier {

private static final Logger logger = LoggerFactory.getLogger(MonkeyCompositeNotifier.class);
private JavaMailSender mailSender;
@Value("${spring.boot.admin.notify.mail.from}")
private String from;
@Value("${spring.boot.admin.notify.mail.to}")
private String to;
private final static Map<Class<?>, BiFunction<InstanceEvent, Instance, MessageBody>> cacheMap = new HashMap<>();
static {

//注册
cacheMap.put(InstanceRegisteredEvent.class, MonkeyCompositeNotifier::register);
//注销
cacheMap.put(InstanceDeregisteredEvent.class, MonkeyCompositeNotifier::deRegistered);
//状态变更
cacheMap.put(InstanceStatusChangedEvent.class, MonkeyCompositeNotifier::statusChanged);
}
public MonkeyCompositeNotifier(InstanceRepository repository, JavaMailSender mailSender) {

super(repository);
this.mailSender = mailSender;
}
@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {

return Mono.fromRunnable(() -> {

BiFunction<InstanceEvent, Instance, MessageBody> messageBiFunction = cacheMap.get(event.getClass());
if (messageBiFunction != null) {

MessageBody messageBody = messageBiFunction.apply(event, instance);
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, StandardCharsets.UTF_8.name());
try {

message.setText(messageBody.getText());
message.setSubject(messageBody.getTitle());
message.setTo(to);
message.setFrom(from);
mailSender.send(mimeMessage);
} catch (MessagingException e) {

e.printStackTrace();
logger.error("发送邮件异常!", e);
}
}
});
}
/**
* 注册通知
*
* @param event
* @param instance
* @return
*/
private static MessageBody register(InstanceEvent event, Instance instance) {

Registration registration = instance.getRegistration();
MessageBody body = new MessageBody();
body.setText("服务名称:" + registration.getName() + " 服务地址:" + registration.getServiceUrl());
body.setTitle("服务上线通知!");
return body;
}
/**
* 注销通知
*
* @param event
* @param instance
* @return
*/
private static MessageBody deRegistered(InstanceEvent event, Instance instance) {

Registration registration = instance.getRegistration();
MessageBody body = new MessageBody();
body.setText("服务名称:" + registration.getName() + " 服务地址:" + registration.getServiceUrl());
body.setTitle("服务下线通知!");
return body;
}
/**
* 状态变更通知
*
* @param event
* @param instance
* @return
*/
private static MessageBody statusChanged(InstanceEvent event, Instance instance) {

StatusInfo statusInfo = ((InstanceStatusChangedEvent) event).getStatusInfo();
Registration registration = instance.getRegistration();
MessageBody body = new MessageBody();
body.setText("服务名称:" + registration.getName() + " 服务地址:" + registration.getServiceUrl() + " 状态:" + statusInfo.getStatus());
body.setTitle("服务状态变量通知!");
return body;
}
}

如果看过源码的小伙伴知道,自带发送邮件服务继承AbstractStatusChangeNotifier,而我们需要重写他的方法是无法收到消息,这是为什么呢?
它有一个shouldNotify方法,把一些方法过滤了。所以我们继承AbstractEventNotifier

 @Override
protected boolean shouldNotify(InstanceEvent event, Instance instance) {

if (event instanceof InstanceStatusChangedEvent) {

InstanceStatusChangedEvent statusChange = (InstanceStatusChangedEvent) event;
String from = getLastStatus(event.getInstance());
String to = statusChange.getStatusInfo().getStatus();
return Arrays.binarySearch(ignoreChanges, from + ":" + to) < 0
&& Arrays.binarySearch(ignoreChanges, "*:" + to) < 0
&& Arrays.binarySearch(ignoreChanges, from + ":*") < 0;
}
return false;
}

而且他本身的邮件通知只支持InstanceStatusChangedEvent(状态变更通知),没有注册、上下线通知,所以我们重写一个。

访问:http://localhost:8080出现管理界面

权限访问配置

  • 添加Pom文件
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
  • 配置文件
spring:
security:
user:
name: "admin"
password: "admin"

访问页面时候会弹出登录界面。

版权声明
本文为[osc_eoqljui5]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4255780/blog/4710492

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云