【SpringBoot】Spring Boot實現SSE實時推送實戰

以下是一個完整的基于 Spring Boot 的 Server-Sent Events (SSE) 示例,包括服務端和客戶端的實現。

一、服務端實現

1. 創建 Spring Boot 項目

首先,創建一個基本的 Spring Boot 項目,并添加 spring-boot-starter-web 依賴。在 pom.xml 中添加以下內容:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
2. 創建 SSE 控制器

創建一個控制器來處理 SSE 連接并推送實時消息。

SseController.java

package com.example.sse;import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;@RestController
public class SseController {private final ExecutorService executorService = Executors.newCachedThreadPool();@GetMapping("/sse")public SseEmitter handleSse() {SseEmitter emitter = new SseEmitter();executorService.execute(() -> {try {for (int i = 0; i < 10; i++) {emitter.send("Message " + i, MediaType.TEXT_PLAIN);TimeUnit.SECONDS.sleep(1);}emitter.complete();} catch (IOException | InterruptedException e) {emitter.completeWithError(e);}});return emitter;}
}
3. 配置跨域(可選)

如果前端和后端運行在不同端口上,需要配置跨域。

CorsConfig.java

package com.example.sse;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class CorsConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*").allowCredentials(true);}
}

二、客戶端實現

在前端頁面中,使用 EventSource 來訂閱 SSE。

index.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>SSE Example</title>
</head>
<body><h1>Server-Sent Events Example</h1><div id="events"></div><script>const eventSource = new EventSource('/sse');eventSource.onmessage = function(event) {const newElement = document.createElement("div");newElement.innerHTML = "Message: " + event.data;document.getElementById("events").appendChild(newElement);};eventSource.onerror = function(event) {eventSource.close();alert("EventSource failed: " + event);};</script>
</body>
</html>

三、運行和測試

  1. 啟動 Spring Boot 應用。
  2. 在瀏覽器中訪問 http://localhost:8080,即可看到服務端每秒推送的消息。

四、擴展功能

1. 動態推送消息

可以通過維護一個 SseEmitter 的映射來動態推送消息。

SseController.java(動態推送版本)

package com.example.sse;import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;@RestController
public class SseController {private final Map<String, SseEmitter> emitterMap = new ConcurrentHashMap<>();@GetMapping("/sse/{userId}")public SseEmitter connect(@PathVariable String userId) {SseEmitter emitter = new SseEmitter();emitterMap.put(userId, emitter);emitter.onCompletion(() -> emitterMap.remove(userId));emitter.onTimeout(() -> emitterMap.remove(userId));emitter.onError(e -> emitterMap.remove(userId));return emitter;}@GetMapping("/push/{userId}")public void push(@PathVariable String userId, @RequestParam String message) {SseEmitter emitter = emitterMap.get(userId);if (emitter != null) {try {emitter.send(message);} catch (IOException e) {emitter.completeWithError(e);emitterMap.remove(userId);}}}
}
2. 使用 WebFlux 實現 SSE

如果需要更高效的響應式編程支持,可以使用 Spring WebFlux。

SseController.java(WebFlux 版本)

package com.example.sse;import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;import java.time.Duration;@RestController
public class SseController {@GetMapping("/sse/stream")public Flux<ServerSentEvent<String>> streamSse() {return Flux.interval(Duration.ofSeconds(1)).map(sequence -> ServerSentEvent.<String>builder().id(String.valueOf(sequence)).event("periodic-event").data("Current time: " + java.time.LocalTime.now()).build());}
}

通過以上步驟,你可以實現一個完整的基于 Spring Boot 的 SSE 應用。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/88035.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/88035.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/88035.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

若依導出模板時設置動態excel下拉框(表連接的)

若依導出模板時設置動態excel下拉框&#xff08;表連接的&#xff09; 一、問題二、解決1、實體類2.1、臨時使用2.2、統一工具類3、調用 一、問題 若依導出只能&#xff1b;使用dictType、combo、comboReadDict、readConverterExp這些來控制字典的導出下拉&#xff0c;如果不是…

Rabbitmq集成springboot 使用死信隊列

一、何為死信隊列 RabbitMQ的死信隊列&#xff08;Dead Letter Queue&#xff0c;DLQ&#xff09;是一種特殊的隊列機制&#xff0c;用于處理那些無法被正常消費的消息。這些消息可能由于各種原因無法被消費者正確處理&#xff0c;如果不加以處理&#xff0c;可能會導致隊列堵塞…

Spring Boot 項目中 resources 文件讀取

開發必備&#xff01;Spring Boot 項目中 resources 文件讀取的 9 大方案詳解 在 Spring Boot 項目中&#xff0c;resources 目錄承載著大量的關鍵資源&#xff0c;如配置文件、模板文件、腳本資源、數據文件等。而如何以合適的方式高效、安全地讀取這些資源&#xff0c;往往是…

力扣-1143.最長公共子序列

題目描述 給定兩個字符串 text1 和 text2&#xff0c;返回這兩個字符串的最長 公共子序列 的長度。如果不存在 公共子序列 &#xff0c;返回 0 。 一個字符串的 子序列 是指這樣一個新的字符串&#xff1a;它是由原字符串在不改變字符的相對順序的情況下刪除某些字符&#xf…

《算法筆記》之二(筆記)

1. vector&#xff1a; 1.定義&#xff1a;“變長數組”&#xff08;長度依據需要而自動改變&#xff0c;節省空間&#xff0c;避免普通數組超內存&#xff09; 代碼定義&#xff1a;vector < typename > name; 注&#xff1a;&#xff08;注意理解&#xff09; vecto…

PROFIBUS DP 轉 EtherCAT 網關:冶金自動化高效協同的基石

在冶金行業高爐、連鑄、軋鋼等復雜場景中&#xff0c;生產設備往往跨越不同時代。許多關鍵產線仍依賴西門子PLC為核心的PROFIBUS DP網絡&#xff0c;而新型伺服驅動器、機器人手臂則普遍采用高性能EtherCAT接口。如何實現新舊系統的無縫集成&#xff1f;JH-PB-ECT疆鴻智能PROFI…

開發云數據庫

1、云數據庫概述 云數據庫是一款端云協同的數據庫產品&#xff0c;是AGC云開發&#xff08;AGC Serverless&#xff09;關鍵服務之一&#xff0c;為AGC構建了MBaas&#xff08;Mobile Backend as a Service&#xff0c;移動后端即服務&#xff09;能力。云數據庫提供了端云數據…

IEEE使用遇到的問題

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄 前言一、如何在已知期刊中查找自己方向的論文 前言 IEEE 使用相關問題記錄 一、如何在已知期刊中查找自己方向的論文 比如在IEEE Transactions on Visualization …

深入解析C#數組協變與克隆機制

—— 值類型與引用類型的內存行為差異 &#x1f50d; 一、數組協變&#xff08;Array Covariance&#xff09; 核心條件&#xff1a; 僅適用于引用類型數組被賦值對象與數組基類型需存在隱式/顯式轉換關系 class Animal {} class Dog : Animal {}Animal[] animals new Dog…

零散問題一

1.函數重載的原理 名稱修飾&#xff08;Name Mangling&#xff09; 作用&#xff1a;編譯器在編譯時對函數名進行編碼&#xff0c;生成唯一的內部標識符&#xff0c;使得同名函數能通過參數列表的差異被區分。示例&#xff1a; void func(int a); // 修飾后可能為 _Z4funcivo…

React Native【詳解】內置 API

屏幕 Dimensions 獲取屏幕信息 import { Dimensions } from "react-native"; export default function demo() {const { width, height, scale, fontScale } Dimensions.get("window");console.log(width, height, scale, fontScale); }參數為 window 時…

Selenium自動化測試常見的異常處理

在軟件開發和測試領域,Selenium作為一種廣泛使用的自動化測試工具,扮演著至關重要的角色。隨著自動化測試的不斷普及,如何在測試過程中有效捕獲并處理異常,成為了每個測試工程師必須掌握的技能。本文旨在深入探討Selenium異常處理的方法,通過豐富的案例和代碼,幫助新手朋…

企業級安全實踐:SSL 加密與權限管理(二)

權限管理&#xff1a;企業數據的守護者 權限管理的基本概念與重要性 權限管理&#xff0c;是指根據系統設置的安全規則或策略&#xff0c;用戶可以訪問且僅能訪問自己被授權的資源&#xff0c;不多不少 。它是企業信息安全體系的重要組成部分&#xff0c;旨在確保只有授權的人…

AMAT P5000 CVDFDT CVDMAINT Precision 5000 Mark 操作 電氣原理 PCB圖 電路圖等

AMAT P5000 CVDFDT CVDMAINT Precision 5000 Mark 操作 電氣原理 PCB圖 電路圖等

深入淺出:語言模型中的“自回歸生成”是什么?

在當今大語言模型&#xff08;LLM&#xff09;如 ChatGPT、GPT-4、文心一言、通義千問等風靡的時代&#xff0c;“自回歸生成”是驅動它們流暢對話、創作文本的核心引擎。 理解它是深入掌握LLM工作原理的關鍵一步。本文將用清晰易懂的語言&#xff0c;結合實例&#xff0c;為你…

LLMs基礎學習(八)強化學習專題(5)

LLMs基礎學習&#xff08;八&#xff09;強化學習專題&#xff08;5&#xff09; 文章目錄 LLMs基礎學習&#xff08;八&#xff09;強化學習專題&#xff08;5&#xff09;重要性采樣&#xff08;Importance Sampling&#xff09;權重計算邏輯兩種實現形式使用注意事項 PPO 與…

深入理解“回調地獄“(Callback Hell)

"回調地獄"是異步編程中常見的問題&#xff0c;指由于過多嵌套的回調函數導致的代碼難以理解和維護的情況。 一、什么是回調地獄 基本概念 回調地獄(Callback Hell/Pyramid of Doom)是指&#xff1a; 多層嵌套的回調函數形成的代碼結構 代碼向右縮進越來越深&…

Oracle 的 TCP.SEND_TIMEOUT 參數

Oracle 的 TCP.SEND_TIMEOUT 參數 一 參數基本概念 TCP.SEND_TIMEOUT 是 Oracle Net Services 中的一個重要參數&#xff0c;用于控制 TCP 數據發送操作的最長等待時間。 二 關鍵特性 特性說明參數類型sqlnet.ora 配置文件參數默認值none (無超時限制)單位ms, sec, min, 默…

[Nginx] 配置中的sendfile參數詳解:從傳統 IO 到零拷貝的性能優化

一、sendfile 是什么&#xff1f; sendfile 是 Nginx 中一個關鍵的配置參數&#xff0c;用于控制是否使用操作系統提供的 sendfile() 系統調用來傳輸文件。 sendfile on;&#xff1a;啟用零拷貝技術&#xff0c;直接由內核將文件發送到網絡。sendfile off;&#xff1a;使用傳統…

(LeetCode 每日一題) 2138. 將字符串拆分為若干長度為 k 的組 (字符串、模擬)

題目&#xff1a;2138. 將字符串拆分為若干長度為 k 的組 思路&#xff1a;字符串模擬&#xff0c;時間復雜度0(n)。 C版本&#xff1a; class Solution { public:vector<string> divideString(string s, int k, char fill) {vector<string> v;int ns.size();for…