Spring?Boot?Admin集成與自定義監(jiān)控告警示例詳解
一.Spring Boot Admin集成
1.引入依賴
<dependencies> <!-- Spring Boot Admin Server 服務(wù)端引入--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server</artifactId> <version>2.7.1</version> </dependency> <!-- Spring Boot Admin Client客戶端引入 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.7.1</version> </dependency> <!-- 使用nacos時(shí)引入--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- Spring Boot Starter Mail 郵件提醒時(shí)引入--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies>
2.添加配置
management: endpoints: web: exposure: #啟用所有的 Actuator 端點(diǎn) include: "*" endpoint: health: show-details: always server: port: 9000 spring: application: name: admin-client cloud: nacos: discovery: server-addr: 127.0.0.1:8848
3.監(jiān)控界面
啟動(dòng)后訪問:http://IP:9000,即可訪問到監(jiān)控頁面
二.Spring Boot Admin告警機(jī)制
Spring Boot Admin 的告警機(jī)制可以幫助你監(jiān)控 Spring Boot 應(yīng)用的狀態(tài)并在發(fā)生重要事件時(shí)發(fā)送通知。Spring Boot Admin 提供了多種告警方式和配置選項(xiàng),以便與不同的通知系統(tǒng)集成。以下是 Spring Boot Admin 的告警機(jī)制的詳細(xì)說明
1. 基本告警機(jī)制
Spring Boot Admin 的告警機(jī)制基于監(jiān)控事件和實(shí)例狀態(tài)變化。可以配置不同的觸發(fā)條件和通知方式來處理這些事件。
Spring Boot Admin 監(jiān)控應(yīng)用的狀態(tài)變化(例如:UP、DOWN、OFFLINE)。當(dāng)應(yīng)用的狀態(tài)發(fā)生變化時(shí),可以觸發(fā)告警。告警通常包括以下幾個(gè)方面:
實(shí)例狀態(tài)變化: 例如,當(dāng)應(yīng)用從 UP 狀態(tài)變?yōu)?DOWN 狀態(tài)時(shí)。健康檢查: 健康檢查失敗時(shí),可以觸發(fā)告警。
2. 配置告警
配置郵件信息
在 Spring Boot Admin 服務(wù)器的配置文件中,可以設(shè)置告警的基本信息,包括通知發(fā)送者、SMTP 配置等。
示例 application.yml 配置:
spring: boot: admin: notify: mail: from: "your-email@example.com" to: "alert-email@example.com" host: "smtp.example.com" port: 587 username: "your-email@example.com" password: "your-email-password" properties.mail.smtp.auth: true properties.mail.smtp.starttls.enable: true
或者通過bean配置方式
@Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost(MailUtil.MAIL_SMTP_HOST); // 郵件服務(wù)器地址 mailSender.setPort(Integer.parseInt(MailUtil.MAIL_SMTP_PORT)); // 使用SSL的端口號(hào) mailSender.setUsername(MailUtil.MAIL_SMTP_USER); // 發(fā)送郵件的用戶名 mailSender.setPassword(MailUtil.MAIL_SMTP_PASSWORD); // 發(fā)送郵件的密碼 Properties props = mailSender.getJavaMailProperties(); props.put("mail.transport.protocol", "smtp");//常見的協(xié)議類型smtp,imap,pop3 props.put("mail.smtp.auth", "true"); props.put("mail.smtp.socketFactory.port", MailUtil.MAIL_SMTP_PORT); // 配置socket工廠端口 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // 使用SSL props.put("mail.smtp.starttls.enable", "false"); // 由于使用SSL,禁用STARTTLS // props.put("mail.debug", "true"); // 調(diào)試模式 return mailSender; }
配置觸發(fā)條件
Spring Boot Admin 允許基于特定條件觸發(fā)告警。以下是一些常見的告警觸發(fā)條件:
- CPU 使用率: 當(dāng) CPU 使用率超過指定閾值時(shí)觸發(fā)告警。
- 內(nèi)存使用率: 當(dāng) JVM 內(nèi)存使用超過指定閾值時(shí)觸發(fā)告警。
- 線程數(shù): 當(dāng)線程數(shù)超出設(shè)定范圍時(shí)觸發(fā)告警。
- GC 暫停: 當(dāng)垃圾回收暫停時(shí)間過長(zhǎng)時(shí)觸發(fā)告警。
- 自定義指標(biāo): 可以基于應(yīng)用程序的自定義指標(biāo)來觸發(fā)告警。
示例配置:
spring: boot: admin: monitor: status-lifetime: 10m threshold: 0.9 notify: triggers: - name: cpu metric: "system.cpu.usage" threshold: 0.9 above: true period: 5m - name: memory metric: "jvm.memory.used" threshold: 0.9 above: true period: 5m - name: jvm-heap metric: "jvm.memory.heap.used" threshold: 0.9 above: true period: 5m - name: threads metric: "jvm.threads.live" threshold: 1000 above: true period: 5m - name: gc metric: "jvm.gc.pause" threshold: 0.1 above: true period: 5m
2.1 triggers觸發(fā)器講解
spring.boot.admin.monitor.triggers 是 Spring Boot Admin 的配置項(xiàng),用于定義在監(jiān)控應(yīng)用程序?qū)嵗龝r(shí)的觸發(fā)器。通過這些觸發(fā)器,你可以設(shè)定特定的條件,當(dāng)這些條件滿足時(shí),Spring Boot Admin 會(huì)執(zhí)行相應(yīng)的動(dòng)作,比如發(fā)送通知、觸發(fā)告警等。
觸發(fā)器的基本概念
觸發(fā)器(Triggers)是基于條件表達(dá)式的邏輯,用于自動(dòng)化監(jiān)控和管理任務(wù)。當(dāng)應(yīng)用程序的狀態(tài)或性能指標(biāo)滿足預(yù)設(shè)的條件時(shí),觸發(fā)器會(huì)激活并執(zhí)行預(yù)定義的動(dòng)作。這種機(jī)制使得系統(tǒng)監(jiān)控變得更加智能和自動(dòng)化。
配置項(xiàng)解釋
spring.boot.admin.monitor.triggers 配置項(xiàng)用于定義一個(gè)或多個(gè)觸發(fā)器。每個(gè)觸發(fā)器可以包含以下部分:
1.condition: 條件表達(dá)式,用于定義觸發(fā)器何時(shí)觸發(fā)。這通常是一個(gè)SpEL(Spring Expression Language)表達(dá)式。
2.action: 當(dāng)條件滿足時(shí),應(yīng)該執(zhí)行的動(dòng)作。動(dòng)作可以包括發(fā)送郵件、觸發(fā)Webhook、記錄日志等。
觸發(fā)器配置結(jié)構(gòu)
觸發(fā)器通常配置在 application.yml 或 application.properties 文件中,以下是配置結(jié)構(gòu)的詳細(xì)解釋:
spring: boot: admin: monitor: triggers: - condition: "health.status=='DOWN' or info.memory.free<52428800 or info.cpu.usage>0.8" # 在這里定義動(dòng)作,比如發(fā)送通知或執(zhí)行其他操作 - action: ...
1.condition
condition: 這是觸發(fā)器的核心部分,定義了觸發(fā)的條件。條件可以基于健康狀態(tài)、度量指標(biāo)(如 CPU 使用率、內(nèi)存使用情況等),甚至是自定義的應(yīng)用程序信息。
典型的條件表達(dá)式可能包括健康狀態(tài)檢查、資源使用率監(jiān)控等。
條件可以包括對(duì)監(jiān)控?cái)?shù)據(jù)、應(yīng)用狀態(tài)或其他系統(tǒng)指標(biāo)的檢查。以下是一些常見的配置條件:
1. 應(yīng)用健康狀態(tài)
health.status=='UP': 應(yīng)用健康狀態(tài)為 "UP"。
health.status=='DOWN': 應(yīng)用健康狀態(tài)為 "DOWN"。
health.status=='OUT_OF_SERVICE': 應(yīng)用健康狀態(tài)為 "OUT_OF_SERVICE"。
2. 內(nèi)存使用情況
info.memory.usage > 0.8: 內(nèi)存使用率超過 80%。
info.memory.usage < 0.5: 內(nèi)存使用率低于 50%。
3. CPU 使用率
info.cpu.usage > 0.7: CPU 使用率超過 70%。
info.cpu.usage < 0.3: CPU 使用率低于 30%。
4. 自定義指標(biāo)
custom.metric > 100: 某個(gè)自定義指標(biāo)超過 100。
custom.metric < 10: 某個(gè)自定義指標(biāo)低于 10。
5. 應(yīng)用實(shí)例
instance.name=='app-instance-1': 應(yīng)用實(shí)例名稱為 app-instance-1。
instance.id=='12345': 應(yīng)用實(shí)例 ID 為 12345。
6. 事件類型
event.type=='InstanceRegisteredEvent': 事件類型為 InstanceRegisteredEvent。
event.type=='InstanceDeregisteredEvent': 事件類型為 InstanceDeregisteredEvent。
7. 響應(yīng)時(shí)間
http.server.requests.max > 5000: 最大 HTTP 請(qǐng)求響應(yīng)時(shí)間超過 5000 毫秒。
http.server.requests.mean > 2000: 平均 HTTP 請(qǐng)求響應(yīng)時(shí)間超過 2000 毫秒。
示例配置
在配置中可以結(jié)合使用多種條件。例如:
triggers: - condition: health.status=='DOWN' or info.cpu.usage > 0.8 - condition: info.memory.usage > 0.7
這將觸發(fā)警報(bào)當(dāng)應(yīng)用健康狀態(tài)為 "DOWN" 或 CPU 使用率超過 80%,或者內(nèi)存使用率超過 70% 時(shí)。
2.action:
action: 這個(gè)部分定義了當(dāng)條件滿足時(shí)應(yīng)該執(zhí)行的操作??梢允嵌喾N操作,比如發(fā)送郵件、觸發(fā) HTTP 請(qǐng)求(Webhook)、記錄日志,或者調(diào)用自定義的動(dòng)作。
spring.boot.admin 可以與其他系統(tǒng)集成,通過 Webhook 通知外部系統(tǒng),或者使用電子郵件通知運(yùn)維人員。
使用場(chǎng)景
健康監(jiān)控: 你可以設(shè)置一個(gè)觸發(fā)器,當(dāng)應(yīng)用程序的健康狀態(tài)變?yōu)?DOWN 時(shí)自動(dòng)發(fā)送警報(bào)或執(zhí)行恢復(fù)腳本。
性能監(jiān)控: 通過監(jiān)控 CPU 使用率或內(nèi)存使用情況,可以在資源使用率超過特定閾值時(shí)觸發(fā)警報(bào),從而避免潛在的性能問題。
自動(dòng)化響應(yīng): 你可以在觸發(fā)條件下執(zhí)行自動(dòng)化的恢復(fù)操作,或者通知運(yùn)維團(tuán)隊(duì)采取行動(dòng)。
觸發(fā)器的示例
spring: boot: admin: monitor: triggers: - condition: "health.status=='DOWN'" action: type: "email" recipients: - "admin@example.com" subject: "Application Down" body: "The application is currently down. Please check immediately." - condition: "info.memory.free < 104857600" # 100MB action: type: "webhook" url: "https://alert-system.example.com/notify" method: "POST" headers: Content-Type: "application/json" body: '{"message": "Memory usage is critically low"}'
在這個(gè)示例中:
第一個(gè)觸發(fā)器檢測(cè)到應(yīng)用程序健康狀態(tài)為 DOWN 時(shí)發(fā)送電子郵件通知。
第二個(gè)觸發(fā)器檢測(cè)到可用內(nèi)存小于 100MB 時(shí),觸發(fā)一個(gè) HTTP POST 請(qǐng)求,通知外部的告警系統(tǒng)。
完成示例
server.port=9000 #啟用所有的 Actuator 端點(diǎn) management.endpoints.web.exposure.include="*" management.endpoint.health.show-details=always #是否暴露 metrics 端點(diǎn),提供如 JVM 內(nèi)存使用、系統(tǒng) CPU 使用率、線程使用情況等信息,Actuator 端點(diǎn)包括Health,Info,Threads, Heap Dump,Environment,Loggers,Caches,Dump,Trace? management.endpoint.metrics.enabled=true #是否將應(yīng)用程序的度量指標(biāo)導(dǎo)出到控制臺(tái)或日志中 management.metrics.export.simple.enabled=true #郵件服務(wù)器配置 spring.mail.host=smtp.your-email-provider.com spring.mail.port=465 spring.mail.username=your-email@example.com spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true # 告警郵件配置 spring.boot.admin.notify.mail.from=xxxxx@qq.com spring.boot.admin.notify.mail.to=877507054@qq.com spring.boot.admin.notify.mail.subject="Service Alert:[${spring.application.name}] " spring.boot.admin.notify.mail.enabled=true # 檢查和更新注冊(cè)的客戶端應(yīng)用程序的狀態(tài)的頻率 spring.boot.admin.monitor.instances.poll-interval=10s #應(yīng)用程序的狀態(tài)在這個(gè)時(shí)間段內(nèi)沒有更新,會(huì)將其狀態(tài)標(biāo)記為過時(shí)(OFFLINE) spring.boot.admin.monitor.status-lifetime=10s spring.boot.admin.ui.title=消費(fèi)幫扶監(jiān)控平臺(tái) #觸發(fā)器配置,當(dāng)應(yīng)用程序的健康狀態(tài)為 DOWN或者可用內(nèi)存少于 50MB 時(shí)或者當(dāng) CPU 使用率超過 80% 時(shí)觸發(fā) spring.boot.admin.monitor.triggers.0.condition=health.status=='DOWN' or info.memory.free<52428800 or info.cpu.usage>0.1 spring.boot.admin.monitor.triggers.0.action.mail.from=xxxxx@qq.com spring.boot.admin.monitor.triggers.0.action.mail.to=877507054@qq.com,xxxx@163.com spring.boot.admin.monitor.triggers.0.action.subject=[${spring.application.name}] 程序告警 spring.boot.admin.monitor.triggers.0.action.mail.text=The application [${spring.application.name}] (ID: ${spring.boot.admin.client.instance-id}) has a CPU usage over 80%. Immediate attention required. Instance details: IP - ${instance.remoteAddress}
關(guān)鍵變量解釋
${spring.application.name}: 代表當(dāng)前應(yīng)用程序的名稱。這是在 Spring Boot 配置中常用的屬性,能夠標(biāo)識(shí)應(yīng)用程序。
${spring.boot.admin.client.instance-id}: 代表 Spring Boot Admin 為每個(gè)客戶端實(shí)例分配的唯一 ID。如果你在應(yīng)用程序的 application.properties 中配置了 spring.boot.admin.client.instance-id,它可以用來唯一標(biāo)識(shí)每個(gè)實(shí)例。
${instance.remoteAddress}: 代表應(yīng)用程序?qū)嵗?IP 地址,能夠幫助你識(shí)別警報(bào)來自哪個(gè)服務(wù)器。
總結(jié)
spring.boot.admin.monitor.triggers 是 Spring Boot Admin 中一個(gè)強(qiáng)大的功能,用于自動(dòng)化應(yīng)用程序監(jiān)控和管理。通過定義觸發(fā)器條件和相應(yīng)的動(dòng)作,可以實(shí)現(xiàn)智能化的監(jiān)控響應(yīng),確保系統(tǒng)的穩(wěn)定性和可用性。使用觸發(fā)器,你可以在應(yīng)用程序狀態(tài)出現(xiàn)異常時(shí)及時(shí)收到通知或采取自動(dòng)化措施,從而提高系統(tǒng)的可靠性和響應(yīng)速度。
3. 自定義通知
可以實(shí)現(xiàn)自定義的通知邏輯,例如發(fā)送郵件、推送通知、調(diào)用 API 等。通過實(shí)現(xiàn) Notifier 接口或擴(kuò)展 AbstractEventNotifier 類,可以定義自己的告警處理方式。
示例CustomEventNotifier
/** * 自定義通知器類 * { * "names": [ * "http.server.requests:HTTP 請(qǐng)求的計(jì)數(shù)和統(tǒng)計(jì)信息", * "jvm.buffer.count:JVM 中緩沖區(qū)的數(shù)量", * "jvm.buffer.memory.used:JVM 中緩沖區(qū)使用的內(nèi)存量", * "jvm.buffer.total.capacity:JVM 中緩沖區(qū)的總?cè)萘?, * "jvm.classes.loaded:當(dāng)前加載的 JVM 類的數(shù)量", * "jvm.classes.unloaded:已卸載的 JVM 類的數(shù)量", * "jvm.gc.live.data.size:JVM 堆中活躍數(shù)據(jù)的大小", * "jvm.gc.max.data.size:最大可回收的數(shù)據(jù)大小", * "jvm.gc.memory.allocated:分配的 JVM 內(nèi)存量", * "jvm.gc.memory.promoted:晉升的內(nèi)存量", * "jvm.gc.pause:垃圾回收暫停時(shí)間", * "jvm.memory.committed:已提交的 JVM 內(nèi)存量", * "jvm.memory.max:JVM 可用的最大內(nèi)存量", * "jvm.memory.used:當(dāng)前使用的 JVM 內(nèi)存量", * "jvm.threads.daemon:當(dāng)前活躍的守護(hù)線程數(shù)", * "jvm.threads.live:當(dāng)前活躍的線程總數(shù)", * "jvm.threads.peak:JVM 線程的峰值數(shù)量", * "jvm.threads.states:各個(gè)線程狀態(tài)的數(shù)量", * "logback.events:Logback 事件的計(jì)數(shù)", * "process.cpu.usage:進(jìn)程的 CPU 使用率", * "process.files.max:進(jìn)程允許的最大文件描述符數(shù)量", * "process.files.open:當(dāng)前打開的文件描述符數(shù)量", * "process.start.time:進(jìn)程啟動(dòng)時(shí)間", * "process.uptime:進(jìn)程的運(yùn)行時(shí)間", * "system.cpu.count:系統(tǒng)中的 CPU 核心數(shù)量", * "system.cpu.usage:系統(tǒng)的 CPU 使用率", * "system.load.average.1m:系統(tǒng) 1 分鐘內(nèi)的平均負(fù)載", * "tomcat.sessions.active.current:當(dāng)前活躍的 Tomcat 會(huì)話數(shù)量", * "tomcat.sessions.active.max:Tomcat 中的最大活躍會(huì)話數(shù)量", * "tomcat.sessions.alive.max:Tomcat 中的最大存活會(huì)話數(shù)量", * "tomcat.sessions.created:創(chuàng)建的 Tomcat 會(huì)話總數(shù)", * "tomcat.sessions.expired:過期的 Tomcat 會(huì)話數(shù)量", * "tomcat.sessions.rejected:被拒絕的 Tomcat 會(huì)話數(shù)量" * ] * } */ @Component public class CustomNotifier extends AbstractEventNotifier implements HealthIndicator { private static final Logger logger = LoggerFactory.getLogger(CustomNotifier.class); private RestTemplate restTemplate; //閾值 private static Double cpuThreshold = 50.0;//CPU使用率閾值 private static Double jvmMemoryThreshold = 50.0;//內(nèi)存使用率閾值 //使用率 private static Double cpuUsage = 0.0;//CPU 使用率 private static Double jvmMemoryUsage = 0.0;//jvm內(nèi)存使用率 //所屬環(huán)境 private static String environment; static { if (EnvironmentUtil.isProdProfile()) {//生產(chǎn)環(huán)境 environment = "生產(chǎn)環(huán)境"; } else { environment = "測(cè)試環(huán)境"; } } @Resource private JavaMailSender mailSender; public CustomNotifier(InstanceRepository repository, RestTemplate restTemplate) { super(repository); this.restTemplate = restTemplate; } @Override protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { return Mono.fromRunnable(() -> { try { if (event instanceof InstanceRegisteredEvent) {//新實(shí)例加入事件 handleEvent(instance, "1"); logger.info("新實(shí)例加入事件,實(shí)例名:{}", instance.getRegistration().getName()); } else if (event instanceof InstanceDeregisteredEvent) {//實(shí)例關(guān)閉或注銷事件 handleEvent(instance, "2"); logger.info("實(shí)例關(guān)閉或注銷事件,實(shí)例名:{}", instance.getRegistration().getName()); } else if (event instanceof InstanceStatusChangedEvent) {//實(shí)例健康狀態(tài)改變事件 handleEvent(instance, "3"); logger.info("實(shí)例健康狀態(tài)改變事件,實(shí)例名:{}", instance.getRegistration().getName()); } else if (event instanceof InstanceInfoChangedEvent) {//實(shí)例信息更新事件 handleEvent(instance, "4"); logger.info("實(shí)例信息更新事件,實(shí)例名:{}", instance.getRegistration().getName()); } else { handleEvent(instance, "5");//其他事件 logger.info("其他事件,實(shí)例名:{}", instance.getRegistration().getName()); } } catch (Exception e) { logger.info("監(jiān)控平臺(tái)異常:" + e.getMessage()); } }); } private void handleEvent(Instance instance, String type) { String alertMessage = ""; /*****************************************1.收集信息************************************************************/ String applicationName = instance.getRegistration().getName(); //狀態(tài) String status = instance.getStatusInfo().getStatus(); //獲取Metrics信息 Map<String, Object> allMetrics = new HashMap<>(); try { // 獲取 /metrics 基本 URL String baseMetricUrl = instance.getRegistration().getServiceUrl() + "/actuator/metrics"; // 獲取所有 metrics 名稱 Map<String, Object> metricsResponse = restTemplate.getForObject(baseMetricUrl, Map.class); if (metricsResponse == null||metricsResponse.isEmpty()) { throw new RuntimeException("獲取metrics端點(diǎn)信息為null"); } List<String> nameList = (List) metricsResponse.get("names"); for (String metricName : nameList) { String metricUrl = baseMetricUrl + "/" + metricName; Map<String, Object> metricDetails = restTemplate.getForObject(metricUrl, Map.class); allMetrics.put(metricName, metricDetails); } Double jvmMemoryUsed = 0.0; Double jvmMemoryMax = 0.0; Map<String, Object> memoryUsedMap = (Map<String, Object>) allMetrics.get("jvm.memory.used");//當(dāng)前使用的 JVM 內(nèi)存量 Map<String, Object> memoryMaxMap = (Map<String, Object>) allMetrics.get("jvm.memory.max");//JVM 可用的最大內(nèi)存量 if (memoryUsedMap != null && memoryMaxMap != null) { // 獲取內(nèi)存使用和最大值的 measurements List<Map<String, Object>> memoryUsedMeasurements = (List<Map<String, Object>>) memoryUsedMap.get("measurements"); List<Map<String, Object>> memoryMaxMeasurements = (List<Map<String, Object>>) memoryMaxMap.get("measurements"); if (memoryUsedMeasurements != null && !memoryUsedMeasurements.isEmpty() && memoryMaxMeasurements != null && !memoryMaxMeasurements.isEmpty()) { jvmMemoryUsed = (Double) memoryUsedMeasurements.get(0).get("value"); jvmMemoryMax = (Double) memoryMaxMeasurements.get(0).get("value"); } } // 計(jì)算內(nèi)存使用率 jvmMemoryUsage = (jvmMemoryUsed / jvmMemoryMax) * 100; logger.info("JVM 內(nèi)存使用率: {}%", jvmMemoryUsage); // CPU 使用率 Map<String, Object> cpuUsageMap = (Map<String, Object>) allMetrics.get("system.cpu.usage"); if (cpuUsageMap != null) { List<Map<String, Object>> cpuUsageMeasurements = (List<Map<String, Object>>) cpuUsageMap.get("measurements"); if (cpuUsageMeasurements != null && !cpuUsageMeasurements.isEmpty()) { cpuUsage = (Double) cpuUsageMeasurements.get(0).get("value") * 100; } } logger.info("系統(tǒng) CPU 使用率: {}%", cpuUsage); /*****************************************2.組裝輸出信息************************************************************/ alertMessage = "\n應(yīng)用ID:" + instance.getId() + "\n應(yīng)用名:" + applicationName + "\n應(yīng)用名:" + applicationName + "\n狀態(tài):" + status + "\n事件類型:" + type + "\n實(shí)例注冊(cè)名稱:" + instance.getRegistration().getName() + "\n管理URL:" + instance.getRegistration().getManagementUrl() + "\n健康URL:" + instance.getRegistration().getHealthUrl() + "\n當(dāng)前使用的JVM內(nèi)存量(MB):" + Math.round(jvmMemoryUsed/ (1024*1024) * 100) / 100.0+ "\nJVM可用的最大內(nèi)存量(MB):" + Math.round(jvmMemoryMax / (1024*1024) * 100) / 100.0+ "\nJVM內(nèi)存使用率:" + Math.round(jvmMemoryUsage * 100) / 100.0+ "\nCPU使用率:" + Math.round(cpuUsage * 100) / 100.0 + "\nmetrics端點(diǎn)JSON:" + JSON.toJSONString(allMetrics) ; logger.info(alertMessage); } catch (Exception e) { logger.error("獲取 Metrics 端點(diǎn)信息錯(cuò)誤: ", e); } /*****************************************3.根據(jù)情況發(fā)送郵件************************************************************/ if ("2".equals(type)) { sendEmail(environment + "監(jiān)控告警]-實(shí)例關(guān)閉或注銷事件:" + applicationName, alertMessage, null); } if (cpuUsage > cpuThreshold) { sendEmail(environment + "監(jiān)控告警]-系統(tǒng)CPU使用率達(dá)到" + cpuUsage + "%告警:" + applicationName, alertMessage, null); } if (jvmMemoryUsage > jvmMemoryThreshold) { sendEmail(environment + "監(jiān)控告警]-JVM內(nèi)存使用率達(dá)到" + jvmMemoryUsage + "%告警:" + applicationName, alertMessage, null); } } private void sendEmail(String title, String body, String toMail) { try { SimpleMailMessage message = new SimpleMailMessage(); if (StringUtils.isEmpty(toMail)) { toMail = MailUtil.MAIL_SMTP_TO; } message.setTo(toMail.split(",")); // 收件人 message.setFrom(MailUtil.MAIL_SMTP_USER); message.setSubject(title); message.setText(body); logger.info("發(fā)送告警郵件:", message.getSubject() + ",TO:" + message.getTo()); mailSender.send(message); } catch (Exception e) { logger.error("發(fā)送郵件失敗,失敗信息:" + e.getMessage()); } } /** * 定義信息到/actuator/health端點(diǎn),向/actuator/info端點(diǎn)自定義信息可以繼承InfoContributor接口 * HealthIndicator 是一個(gè)用于提供應(yīng)用健康檢查的接口 * HealthIndicator 會(huì)被 Spring Boot Actuator 自動(dòng)掃描,并在 /actuator/health 端點(diǎn)上暴露。 * 訪問 /actuator/health * { * "status": "UP", * "details": { * "maxMemory": 4294967296, * "totalMemory": 104857600, * "freeMemory": 73741824, * "usedMemory": 31115776 * } * } * * @return */ @Override public Health health() { // 獲取當(dāng)前JVM內(nèi)存使用情況 Runtime runtime = Runtime.getRuntime(); /**獲取當(dāng)前JVM 內(nèi)存使用情況**/ MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage(); /**獲取垃圾回收器信息**/ List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans(); StringBuilder gcInfo = new StringBuilder(); for (GarbageCollectorMXBean gcBean : garbageCollectorMXBeans) { gcInfo.append(gcBean.getName()) .append(": ") .append("Collection count: ").append(gcBean.getCollectionCount() == -1 ? "N/A" : gcBean.getCollectionCount()) .append(", Collection time (ms): ").append(gcBean.getCollectionTime() == -1 ? "N/A" : gcBean.getCollectionTime()) .append("\n"); } /**獲取線程信息**/ ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); /**獲取操作系統(tǒng)信息**/ OperatingSystemMXBean osMXBean = ManagementFactory.getOperatingSystemMXBean(); // 檢查內(nèi)存使用情況是否超出了閾值 long usedMemory = runtime.totalMemory() - runtime.freeMemory(); return Health.up() .withDetail("JVM堆最大內(nèi)存", runtime.maxMemory()) .withDetail("JVM堆總內(nèi)存", runtime.totalMemory())//返回當(dāng)前 JVM 堆的總內(nèi)存量,代碼所在jvm .withDetail("JVM堆空閑的內(nèi)存量", runtime.freeMemory())//返回當(dāng)前 JVM 堆中空閑的內(nèi)存量 .withDetail("JVM堆已用內(nèi)存", usedMemory) .withDetail("非堆內(nèi)存(Non-Heap)內(nèi)存使用", nonHeapMemoryUsage.getUsed() + "/" + Math.round(nonHeapMemoryUsage.getMax())) .withDetail("堆(Heap)內(nèi)存使用", heapMemoryUsage.getUsed() + "/" + Math.round(heapMemoryUsage.getMax())) .withDetail("可用處理器", runtime.availableProcessors()) .withDetail("線程信息", threadMXBean.getThreadCount()) .withDetail("峰值線程數(shù)", threadMXBean.getPeakThreadCount()) .withDetail("總啟動(dòng)線程數(shù)", threadMXBean.getTotalStartedThreadCount()) .withDetail("操作系統(tǒng)可用處理器", osMXBean.getAvailableProcessors()) .withDetail("系統(tǒng)負(fù)載平均值", osMXBean.getSystemLoadAverage()) .withDetail("GC信息", gcInfo) .build(); } }
其他配置信息
@Bean public RestTemplate restTemplate() { return new RestTemplate(); }
在 CustomNotifier 類中,doNotify 方法的 Instance 參數(shù)代表了一個(gè)注冊(cè)的應(yīng)用實(shí)例的詳細(xì)信息。理解 Instance 的結(jié)構(gòu)和內(nèi)容可以幫助你在處理通知時(shí)更好地利用這些數(shù)據(jù)。
Instance 的主要字段,你可以訪問 Instance 對(duì)象來獲取有關(guān)應(yīng)用實(shí)例的信息,以便在通知中使用。例如,你可以:
- 獲取實(shí)例的基本信息:如 name、id 等,來確定是哪個(gè)實(shí)例觸發(fā)了事件。
- 檢查實(shí)例的狀態(tài):通過 statusInfo 和 status 字段,你可以了解實(shí)例的健康狀況,并據(jù)此做出通知決策。
- 使用注冊(cè)信息:例如,managementUrl 和 healthUrl,可以用于構(gòu)建通知內(nèi)容或執(zhí)行進(jìn)一步的檢查。
- 訪問實(shí)例的端點(diǎn)信息:例如,你可以查看哪些 Actuator 端點(diǎn)被暴露,并利用這些端點(diǎn)進(jìn)行更深入的監(jiān)控或操作。
3.1 Instance 對(duì)象
以下是 Instance 對(duì)象的一些關(guān)鍵字段及其含義:
id:
唯一標(biāo)識(shí)該實(shí)例的 ID。
version:
該實(shí)例的版本號(hào)。
registration:
實(shí)例注冊(cè)信息,包括:
name: 實(shí)例的名稱。
managementUrl: 用于管理的 URL(例如 /actuator 的 URL)。
healthUrl: 用于健康檢查的 URL。
serviceUrl: 服務(wù)的基礎(chǔ) URL。
source: 實(shí)例的注冊(cè)來源(例如 discovery)。
metadata: 與實(shí)例相關(guān)的元數(shù)據(jù),如心跳超時(shí)、Nacos 配置等。
registered:
布爾值,指示實(shí)例是否已注冊(cè)。
statusInfo:
實(shí)例的狀態(tài)信息,包括:
status: 實(shí)例的健康狀態(tài)(例如 UP、DOWN)。
details: 包含詳細(xì)的狀態(tài)信息,可能包括:sentinel, elasticsearch, diskSpace, ping, discoveryComposite, refreshScope, nacosDiscovery, db, redis 等。每個(gè)狀態(tài)組件可能有 status 和 details,這些詳細(xì)信息可以包括服務(wù)的健康狀況、性能指標(biāo)等。
statusTimestamp:
實(shí)例狀態(tài)的時(shí)間戳。
info:
實(shí)例的額外信息,通常是應(yīng)用程序的自定義信息。
endpoints:
實(shí)例暴露的 Actuator 端點(diǎn)的列表,包括每個(gè)端點(diǎn)的 ID 和 URL(例如 /actuator/health、/actuator/metrics)。
tags:
實(shí)例的標(biāo)簽,用于標(biāo)識(shí)或分類實(shí)例。
三.Spring Boot Admin支持的監(jiān)控屬性
在Spring Boot Admin中,你可以監(jiān)控應(yīng)用程序的各種指標(biāo)。Spring Boot Admin通過Spring Boot Actuator提供的端點(diǎn)來收集這些監(jiān)控信息。下面是Spring Boot Admin支持的監(jiān)控屬性的詳細(xì)列表,它們通常包括系統(tǒng)的健康狀態(tài)、度量數(shù)據(jù)、環(huán)境信息等。
1.常見的Spring Boot Admin監(jiān)控屬性
1. Health
status: 應(yīng)用程序的整體健康狀態(tài) (UP, DOWN, OUT_OF_SERVICE, UNKNOWN)。
details: 各個(gè)健康檢查的詳細(xì)信息,如數(shù)據(jù)庫(kù)、消息中間件等。
2. Metrics
jvm.memory.used: 已使用的JVM內(nèi)存。
jvm.memory.max: 最大JVM內(nèi)存。
jvm.memory.committed: 已提交的JVM內(nèi)存。
jvm.gc.total: JVM垃圾回收總數(shù)。
jvm.gc.time: JVM垃圾回收時(shí)間。
system.cpu.usage: 系統(tǒng)CPU使用率。
system.cpu.load: 系統(tǒng)CPU負(fù)載。
disk.space.free: 磁盤可用空間。
disk.space.total: 磁盤總空間。
disk.space.used: 磁盤已用空間。
3. Environment
properties: 環(huán)境屬性,如server.port, spring.datasource.url等。
configurations: 應(yīng)用程序配置文件的詳細(xì)信息。
beans: Spring上下文中定義的bean信息。
4. Info
app.version: 應(yīng)用程序的版本。
app.description: 應(yīng)用程序的描述。
build.version: 構(gòu)建版本。
build.name: 構(gòu)建名稱。
build.time: 構(gòu)建時(shí)間。
git.commit.id: Git提交ID。
git.commit.time: Git提交時(shí)間。
git.branch: Git分支。
5. Loggers
loggers: 各個(gè)日志記錄器的級(jí)別(例如DEBUG, INFO, WARN, ERROR)。
logging.level: 各個(gè)包的日志級(jí)別配置。
6. Threads
thread.count: 當(dāng)前活動(dòng)線程的數(shù)量。
thread.states: 各種線程狀態(tài)的計(jì)數(shù)(例如RUNNABLE, WAITING, TIMED_WAITING, BLOCKED)。
thread.details: 線程的詳細(xì)信息。
7. Caches
caches: 相關(guān)緩存的信息,如緩存名稱、大小等。
8. Heap Dump
heap: 堆轉(zhuǎn)儲(chǔ)的詳細(xì)信息,包括各個(gè)類的實(shí)例和內(nèi)存占用情況。
9. Dump
dump: 應(yīng)用程序的線程轉(zhuǎn)儲(chǔ)信息。
10. Trace
trace: 跟蹤信息,通常用于調(diào)試目的。
配置Spring Boot Admin以顯示這些屬性
確保你的application.properties或application.yml中配置了Actuator的相關(guān)端點(diǎn),以便Spring Boot Admin能夠獲取這些監(jiān)控信息。例如:
management.endpoints.web.exposure.include=health,metrics,info,env,loggers,threaddump management.endpoint.health.show-details=always
https://docs.spring.io/spring-boot/reference/actuator/endpoints.html
2.端點(diǎn)接口
1. /actuator/health
狀態(tài)信息 (statusInfo):包括應(yīng)用程序的健康狀態(tài)(如 UP、DOWN),以及各種健康檢查的詳細(xì)信息(例如數(shù)據(jù)庫(kù)、緩存、磁盤空間的健康狀況)。這對(duì)應(yīng)于 Instance 對(duì)象的 statusInfo 字段。
{ "status": "UP", "details": { "diskSpace": { "status": "UP", "details": { "total": 51510251520, "free": 35968552960 } } } }
2. /actuator/info
額外信息 (info):通常用于返回應(yīng)用程序的元數(shù)據(jù),例如版本信息、構(gòu)建信息等。對(duì)應(yīng) Instance 對(duì)象的 info 字段。示例:
{ "app": "my-app", "version": "1.0.0" }
3. /actuator/env
環(huán)境信息:提供有關(guān)應(yīng)用程序的環(huán)境屬性的詳細(xì)信息。這不是直接包含在 Instance 對(duì)象中的字段,但可以在 info 中看到。 示例:
{ "propertySources": [ { "name": "systemProperties", "properties": { "java.version": "1.8.0_212" } } ] }
4. /actuator/metrics
性能指標(biāo):提供關(guān)于應(yīng)用程序性能的詳細(xì)數(shù)據(jù)(例如內(nèi)存使用情況、垃圾回收信息等)。這些信息可以用來補(bǔ)充 statusInfo 中的性能數(shù)據(jù),但通常 metrics 不直接映射到 Instance 的字段中。示例:
{ "jvm.memory.used": 12345678, "jvm.memory.max": 98765432 }
5. /actuator/mappings
端點(diǎn)映射:提供所有控制器端點(diǎn)的映射信息。這可以與 Instance 的 endpoints 字段相關(guān)聯(lián)。 示例:
{ "controllers": { "home": "/home", "admin": "/admin" } }
6. /actuator/beans
Bean 信息:列出所有 Spring Bean 和其配置。這有助于調(diào)試和了解應(yīng)用程序的內(nèi)部結(jié)構(gòu),雖然它通常不直接映射到 Instance 對(duì)象的字段中。示例:
{ "beans": [ { "name": "dataSource", "type": "javax.sql.DataSource" } ] }
這些端點(diǎn)提供了不同類型的監(jiān)控和管理信息,可以幫助你從 Actuator 中提取所需的數(shù)據(jù)
3.訪問示例
訪問:http://localhost:9000/actuator
{ "_links": { "self": { "href": "http://localhost:9000/actuator", "templated": false }, "archaius": { "href": "http://localhost:9000/actuator/archaius", "templated": false }, "nacosdiscovery": { "href": "http://localhost:9000/actuator/nacosdiscovery", "templated": false }, "sentinel": { "href": "http://localhost:9000/actuator/sentinel", "templated": false }, "beans": { "href": "http://localhost:9000/actuator/beans", "templated": false }, "caches-cache": { "href": "http://localhost:9000/actuator/caches/{cache}", "templated": true }, "caches": { "href": "http://localhost:9000/actuator/caches", "templated": false }, "health": { "href": "http://localhost:9000/actuator/health", "templated": false }, "health-path": { "href": "http://localhost:9000/actuator/health/{*path}", "templated": true }, "info": { "href": "http://localhost:9000/actuator/info", "templated": false }, "conditions": { "href": "http://localhost:9000/actuator/conditions", "templated": false }, "configprops": { "href": "http://localhost:9000/actuator/configprops", "templated": false }, "env-toMatch": { "href": "http://localhost:9000/actuator/env/{toMatch}", "templated": true }, "env": { "href": "http://localhost:9000/actuator/env", "templated": false }, "loggers": { "href": "http://localhost:9000/actuator/loggers", "templated": false }, "loggers-name": { "href": "http://localhost:9000/actuator/loggers/{name}", "templated": true }, "heapdump": { "href": "http://localhost:9000/actuator/heapdump", "templated": false }, "threaddump": { "href": "http://localhost:9000/actuator/threaddump", "templated": false }, "metrics-requiredMetricName": { "href": "http://localhost:9000/actuator/metrics/{requiredMetricName}", "templated": true }, "metrics": { "href": "http://localhost:9000/actuator/metrics", "templated": false }, "scheduledtasks": { "href": "http://localhost:9000/actuator/scheduledtasks", "templated": false }, "mappings": { "href": "http://localhost:9000/actuator/mappings", "templated": false }, "refresh": { "href": "http://localhost:9000/actuator/refresh", "templated": false }, "features": { "href": "http://localhost:9000/actuator/features", "templated": false }, "service-registry": { "href": "http://localhost:9000/actuator/service-registry", "templated": false } } }
訪問http://localhost:9000/actuator/metrics
{ "names": [ "http.server.requests", "jvm.buffer.count", "jvm.buffer.memory.used", "jvm.buffer.total.capacity", "jvm.classes.loaded", "jvm.classes.unloaded", "jvm.gc.live.data.size", "jvm.gc.max.data.size", "jvm.gc.memory.allocated", "jvm.gc.memory.promoted", "jvm.gc.pause", "jvm.memory.committed", "jvm.memory.max", "jvm.memory.used", "jvm.threads.daemon", "jvm.threads.live", "jvm.threads.peak", "jvm.threads.states", "logback.events", "process.cpu.usage", "process.files.max", "process.files.open", "process.start.time", "process.uptime", "system.cpu.count", "system.cpu.usage", "system.load.average.1m", "tomcat.sessions.active.current", "tomcat.sessions.active.max", "tomcat.sessions.alive.max", "tomcat.sessions.created", "tomcat.sessions.expired", "tomcat.sessions.rejected" ] }
訪問具體的參數(shù)可以得到具體的值,比如訪問/actuator/metrics/jvm.memory.used
訪問http://localhost:9000/applications
Spring Boot Admin 的 /applications 接口 獲取的。該接口返回當(dāng)前在 Spring Boot Admin 中注冊(cè)的所有應(yīng)用程序的信息,包括每個(gè)應(yīng)用的詳細(xì)實(shí)例數(shù)據(jù),例如 health、status、managementUrl、endpoints 等。
name: 應(yīng)用程序的名稱,status: 應(yīng)用程序的整體狀態(tài),通常是 UP 或 DOWN。
instances: 應(yīng)用程序的所有實(shí)例信息。
id: 實(shí)例的唯一標(biāo)識(shí)符。
statusInfo: 包含實(shí)例的詳細(xì)狀態(tài)信息,例如 Sentinel、Elasticsearch、DiskSpace、Redis 等相關(guān)服務(wù)的狀態(tài)。
endpoints: 列舉了實(shí)例暴露的 Actuator 端點(diǎn),例如 /actuator/health, /actuator/metrics 等。
{ "name": "YIXIEKEJI-MEMBER", "buildVersion": null, "status": "UP", "statusTimestamp": "2024-09-03T05:18:26.492Z", "instances": [ { "id": "xxx", "version": 2, "registration": { "name": "YIXIEKEJI-MEMBER", "managementUrl": "http://IP:PORT/actuator", "healthUrl": "http://IP:PORT/actuator/health", "serviceUrl": "http://IP:PORT", "source": "discovery", "metadata": { "preserved.heart.beat.timeout": "5000", "nacos.instanceId": "xxxxxx", "nacos.weight": "1.0", "nacos.cluster": "DEFAULT", "nacos.ephemeral": "true", "nacos.healthy": "true", "preserved.register.source": "SPRING_CLOUD", "preserved.heart.beat.interval": "5000" } }, "registered": true, "statusInfo": { "status": "UP", "details": { "diskSpace": { "status": "UP", "details": { "total": 11111, "free": 11111, "threshold": 11111, "exists": true } }, "ping": { "status": "UP" } } }, "statusTimestamp": "2024-09-03T05:18:26.492Z", "info": { }, "endpoints": [ { "id": "sentinel", "url": "http://IP:PORT/actuator/sentinel" }, { "id": "caches", "url": "http://IP:PORT/actuator/caches" }, { "id": "loggers", "url": "http://IP:PORT/actuator/loggers" }, { "id": "health", "url": "http://IP:PORT/actuator/health" }, { "id": "refresh", "url": "http://IP:PORT/actuator/refresh" }, { "id": "env", "url": "http://IP:PORT/actuator/env" }, { "id": "nacosdiscovery", "url": "http://IP:PORT/actuator/nacosdiscovery" }, { "id": "heapdump", "url": "http://IP:PORT/actuator/heapdump" }, { "id": "features", "url": "http://IP:PORT/actuator/features" }, { "id": "scheduledtasks", "url": "http://IP:PORT/actuator/scheduledtasks" }, { "id": "mappings", "url": "http://IP:PORT/actuator/mappings" }, { "id": "archaius", "url": "http://IP:PORT/actuator/archaius" }, { "id": "beans", "url": "http://IP:PORT/actuator/beans" }, { "id": "configprops", "url": "http://IP:PORT/actuator/configprops" }, { "id": "threaddump", "url": "http://IP:PORT/actuator/threaddump" }, { "id": "metrics", "url": "http://IP:PORT/actuator/metrics" }, { "id": "conditions", "url": "http://IP:PORT/actuator/conditions" }, { "id": "service-registry", "url": "http://IP:PORT/actuator/service-registry" }, { "id": "info", "url": "http://IP:PORT/actuator/info" } ], "buildVersion": null, "tags": { } } ] }
參考資料:
https://juejin.cn/post/6956483441227464740
開始使用Spring Boot Admin吧-使用Nacos注冊(cè)SBA_spring boot admin nacos
springBoot Admin整合nacos_springboot admin nacos
到此這篇關(guān)于Spring Boot Admin集成與自定義監(jiān)控告警的文章就介紹到這了,更多相關(guān)Spring Boot Admin自定義監(jiān)控告警內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot配置MySQL數(shù)據(jù)庫(kù)連接、Hikari連接池和Mybatis的簡(jiǎn)單配置方法
這篇文章主要介紹了spring boot配置MySQL數(shù)據(jù)庫(kù)連接、Hikari連接池和Mybatis的簡(jiǎn)單配置方法,需要的朋友可以參考下2018-03-03java實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06mybatis條件語句中帶數(shù)組參數(shù)的處理
這篇文章主要介紹了mybatis條件語句中帶數(shù)組參數(shù)的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java使用Socket實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了java使用Socket實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02關(guān)于Java中阻塞隊(duì)列BlockingQueue的詳解
這篇文章主要介紹了關(guān)于Java中阻塞隊(duì)列BlockingQueue的詳解,BlockingQueue是為了解決多線程中數(shù)據(jù)高效安全傳輸而提出的,從阻塞這個(gè)詞可以看出,在某些情況下對(duì)阻塞隊(duì)列的訪問可能會(huì)造成阻塞,需要的朋友可以參考下2023-05-05SpringBoot中實(shí)現(xiàn)分布式的Session共享的詳細(xì)教程
這篇文章主要介紹了SpringBoot中實(shí)現(xiàn)分布式的Session共享,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Springboot整合Thymeleaf引入公共的CSS和JS文件的方法及注意點(diǎn)
有時(shí)候很多css文件是公共的,我們必須要在每個(gè)html文件中引入它們,下面這篇文章主要給大家介紹了關(guān)于Springboot整合Thymeleaf引入公共的CSS和JS文件的方法及注意點(diǎn),需要的朋友可以參考下2024-06-06