[英]springboot2 actuator endpoint not supporting Server Sent Event(SSE)
I am trying to expose few metrics as a stream using SSE.我正在尝试使用 SSE 将几个指标公开为 stream。 I am able to consume the SSE event from the restController but when I added custom actuator endpoint, it just closes the connection right way.
我可以使用来自 restController 的 SSE 事件,但是当我添加自定义执行器端点时,它只是以正确的方式关闭连接。
@Component
@Endpoint(id = "test")
public class StreamMetrics {
@ReadOperation
public Flux<ServerSentEvent<String>> streamEvents() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> ServerSentEvent.<String> builder()
.id(String.valueOf(sequence))
.event("pingpong")
.data("ping")
.build());
}
}
Result结果
curl -n -v http://localhost:9080/actuator/test
* Trying ::1:9080...
* TCP_NODELAY set
* Connected to localhost (::1) port 9080 (#0)
> GET /actuator/test HTTP/1.1
> Host: localhost:9080
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< transfer-encoding: chunked
< Content-Type: text/event-stream;charset=UTF-8
<
id:0
event:pingpong
data:ping
* Connection #0 to host localhost left intact
this is terminated right after the fist event这在第一个事件之后立即终止
where as然而
@RestController
@RequestMapping(value = "/test")
public class SSETest {
@GetMapping("/stream-sse")
public Flux<ServerSentEvent<String>> streamEvents() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> ServerSentEvent.<String> builder()
.id(String.valueOf(sequence))
.event("pingpong")
.data("ping")
.build());
}
}
Result结果
curl -v -n http://localhost:9080/test/stream-sse
* Trying ::1:9080...
* TCP_NODELAY set
* Connected to localhost (::1) port 9080 (#0)
> GET /test/stream-sse HTTP/1.1
> Host: localhost:9080
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< transfer-encoding: chunked
< Content-Type: text/event-stream;charset=UTF-8
<
id:0
event:pingpong
data:ping
id:1
event:pingpong
data:ping
id:2
event:pingpong
data:ping
this goes on without getting terminated.这种情况不会被终止。
What is special about endpoint annotation that is terminating the event (continuous flow)?终止事件(连续流)的端点注释有什么特别之处?
I tested this in '2.2.4 ' and '2.3.0'我在“2.2.4”和“2.3.0”中测试了这个
I found an answer.我找到了答案。 @ReadOperation(produces = "text/event-stream") will alter the default content type application/vnd.spring-boot.actuator.v3+json in actuator endpoints.
@ReadOperation(produces = "text/event-stream") 将更改执行器端点中的默认内容类型 application/vnd.spring-boot.actuator.v3+json。 There was not a document so I looked at the code.
没有文档,所以我查看了代码。
@Component
@Endpoint(id = "test")
public class StreamMetrics {
@ReadOperation(produces = "text/event-stream")
public Flux<ServerSentEvent<String>> streamEvents() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> ServerSentEvent.<String> builder()
.id(String.valueOf(sequence))
.event("pingpong")
.data("ping")
.build());
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.