Springboot SseEmitter流式輸出的實(shí)現(xiàn)代碼
Springboot SseEmitter流式輸出
前言
最近做AI類(lèi)的開(kāi)發(fā),看到各大AI模型的輸出方式都是采取的一種EventStream
的方式實(shí)現(xiàn)。
不是通常的等接口處理完成后,一次性返回。
而是片段式的處理完成一個(gè)分片,就立馬告知前端做出處理;后續(xù)處理出新的片段則再次發(fā)送給客戶(hù)端。
在Spring
框架中就有一個(gè)類(lèi)似的方式實(shí)現(xiàn)。SseEmitter
。
SseEmitter 簡(jiǎn)介
SseEmitter
是在Spring 4.2
開(kāi)始引入的,使用的話需要注意版本,不過(guò)Springboot 2.X 是可以玩的。
測(cè)試demo
編寫(xiě)一段代碼,循環(huán)返回給客戶(hù)端。如下所示:
package cn.xj.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.io.IOException; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @RestController @RequestMapping("/sse/mitter") public class SseMitterController { @GetMapping(value = "/stream", produces = "text/event-stream") public SseEmitter stream() { // 設(shè)置默認(rèn)超時(shí)時(shí)間 0L 表示無(wú)限 // 注意:這里的單位是 ms SseEmitter sseEmitter = new SseEmitter(30000L); // 最好不要阻塞主線程 Executors.newSingleThreadExecutor().execute(() -> { try { for (int i = 0; i < 10; i++) { sseEmitter.send("這只是一個(gè)流式輸出案例:" + i); TimeUnit.SECONDS.sleep(1); } // 通知客戶(hù)端消息發(fā)送完畢 sseEmitter.complete(); } catch (Exception e) { e.printStackTrace(); sseEmitter.completeWithError(e); } }); return sseEmitter; } }
瀏覽器請(qǐng)求,打開(kāi)控制臺(tái)查看數(shù)據(jù)格式,如下所示:
注意點(diǎn)
異常一 ResponseBodyEmitter is already set complete
這種問(wèn)題通常是 設(shè)置超時(shí)時(shí)間timeout
太小導(dǎo)致的。網(wǎng)上很多demo中說(shuō)的這個(gè)單位是秒,但實(shí)際測(cè)試來(lái)看,單位應(yīng)該是毫秒 ms
。
補(bǔ)充:SpringBoot中SSE流式輸出中止的核心代碼
SpringBoot中SSE流式輸出中止的核心代碼
在大模型會(huì)話中,會(huì)有一個(gè)功能是停止生成功能。這個(gè)功能如果在前端實(shí)現(xiàn),既取消監(jiān)聽(tīng)后端的流式返回事件,會(huì)導(dǎo)致后端日志中報(bào)錯(cuò)連接中斷等錯(cuò)誤。
由此引出的需求,我的接口A中使用了sse流式返回,需要做一個(gè)接口B,B的功能是中止第一個(gè)接口的流式返回,以下是核心代碼和思路:
方案一:需要借助redis,在輸出時(shí)循環(huán)判定來(lái)解決。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.concurrent.TimeUnit; @Controller public class MyController { @Autowired private RedisTemplate<String, Object> redisTemplate; @RequestMapping("/startStreaming") public SseEmitter startStreaming(HttpServletRequest request) throws IOException { String requestId = request.getId(); // 獲取請(qǐng)求的唯一標(biāo)識(shí)符 String key = "shouldStopStreaming_" + requestId; // 生成唯一的key SseEmitter emitter = new SseEmitter(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(/*輸入流*/)); // SSE輸出邏輯 String line; while ((line = bufferedReader.readLine()) != null) { Boolean shouldStop = (Boolean) redisTemplate.opsForValue().get(key); if (shouldStop != null && shouldStop) { break; // 檢查shouldStopStreaming標(biāo)志,若為true則中斷循環(huán) } // 發(fā)送數(shù)據(jù)給客戶(hù)端 emitter.send(line); } // 刪除key,確保不再需要該key時(shí)將其移除 redisTemplate.delete(key); return emitter; } @RequestMapping("/stopStreaming") @ResponseBody public String stopStreaming(HttpServletRequest request) { String requestId = request.getId(); // 獲取請(qǐng)求的唯一標(biāo)識(shí)符 String key = "shouldStopStreaming_" + requestId; // 生成唯一的key // 設(shè)置shouldStopStreaming為true,終止流式輸出 redisTemplate.opsForValue().set(key, true, 1, TimeUnit.HOURS); // 設(shè)置過(guò)期時(shí)間為1小時(shí)(可根據(jù)需要調(diào)整) return "Streaming stopped"; } }
A接口定期從Redis中獲取shouldStopStreaming的值,并檢查是否應(yīng)該中止流式輸出。B接口使用RedisTemplate將shouldStopStreaming的值設(shè)置為true,以指示A接口中止輸出。由于Redis的操作是原子性的,并且RedisTemplate提供了線程安全的訪問(wèn),這樣可以確保多個(gè)線程之間的協(xié)調(diào)和線程安全性。
方案二:使用本地緩存,結(jié)合SseEmitter特性實(shí)現(xiàn)(實(shí)際使用的此種方案)
private final Map<String, SseEmitter> sseCache = new ConcurrentHashMap<>(10); ## 對(duì)話接口中put一下前端隨機(jī)生成的不唯一emitterId sseCache.put(emitterId, emitter); ## 停止回答接口 @Override public void stop(String emitterId) { if (sseCache.containsKey(emitterId)) { sseCache.get(emitterId).complete(); sseCache.remove(emitterId); } }
到此這篇關(guān)于Springboot SseEmitter流式輸出 的文章就介紹到這了,更多相關(guān)Springboot SseEmitter流式輸出 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot+SseEmitter和Vue3+EventSource實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)推送
- SpringBoot?2.x?接入非標(biāo)準(zhǔn)SSE格式大模型流式響應(yīng)的實(shí)戰(zhàn)解決方案
- SpringBoot使用ResponseBodyEmitter處理流式日志和進(jìn)度條
- SpringBoot項(xiàng)目實(shí)現(xiàn)MyBatis流式查詢(xún)的教程詳解
- Java調(diào)用ChatGPT(基于SpringBoot和Vue)實(shí)現(xiàn)可連續(xù)對(duì)話和流式輸出的ChatGPT API
- springboot-mybatis/JPA流式查詢(xún)的多種實(shí)現(xiàn)方式
相關(guān)文章
Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解
這篇文章主要介紹了Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Spring中Bean的創(chuàng)建流程詳細(xì)解讀
這篇文章主要介紹了Spring中Bean的創(chuàng)建流程詳細(xì)解讀,Spring 中創(chuàng)建 Bean ,是通過(guò)調(diào)用 GetBean 方法來(lái)觸發(fā)的,所以,我們會(huì)從這個(gè)方法開(kāi)始,需要的朋友可以參考下2023-10-10SpringBoot校園綜合管理系統(tǒng)實(shí)現(xiàn)流程分步講解
這篇文章主要介紹了SpringBoot+Vue實(shí)現(xiàn)校園綜合管理系統(tǒng)流程分步講解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09Java?代碼本地設(shè)置Hadoop用戶(hù)名密碼的方法
在Hadoop環(huán)境中,通常使用Kerberos進(jìn)行身份驗(yàn)證,這篇文章主要介紹了Java?代碼本地設(shè)置Hadoop用戶(hù)名密碼的方法,需要的朋友可以參考下2024-08-08詳解SpringMVC和MyBatis框架開(kāi)發(fā)環(huán)境搭建和簡(jiǎn)單實(shí)用
這篇文章主要介紹了詳解SpringMVC和MyBatis框架開(kāi)發(fā)環(huán)境搭建和簡(jiǎn)單實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05