springboot搭建流式响应服务,SSE服务端实现

本文介绍了如何在SpringBoot应用中利用SSE(Server-SentEvents)模式处理客户端的流式响应需求,通过SseEmitter发送数据并展示了一个与OkHttp客户端交互的示例,展示了onEvent、onOpen和onClose事件的触发过程。
摘要由CSDN通过智能技术生成

我们经常会有一些流式响应的需求,即客户端发一次请求,服务端需要多次响应才能返回完整数据的案例。譬如用户请求一篇长文,在数据库里有很多个段落,我们也不希望一次性全部查询完毕再返回给客户端(耗时太久),而是希望查一段就返回一段,逐次批量返回给客户端。

那么在springboot中,就可以很简单地实现这种场景需求,即SSE(Server-Sent Events)模式。

举例:


   
   
  1. @RequestMapping("/emitter")
  2. public SseEmitter sse (@RequestBody String inputParameter, HttpServletResponse response) {
  3. response.setContentType( "text/event-stream");
  4. response.setCharacterEncoding( "UTF-8");
  5. SseEmitter emitter = new SseEmitter();
  6. // Simulate asynchronous data retrieval from the database
  7. new Thread(() -> {
  8. try {
  9. // Query the database based on the input parameter and send data in batches
  10. for ( int i = 0; i < 10; i++) {
  11. String data = "Data batch " + i + " for parameter: " + inputParameter;
  12. emitter.send(data);
  13. Thread.sleep( 1000); // Simulate delay between batches
  14. }
  15. emitter.complete(); // Complete the SSE connection
  16. } catch (Exception e) {
  17. emitter.completeWithError(e); // Handle errors
  18. }
  19. }).start();
  20. return emitter;
  21. }

如以上代码,返回的对象是SseEmitter,每次调用emitter.send()方法,客户端就会收到一条消息,即一次响应,响应结束的标志是调用emitter.complete方法。

所以即便我读取数据是在new Thread里完成的,在请求该接口时,该方法也不会立即结束,而是阻塞住,等待emitter.complete方法完成。

下面我们可以发起个http客户端请求去查看效果,这里我们使用okhttp客户端。

pom依赖:


   
   
  1. <dependency>
  2. <groupId>com.squareup.okhttp3</groupId>
  3. <artifactId>okhttp</artifactId>
  4. <version> 4.10 .0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.squareup.okhttp3</groupId>
  8. <artifactId>okhttp-sse</artifactId>
  9. <version> 4.10 .0</version>
  10. </dependency>

   
   
  1. public static void main (String[] args) {
  2. String json = "{\"inputParameter\": \"1234\"}";
  3. HttpUtils.stream( "http://localhost:8081/emitter", new HashMap<>(), json, new EventSourceListener() {
  4. @Override
  5. public void onClosed (@NotNull EventSource eventSource) {
  6. System.out.println( "closed");
  7. }
  8. @Override
  9. public void onOpen (@NotNull EventSource eventSource, @NotNull Response response) {
  10. System.out.println( "open");
  11. }
  12. @Override
  13. public void onEvent (@NotNull EventSource eventSource, @Nullable String id, @Nullable String type, @NotNull String data) {
  14. System.out.println(data);
  15. }
  16. });
  17. }

其中HttpUtils是用okhttp封装的工具类:

HttpUtils.java


   
   
  1. public static final MediaType MEDIA_TYPE_JSON = MediaType.parse( "application/json; charset=utf-8");
  2. public final static int MAX_IDLE_CONNECTIONS = 20;
  3. public final static long KEEP_ALIVE_DURATION = 30L;
  4. public final static int CONNECT_TIME_OUT = 6;
  5. public final static int WRITE_TIME_OUT = 10;
  6. public final static int READ_TIME_OUT = 40;
  7. /**
  8. * client
  9. * 配置重试
  10. */
  11. private final static OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder()
  12. .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
  13. .writeTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS)
  14. .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
  15. .connectionPool( new ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION, TimeUnit.MINUTES))
  16. .build();
  17. public static boolean stream (String url, Map<String, String> headers, String json, EventSourceListener eventSourceListener) {
  18. try {
  19. RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);
  20. Request. Builder builder = new Request.Builder();
  21. buildHeader(builder, headers);
  22. Request request = builder.url(url).post(body).build();
  23. EventSource. Factory factory = EventSources.createFactory(HTTP_CLIENT);
  24. //创建事件
  25. log.info( "http stream请求,url: {},参数: {}", url, json);
  26. factory.newEventSource(request, eventSourceListener);
  27. return true;
  28. } catch (Exception e) {
  29. log.error( "http stream请求,url: {} 失败 ,参数: {}", url, json, e);
  30. }
  31. return false;
  32. }

当服务端每次调用emitter.send方法时,客户端的onEvent就会触发一次,同理,onOpen,onClose,onFailure都对应服务端的对应方法调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值