我写了一个服务器端 sse 测试 api 是这样的:
public SseEmitter testStream(String question) {
SseEmitter emitter = new SseEmitter();
// Start asynchronous processing
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
// Generate some event data
String eventData = "Event data " + i;
// Create Server-Sent Event object
ServerSentEvent event = ServerSentEvent.builder()
.event("message")
.data(eventData)
.build();
// Serialize event to string and send to client
String serializedEvent = JSON.toJSONString(event);
emitter.send(serializedEvent);
// Wait for one second before sending the next event
Thread.sleep(1000);
}
// Complete the SSE stream
emitter.complete();
} catch (Exception e) {
emitter.completeWithError(e);
}
}).start();
return emitter;
}
然后将 api 作为 rest api 公开:
@GetMapping(path="/test",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
SseEmitter testStream(@RequestParam(required = true) String question);
当我在本地机器上调用这个api时,sse消息每秒钟一个一个地返回,这就是我想要的。但是我将这个api部署到远程云服务器后,api等待很长时间并返回一次结果。我错过了什么吗?我应该怎么做才能让远程服务器一个一个地返回消息,而不是一次等待很长时间。这是我的测试命令:
# localhost test
curl -X GET -H 'Content-Type: application/json' -N http://localhost:11014/ai/stream/chat/test\?question\=1
# remote server test
curl -X GET -H 'Content-Type: application/json' -H 'x-request-id:1' -H 'x-access-token: eyJhbGciOiJIUzUx.balabala.sDM53g' -N https://ai.example.top/ai/stream/chat/test\?question\=1
这样的远程服务路线:
客户端用户>dns>nginx>kubernetes traefik>kubernetes 中的 spring gateway>kubernetes 服务>kubernetes pod