如果我重复使用一个自定义客户端,该客户端向VERTX端点中的外部服务提出请求,一个请求中的用户异常会阻止另一个用户吗?
我正在为带有Kafka和Flink等的多个用户的缓存数据库查询构建数据流管道。我对何时需要实现多线程感到困惑。
我有一个构建的用户服务,该服务包含使用VERTX的HTTP服务器。我有一个终点,可以接受用户的一些输入。其中一个端点向Kafka服务提出网络请求,并将一些信息从主题返回给用户。我为此创建了一个“ kafka客户端”,在每个用户请求到端点的请求中,我创建了此kafka客户端的新实例来发送请求。每个用户的请求都是完全无关的,因此首先要处理的请求并不重要 - 没有种族条件需要担心。
如果我为每个传入的用户请求创建了此KAFKA客户端的新实例,则在用户1的请求中抛出的例外不应阻止用户2的请求吗? (因为我已经在单独的用户线程中)?
在每个请求上都创建一个客户的新实例吗?我习惯于创建单例并在节点项目中重复使用它们(不确定您如何在Java中“导入”类)。
,如果我创建一个Singleton客户端并将其导入到端点上,以便在每个用户的请求中重复使用,而不是在每个请求上创建新实例,是否会在用户1的请求中抛出一个例外,然后阻止用户2的请求? (除非我手动添加多线程)或者每个用户都有自己的线程,否则用户1的线程中的例外也不会影响用户2的线程,即使他们使用了同一Singleton?
I'm building a data flow pipeline for cached database queries for multiple users with Kafka and Flink etc. and I am confused about when I need to implement multi threading.
I have a built a user service that contains an HTTP server using Vertx. I have an endpoint that accepts some input from users. One of the endpoints makes a network request to a Kafka service and returns some information from a topic to the user. I created a "kafka client" for this, and in every user request to the endpoint I create a new instance of this kafka client to send the request. Every user's request is totally unrelated, so it doesn't matter whose request gets processed first - there's no race condition to worry about.
If I create a new instance of this kafka client for every incoming user request, an exception thrown in user 1's request should not block user 2's request right? (because I'm already in a separate user thread)?
Is creating a new instance of a client on every request a good practice? I'm used to creating singletons and reusing them in Node projects (not sure how you even "import" classes in Java).
Instead of creating new instances on every request, if I create one singleton client and import it into the endpoint so that it gets reused on every user's request, would an exception thrown in user 1's request then block user 2's request? (Unless I manually added multithreading) or if each user gets their own thread, an exception in user 1’s thread would not affect user 2‘s thread even if they’re using the same singleton?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Vert.x中,您通常会尝试避免自己实施多线程。您应该简单地创建处理程序,而Vert.x则在需要时将其拨打。您可以在vert.x 此处读取有关其工作原理的更多信息。
抛出的例外没有阻止任何请求,甚至没有抛出的请求。但是,如果您忘记处理它们,它们将导致请求失败。 。例外根本不会影响第二用户的要求,因为它们彼此隔离。请注意,这也取决于异常的性质。如果您共享您的kafka客户端并断开连接,则所有用户将单独遇到相同的错误。
完全取决于您要创建的客户端,您应该查看其文档。您还可以查看 Connection ploming 如果您对此主题感兴趣。您可以尝试在课堂内创建客户端,而不是为每个请求创建一个新的客户。您也可以使用 vert.x kafka client Verticle的
开始
函数。这样,您应该站在安全的一面。与1。
相同
In vert.x you typically try to avoid implementing multithreading yourself. You should simply create your handlers and vert.x will call them when they are needed. You can read more about how this works in vert.x here.
Thrown exceptions don't block any request, not even the one where they are thrown. They will however cause the request to fail, if you forget to handle them. The exception would not affect the request of the second user at all, because they are isolated from each other. Note that this also depends on the nature of the exception. If you share your kafka client and it gets disconnected, then all users will get the same error individually.
That completely depends on the client that you are creating and you should check out their documentation. You can also have a look at connection pooling if you're interested in this topic. You can try creating the client inside of your class, instead of creating a new one for each request. You can also use the vert.x kafka client and create it inside your verticle's
start
function. This way you should be on the safe side.Same as 1.