Springboot 实现SSE 消息推送

方式一

 @RequestMapping(value = "/task/real/info",method = RequestMethod.GET)
    public void taskState(final HttpServletRequest request, final HttpServletResponse response)
    		throws IOException, InterruptedException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");

        final PrintWriter printWriter = response.getWriter();
        logger.info("建立服务器采集连接");
 try {
            printWriter.write("data:" + "建立连接成功" + "\n\n");
            printWriter.write("retry:500\n");
            printWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }


        messagePublishSubscribe.addListener(new AbstractMessageListener(MessageType.TOPIC,"MessageTopic") {
            @Override
            public void onMessage(String busiType, Object key, Object val, String waitMapBusiType) {

                try {
                    printWriter.write("data:" + val + "\n\n");

                } catch (Exception e) {
                    e.printStackTrace();
                }
                printWriter.write("retry:500\n");
                printWriter.flush();
                /*if(printWriter.checkError()) {
                	logger.error("客户端端口连接");
                	return ;
                }*/
            }
        });

        while (true) {
            Thread.sleep(Integer.MAX_VALUE);
        }
    }

方式二:

@RequestMapping(value ="/server/real/info", method = {RequestMethod.GET}, produces = "text/event-stream;charset=UTF-8")
    public ResponseBodyEmitter pushmemmsg() {

        final SseEmitter emitter = new SseEmitter(0L);

            do {
                try {
                	String key = redisKeyUtil.keyBuilder("statis","serverinfo");
                	logger.info("emitter push message .....");
                	Map hmget = redisUtil.hmget(key);
                	
                	if(!StringUtils.isEmpty(hmget)) {
                		List listInfo = new ArrayList(hmget.values());
                		 emitter.send(listInfo.toString(), MediaType.TEXT_EVENT_STREAM);
                		 logger.info("emitter push server real info .....");
                	}else {
                		logger.info("emitter push server real info empty .....");
                	}

                    
                    try {
                        Thread.sleep(2000L);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    emitter.completeWithError(e);

                    logger.info("emitter close.....");
                   
                }
                return emitter;
            } while (true);
  
    }

 

前端:




SSE Demo


Springboot 实现SSE 消息推送_第1张图片

nginx:

        
             location /api/v1/message/server/real/ {
                      proxy_pass http://${PIE_NGINX_CONF_MESSAGE}:8092;
                                

                      proxy_set_header  X-Real-IP  $remote_addr;
                      proxy_set_header Cookie $http_cookie;
                      proxy_cookie_domain domino.server nginx.server;
                        proxy_buffering   off;

                      fastcgi_read_timeout 300s;
            
      }

你可能感兴趣的:(Springboot 实现SSE 消息推送)