当前位置:   article > 正文

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

eventsources.createfactory

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

那么在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. }
  • 1

如以上代码,返回的对象是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

   
   
  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. }
  • 1

其中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. }
  • 1

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/849951
推荐阅读
相关标签
  

闽ICP备14008679号