简体   繁体   English

如何在微服务的多个实例之间维护 SseEmitters 列表?

[英]How to maintain SseEmitters list between multiple instances of a microservice?

Language: Spring Boot, JS语言:Spring Boot、JS

Overview : I am implementing server sent events functionality in my application which will be deployed in cloud foundry, wherein based on a new message in a queue(which I have subscribed in my micro-service), I will send some update to my client/browser(which is using EventSource).概述:我正在我的应用程序中实现服务器发送的事件功能,该功能将部署在 cloud foundry 中,其中基于队列中的新消息(我已在我的微服务中订阅),我将向我的客户端发送一些更新/浏览器(使用 EventSource)。 For this, I am maintaining a SseEmitters List(for mainitaining all the active SseEmitter) on my server side.为此,我在服务器端维护一个 SseEmitters 列表(用于维护所有活动的 SseEmitter)。 Once I receive a new message from the queue, based on the id(a field in the queue message), I will emit the message to corresponding client.一旦我从队列中收到一条新消息,我就会根据 id(队列消息中的一个字段)将消息发送给相应的客户端。

PROBLEM : How will the above scenario work, when I scale my application by creating multiple instances of it.问题:当我通过创建应用程序的多个实例来扩展我的应用程序时,上述场景将如何工作。 Since only one instance will receive the new queue message, it may happen that the active SseEmitter is not maintained in that particular instance, how do I solve this?由于只有一个实例会收到新的队列消息,因此可能会发生在该特定实例中未维护活动 SseEmitter的情况,我该如何解决?

To solve this problem, following approaches can be observed.为了解决这个问题,可以观察到以下方法。

DNS concept域名系统概念

If you think about it, knowing where your user (SSE Emitter) is, is like knowing where some website is.如果您考虑一下,知道您的用户(SSE Emitter)在哪里,就像知道某个网站在哪里一样。 You can use DNS-look-alike protocol to figure out where your user is.您可以使用 DNS-look-alike 协议来确定您的用户所在的位置。 Protocol would be as follows:协议如下:

  • Whe user lands on any of your instances, associate user with that instance.当用户登陆您的任何实例时,将用户与该实例相关联。 Association can be done by either using external component, eg Redis or a distributed map solution like Hazelcast.关联可以通过使用外部组件(例如 Redis)或分布式地图解决方案(如 Hazelcast)来完成。
  • Whenever user disconnects from SSE, remove association.每当用户与 SSE 断开连接时,删除关联。 Sometimes disconnect is not registered properly with Spring SSEEmiter, so disassociation can be done when sendig message fails.有时断开连接没有正确注册到 Spring SSEEmiter,因此可以在发送消息失败时解除关联。
  • Other parties (microservices) can easily query Redis/Hazelcast to figure on which instance user is.其他方(微服务)可以轻松查询 Redis/Hazelcast 以确定用户是哪个实例。

Message routing concept消息路由概念

If you're using messaging middleware for communication between your microservices, you can use routing feature which AMQP protocol provides.如果您使用消息传递中间件在微服务之间进行通信,则可以使用 AMQP 协议提供的路由功能。 Protocol would be as follows:协议如下:

  • each SSE instance creates their own queue on boot每个 SSE 实例在启动时创建自己的队列
  • user lands on any of SSE instances and instance adds exchange-queue binding with routing key = user uid用户登陆任何 SSE 实例,实例添加交换队列绑定,路由键 = 用户 uid
  • Whenever user disconnects from SSE, remove association.每当用户与 SSE 断开连接时,删除关联。 Sometimes disconnect is not registered properly with Spring SSEEmiter, so disassociation can be done when sendig message fails.有时断开连接没有正确注册到 Spring SSEEmiter,因此可以在发送消息失败时解除关联。
  • Other parties (microservices) need to send message to the exchange and define routing key.其他方(微服务)需要向交换器发送消息并定义路由键。 AMQP broker figures out which queue should receive message based on the routing key. AMQP 代理根据路由键确定哪个队列应该接收消息。

Bindings are not resource intesive on modern AMQP brokers like RabbitMQ.绑定在像 RabbitMQ 这样的现代 AMQP 代理上不是资源密集型的。

Your question is old, and if you didnt figure this out by now, hope this helps.您的问题很老,如果您现在还没有弄清楚,希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用微服务多个实例刷新请求令牌 - How to refresh request token with microservice multiple instances Hibernate如何维护多个Java实例的缓存 - How hibernate maintain cache for multiple java instances 如何维护同一应用程序的多个实例之间的消息顺序 - How to maintain order of messages among multiple instances of same application 存储和转发具有多个微服务实例的设计模式 - Store and forward design pattern with multiple instances of a microservice 硒; 如何运行多个实例并维护不同的会话 - Selenium; How to run multiple instances and maintain different session 如何使用多个 SpringBoot 应用程序实例维护 DB 表中的唯一记录 - How to maintain unique records in DB table with multiple instances of SpringBoot application 如何维护列表项和数据库行之间的连接? - How to maintain connection between list item and database row? 如何为WAS Cell NW部署中的不同节点上运行的多个Web服务器实例维护单个属性文件 - How to maintain a single property file for multiple web server instances running on different node on WAS Cell NW deployment 如何创建一个微服务的多个实例:SpringBoot和Spring Cloud - How to create several instances of a Microservice: SpringBoot and Spring Cloud 如何维护线程列表? - how to maintain a list of threads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM