最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 整合 SSE的高級(jí)實(shí)踐(Server-Sent Events)

 更新時(shí)間:2025年04月25日 10:53:00   作者:小馬不敲代碼  
SSE(Server-Sent Events)是一種基于HTTP協(xié)議的單向通信機(jī)制,允許服務(wù)器向?yàn)g覽器持續(xù)發(fā)送實(shí)時(shí)更新,這篇文章主要介紹了Spring Boot 整合 SSE的高級(jí)實(shí)踐(Server-Sent Events),需要的朋友可以參考下

1、簡(jiǎn)述

SSE(Server-Sent Events)是一種基于HTTP協(xié)議的單向通信機(jī)制,允許服務(wù)器向?yàn)g覽器持續(xù)發(fā)送實(shí)時(shí)更新。與WebSocket不同,SSE更簡(jiǎn)單,使用HTTP/1.1協(xié)議即可,不需要額外的協(xié)議升級(jí)。

SSE的特點(diǎn):

  • 單向通信:服務(wù)器推送數(shù)據(jù)給客戶端,客戶端無法向服務(wù)器發(fā)送消息。
  • 簡(jiǎn)單易用:基于HTTP協(xié)議,無需復(fù)雜的配置。
  • 瀏覽器支持:現(xiàn)代瀏覽器大多內(nèi)置支持(如Chrome、Edge、Firefox等)。

2、Spring Boot 中的SSE實(shí)現(xiàn)

2.1 添加依賴

SSE無需額外的依賴,Spring Boot自帶對(duì)SSE的支持。創(chuàng)建一個(gè)Spring Boot項(xiàng)目即可。

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

2.2 實(shí)現(xiàn)后端接口

使用MediaType.TEXT_EVENT_STREAM_VALUE作為返回類型即可開啟SSE。以下代碼是一個(gè)簡(jiǎn)單的實(shí)現(xiàn)。

package com.example.sse.controller;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@RestController
public class SseController {
    @GetMapping(value = "/sse/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Stream<String> stream() {
        // 模擬數(shù)據(jù)流
        return Stream.generate(() -> "當(dāng)前時(shí)間:" + LocalTime.now())
                     .limit(10); // 限制10條消息
    }
}

2.3 配置超時(shí)時(shí)間(可選)

默認(rèn)情況下,Spring Boot的響應(yīng)會(huì)超時(shí)??梢栽?code>application.properties中調(diào)整超時(shí)時(shí)間:

server.servlet.session.timeout=30s
spring.mvc.async.request-timeout=30000

2.4 前端實(shí)現(xiàn)

SSE在前端通過EventSource對(duì)象實(shí)現(xiàn)。以下是一個(gè)簡(jiǎn)單的前端示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SSE Example</title>
</head>
<body>
    <h1>實(shí)時(shí)消息</h1>
    <div id="messages"></div>
    <script>
        const eventSource = new EventSource('/sse/stream');
        eventSource.onmessage = function(event) {
            const messagesDiv = document.getElementById('messages');
            const newMessage = document.createElement('p');
            newMessage.textContent = event.data;
            messagesDiv.appendChild(newMessage);
        };
        eventSource.onerror = function() {
            console.error('SSE連接出錯(cuò),正在嘗試重連...');
            eventSource.close();
        };
    </script>
</body>
</html>

3、高級(jí)實(shí)踐

使用Spring Scheduler推送數(shù)據(jù),在實(shí)際場(chǎng)景中,可能需要定時(shí)向客戶端推送數(shù)據(jù)。例如,監(jiān)控系統(tǒng)定時(shí)更新。

package com.example.sse.service;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;
@Service
public class SsePushService {
    private final CopyOnWriteArrayList<SseEmitter> emitters = new CopyOnWriteArrayList<>();
    public SseEmitter subscribe() {
        SseEmitter emitter = new SseEmitter(30_000L);
        emitters.add(emitter);
        emitter.onCompletion(() -> emitters.remove(emitter));
        emitter.onTimeout(() -> emitters.remove(emitter));
        return emitter;
    }
    public void pushMessage(String message) {
        for (SseEmitter emitter : emitters) {
            try {
                emitter.send(message, MediaType.TEXT_PLAIN);
            } catch (IOException e) {
                emitters.remove(emitter);
            }
        }
    }
}

創(chuàng)建一個(gè)控制器訂閱和推送消息:

package com.example.sse.controller;
import com.example.sse.service.SsePushService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SsePushController {
    private final SsePushService ssePushService;
    public SsePushController(SsePushService ssePushService) {
        this.ssePushService = ssePushService;
    }
    @GetMapping("/sse/subscribe")
    public SseEmitter subscribe() {
        return ssePushService.subscribe();
    }
    @GetMapping("/sse/push")
    public void pushMessage() {
        ssePushService.pushMessage("當(dāng)前時(shí)間:" + System.currentTimeMillis());
    }
}

注意事項(xiàng):

  • 瀏覽器兼容性:SSE不支持IE,但現(xiàn)代瀏覽器支持良好。
  • 連接斷開處理:可通過EventSourceonerror事件重新連接。
  • 性能問題:對(duì)大量訂閱者時(shí),需考慮使用分布式消息隊(duì)列優(yōu)化(如Kafka)。
  • 超時(shí)時(shí)間:默認(rèn)30秒超時(shí),需要根據(jù)實(shí)際需求調(diào)整。

4、適用場(chǎng)景

  • 實(shí)時(shí)通知:如監(jiān)控系統(tǒng)的告警推送。
  • 實(shí)時(shí)更新:如股票行情、體育比分。
  • 消息流:如系統(tǒng)日志、任務(wù)進(jìn)度。

到此這篇關(guān)于Spring Boot 整合 SSE(Server-Sent Events)的文章就介紹到這了,更多相關(guān)Spring Boot 整合 SSE內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

丹江口市| 湄潭县| 茂名市| 漳浦县| 鹤峰县| 建平县| 宁晋县| 兴化市| 商南县| 镇安县| 囊谦县| 宁津县| 昌乐县| 龙南县| 繁峙县| 赤峰市| 长春市| 遂川县| 哈尔滨市| 嘉定区| 富宁县| 大英县| 河源市| 鹰潭市| 土默特右旗| 拉孜县| 边坝县| 鄂尔多斯市| 承德市| 诸城市| 阜南县| 荆门市| 井陉县| 上林县| 安塞县| 中西区| 肃宁县| 白朗县| 海口市| 米泉市| 漾濞|