Spring cloud 具有服务器发送事件的ZuulProxy

Spring cloud 具有服务器发送事件的ZuulProxy,spring-cloud,Spring Cloud,我有两个spring启动应用程序。一个在端口8081上运行ZuulProxy服务器,并将向另一个在端口8086上运行的spring引导应用程序(通知服务器)发出的所有请求/通知转发给另一个spring引导应用程序 zuul配置如下所示 zuul: routes: notification: path: /notification/** url: http://localhost:8086 @RestController public

我有两个spring启动应用程序。一个在端口8081上运行ZuulProxy服务器,并将向另一个在端口8086上运行的spring引导应用程序(通知服务器)发出的所有请求/通知转发给另一个spring引导应用程序

zuul配置如下所示

 zuul:  
  routes:
    notification:
          path: /notification/**
          url: http://localhost:8086
 @RestController
public class JmsController {

    private Logger log = Logger.getLogger(this.getClass());

    private final List<SseEmitter> emitters = new ArrayList<>();

    @Autowired
    private JmsService jmsService;

    @RequestMapping(value = "send", method = RequestMethod.POST, consumes= MediaType.APPLICATION_JSON_VALUE)
    public NotificationDTO sendMessage(@RequestBody NotificationDTO dto){
        jmsService.sendMessage(dto);
        return dto;
    }

    @RequestMapping(path = "/stream", method = RequestMethod.GET)    
    public SseEmitter stream() throws IOException {

        SseEmitter emitter = new SseEmitter();

        emitters.add(emitter);
        emitter.onCompletion(() -> emitters.remove(emitter));

        return emitter;
    }

    @JmsListener(destination = "${queue.name}", containerFactory = "myJmsContainerFactory")
    public void receiveMessage(NotificationDTO dto) throws Exception{

        log.info("Got message" + dto);

        if(emitters!=null && emitters.size() > 0) {
            emitters.forEach((SseEmitter emitter) -> {
                try {
                    emitter.send(dto, MediaType.APPLICATION_JSON);
                } catch (IOException e) {
                    emitter.complete();
                    emitters.remove(emitter);
                    log.info(e);
                }
            });
        }else{
            log.info("No emitters");
        }

    }


}
通知服务器代码如下所示

 zuul:  
  routes:
    notification:
          path: /notification/**
          url: http://localhost:8086
 @RestController
public class JmsController {

    private Logger log = Logger.getLogger(this.getClass());

    private final List<SseEmitter> emitters = new ArrayList<>();

    @Autowired
    private JmsService jmsService;

    @RequestMapping(value = "send", method = RequestMethod.POST, consumes= MediaType.APPLICATION_JSON_VALUE)
    public NotificationDTO sendMessage(@RequestBody NotificationDTO dto){
        jmsService.sendMessage(dto);
        return dto;
    }

    @RequestMapping(path = "/stream", method = RequestMethod.GET)    
    public SseEmitter stream() throws IOException {

        SseEmitter emitter = new SseEmitter();

        emitters.add(emitter);
        emitter.onCompletion(() -> emitters.remove(emitter));

        return emitter;
    }

    @JmsListener(destination = "${queue.name}", containerFactory = "myJmsContainerFactory")
    public void receiveMessage(NotificationDTO dto) throws Exception{

        log.info("Got message" + dto);

        if(emitters!=null && emitters.size() > 0) {
            emitters.forEach((SseEmitter emitter) -> {
                try {
                    emitter.send(dto, MediaType.APPLICATION_JSON);
                } catch (IOException e) {
                    emitter.complete();
                    emitters.remove(emitter);
                    log.info(e);
                }
            });
        }else{
            log.info("No emitters");
        }

    }


}
我将应用程序的代码上传到github,以防您需要运行代码或获取有关此实现的更多详细信息

发送这样的帖子应该会自动在浏览器上显示消息。该网页仅在前10秒接收消息,然后超时

curl 'http://localhost:8081/notification/send' -H 'Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8,pt;q=0.6,pt-BR;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: */*' -H 'Connection: keep-alive' --data-binary '{"id":"12345","label":"my test 6 curl","type":1,"url":"http://www.gmail.com"}' --compressed

感谢您在advanced中查看该代码,它不是硬编码的,而是默认为可在
应用程序中设置的可配置值。属性
zuul.host.socket超时毫秒
。另一个是
zuul.host.connect timeout millis

对于我来说,服务器通过zuul发送的事件不起作用。我使用spring boot 1.5.8和cloud Dalston.SR4

SSE是否得到zuul的支持


XHR请求被打开到enpdpoint,但我没有得到响应

Zuul当前不支持websocketsI不认为服务器发送的事件使用web套接字。SSE通过传统HTTP发送。这意味着它们不需要特殊的协议或服务器实现就可以工作。好的,谢谢。我来看看。我想我找到了问题所在:超时被编码为10000毫秒,所以所有连接都在10秒后关闭。对于服务器发送的事件,此超时应该是可配置的。查看该代码,它不是硬编码的,而是默认为可在
application.properties
zuul.host.socket timeout milis
中设置的可配置值。另一个是
zuul.host.connect timeout millis
。是。你可以看看这里的例子