本文介绍了如何使服务器发送的事件(SSE)通过Zuul Proxy spring-boot 2.1.1工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过zuul反向代理通过SSE工作时遇到了一些问题

I have some issues getting SSE working trough my zuul reverse proxy

我的设置:

Spring boot version: 2.1.1.RELEASE
Spring cloud dependencies: Greenwich.RC1
Angular-cli: Angular 7
Kotlin: 1.3.11
os: Linux

我有一个使用SSE的小型休息服务

I have a small rest service that uses SSE

@SpringBootApplication
@RestController
@CrossOrigin("http://localhost:19195")
class RestApplication {
    private final val processor = DirectProcessor.create<Int>().serialize()
    private final val sink = processor.sink()
    private final val counter = AtomicInteger()

    @GetMapping("/stream")
    fun stream(): Flux<ServerSentEvent<Int>> {
        println("Stream connected")
        return processor.map { n -> ServerSentEvent.builder<Int>(n).build() }
    }

    @GetMapping("/increment")
    fun sink() {
        sink.next(counter.incrementAndGet())
    }

}

如果我的EventSource从我的角度服务直接连接到它,这将很好工作:

This works fine if my EventSource connects directly to it from my angular service:

const sse = new EventSource('http://localhost:8080/stream');
sse.onopen = (event) => {
  console.log('OnOpen ' + event);
};
sse.onmessage = (event) => {
  console.log(event);
sse.onerror = (error) => {
  console.log(error);
};

如果我点击/increment端点,则该服务会接收到具有递增值的sse事件.

If I hit the /increment endpoint the service receives sse events with incrementing values.

现在使用此zuul配置:

Now with this zuul config:

zuul:
 sensitive-headers:
 routes:
  rest:
    path: /rest/**
    url: http://192.168.75.90:8080
 add-proxy-headers: true
 add-host-header: true

Angular服务已更新为通过代理:

Angular service updated to go via proxy:

const sse = new EventSource('rest/stream');
sse.onopen = (event) => {
  console.log('OnOpen ' + event);
};
sse.onmessage = (event) => {
console.log(event);

};
sse.onerror = (error) => {
console.log(error);
};

现在我的角度服务,连接到流,等待默认的zuul超时(10秒),然后返回406并重新连接.那就是如果我没有数据.

Now my angular service, connects to the stream, waits for the default zuul timeout (10 seconds) then returns a 406 and reconnects. That is if i have no data.

如果我在接收器中有数据,它会连接到sse流,并且只要我将数据推入流中就不会发生任何事情.当我停止推送数据时,EventSource等待zuul超时(10秒),然后接收流中的所有数据并断开连接/重新连接.

If i have data in the sink, it connects to the sse stream and nothing happens as long as i am pushing data onto the stream. When I stop pushing data EventSource waits for zuul timeout (10 seconds) then receives all data on stream and disconnects/reconnects.

我知道zuul 1.x不支持SSE,但是我希望与Spring Boot 2.1.1捆绑在一起的zuul可以.

I am aware that zuul 1.x does not support SSE, but I was hoping the zuul bundeled with spring boot 2.1.1 would.

有人知道吗?

推荐答案

问题是您正在使用Zuul 1.x,即使您使用的是Springboot版本2.x.我遇到了同样的问题,似乎Spring Cloud Netflix当前和将来都将不支持Zuul2.x.因此,当您使用Spring Cloud环境时,我建议切换到 spring-cloud-gateway .我将它与SSE一起使用,并且对当前的解决方案感到满意.另一种选择是直接切换到Zuul 2.0.

The problem is you are using Zuul 1.x, even if you are using springboot version 2.x. I had the same problem and as it seems Spring Cloud Netflix will not support Zuul 2.x currently and in the future. So when you are using the spring cloud environment, i would suggest to switch to spring-cloud-gateway. I am using it with SSE and i am satisfied with the current solution. Another choice would be to switch over to Zuul 2.0 directly.

这篇关于如何使服务器发送的事件(SSE)通过Zuul Proxy spring-boot 2.1.1工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 11:06