SpringBootAdmin监控程序
一、介绍
Spring Boot Admin 是一个用于监控和管理 SpringBoot 应用程序的开源项目。它提供了一个可视化的用户界面,通过该界面可以实时监控应用程序的运行状态、性能指标等信息。
Spring Boot Admin可以监控多个 SpringBoot 应用程序,它提供了以下功能:
- 
简单的集成:Spring Boot Admin提供了一个易于集成的客户端库,可以方便地将应用程序连接到监控服务器。
 
- 
实时监控:可以查看应用程序的运行状态、线程池、内存使用情况、请求指标等实时信息,帮助开发人员及时发现和解决问题。
 
- 
健康检查:Spring Boot Admin可以监测应用程序的健康状态,包括数据库连接、缓存服务、消息队列等组件的可用性。
 
- 
日志管理:可以查看和管理应用程序的日志,包括实时查看日志、搜索和过滤日志等功能。
 
- 
通知与告警:Spring Boot Admin支持通过邮件、Slack 等方式发送通知和告警,例如当应用程序发生异常或出现性能问题时。
 
总之,Spring Boot Admin提供了一个便捷的监控和管理平台,帮助开发人员更好地了解和管理他们的 SpringBoot 应用程序。
二、编码
1)server
首先引用依赖,版本和springBoot一致的,虽然他们是不同的公司出版
1 2 3 4 5
   | <dependency>     <groupId>de.codecentric</groupId>     <artifactId>spring-boot-admin-starter-server</artifactId>     <version>2.7.7</version> </dependency>
   | 
 
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
   | server:   port: 8000
  spring:   application:     name: web-monitor
 
  logging:   pattern:          console: "%red(%d{yyyy-MM-dd HH:mm:ss.SSS}) %green([%thread]) %blue([%-5level]) %cyan(%logger{36}:%line) %msg%n"
  notify:   config:     enable: true          status:       - UP       - OFFLINE   email:     host: smtp.qq.com     port: 587     from: ****     user: ****     pass: ****     receive-email:       - ban-moon@outlook.com
   | 
 
正常的SpringBoot启动类,加上启动注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | package com.banmoon;
  import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
  @EnableAdminServer @SpringBootApplication public class WebMonitorMain {
      public static void main(String[] args) {         SpringApplication.run(WebMonitorMain.class);     } }
   | 
 
主要继承AbstractStatusChangeNotifier这个类,重写doNotify()方法。
如下,继承后我写了一个分发器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
   | package com.banmoon.notify;
  import com.banmoon.config.NotifyConfigProperties; import com.banmoon.handler.InstanceEventHandler; 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 lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono;
  import javax.annotation.Resource; import java.util.Map;
 
 
 
  @Slf4j @Component public class DispatcherNotifier extends AbstractStatusChangeNotifier {
      @Resource     private NotifyConfigProperties notifyConfigProperties;
      @Resource     private Map<String, InstanceEventHandler> instanceEventHandlerMap;
      public DispatcherNotifier(InstanceRepository repository) {         super(repository);     }
      @Override     protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {         return Mono.fromRunnable(() -> {             if (Boolean.TRUE.equals(notifyConfigProperties.getEnable())) {                 String name = event.getClass().getSimpleName();                 instanceEventHandlerMap.get(name).handler(event, instance);             }         });     } }
   | 
 
当然,代码有点多,我将后续的代码放在了Gitee.com,欢迎进行访问。
完成后,直接启动就好
2)client
对于client的话,就是被监控的服务。比较简单,首先添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | <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> <dependency>     <groupId>de.codecentric</groupId>     <artifactId>spring-boot-admin-starter-client</artifactId>     <version>2.7.7</version> </dependency>
   | 
 
在配置文件中,指定监控服务的地址,这样就可以了
1 2 3 4 5 6 7 8 9
   | spring:   application:     name: web-base   boot:     admin:       client:         url: http://localhost:8000         instance:           prefer-ip: true
   | 
 
3)测试
启动client服务,可以看到服务在线,同时邮件接收到服务上线的通知



三、最后
这上面还可以看一些单例类,映射信息,配置信息,内存信息,堆栈信息,日志信息
我是半月,你我一同共勉!!!