0
点赞
收藏
分享

微信扫一扫

使用 SpringBoot + WebSocket,实现实时监控异常

在现代系统开发中,实时监控异常对于系统的稳定性和问题排查至关重要。本篇文章将介绍如何通过 SpringBoot 与 WebSocket 的结合,快速构建一个实时监控异常的系统。

为什么选择 WebSocket?

WebSocket 是一种全双工通信协议,适用于需要实时通信的场景,例如实时监控、消息推送等。它的特点包括:

  • 高效:基于 TCP 的全双工通信,减少了传统 HTTP 轮询的延迟和带宽浪费。
  • 实时性:服务端可以随时向客户端推送数据,无需客户端主动请求。
  • 简单易用:支持主流的开发框架和浏览器。

实现实时异常监控的核心步骤

1. 创建 SpringBoot 项目

使用 Spring Initializr 创建一个 SpringBoot 项目,选择以下依赖:

  • Web
  • WebSocket
  • Spring Boot Starter

2. 配置 WebSocket

创建一个 WebSocket 配置类,用于初始化和注册 WebSocket 的端点。

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new ExceptionWebSocketHandler(), "/exception-monitor")
                .setAllowedOrigins("*");
    }
}

3. 创建 WebSocket 处理器

实现 WebSocketHandler 接口,用于处理连接和消息。

import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.util.concurrent.CopyOnWriteArraySet;

public class ExceptionWebSocketHandler extends TextWebSocketHandler {

    private static final CopyOnWriteArraySet<WebSocketSession> sessions = new CopyOnWriteArraySet<>();

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        sessions.add(session);
        session.sendMessage(new TextMessage("Connection established."));
    }

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        System.out.println("Received message: " + message.getPayload());
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        sessions.remove(session);
    }

    public static void broadcast(String message) {
        for (WebSocketSession session : sessions) {
            try {
                if (session.isOpen()) {
                    session.sendMessage(new TextMessage(message));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

4. 模拟异常日志的生成和推送

创建一个服务类,用于监听异常并通过 WebSocket 广播到客户端。

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Random;

@Service
public class ExceptionMonitorService {

    private final Random random = new Random();

    @Scheduled(fixedRate = 5000)
    public void simulateException() {
        if (random.nextBoolean()) {
            String exceptionLog = "[ERROR] " + LocalDateTime.now() + " - Simulated exception occurred.";
            ExceptionWebSocketHandler.broadcast(exceptionLog);
            System.out.println("Broadcasting: " + exceptionLog);
        }
    }
}

5. 启用定时任务

在主应用类上添加 @EnableScheduling 注解以启动定时任务。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class WebSocketExceptionMonitorApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebSocketExceptionMonitorApplication.class, args);
    }
}

6. 创建客户端页面

用 HTML 和 JavaScript 构建一个简单的 WebSocket 客户端。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Exception Monitor</title>
</head>
<body>
    <h1>Exception Monitor</h1>
    <ul id="log"></ul>

    <script>
        const socket = new WebSocket("ws://localhost:8080/exception-monitor");
        const log = document.getElementById("log");

        socket.onmessage = function(event) {
            const li = document.createElement("li");
            li.textContent = event.data;
            log.appendChild(li);
        };

        socket.onopen = function() {
            console.log("Connection established");
        };

        socket.onclose = function() {
            console.log("Connection closed");
        };
    </script>
</body>
</html>

效果展示

  • 启动服务后,浏览器访问 HTML 页面。
  • 每 5 秒自动检测并广播模拟的异常日志。
  • 实时在页面显示异常信息。

总结

通过 SpringBoot 与 WebSocket 的结合,可以轻松实现实时异常监控系统。相比传统的日志文件或定时轮询,WebSocket 提供了更高效和直观的解决方案。通过扩展,这种架构还可以支持更多的实时监控场景。

举报

相关推荐

0 条评论