如何从客户端角度处理CQRS [英] How to handle CQRS from a client-side perspective

查看:115
本文介绍了如何从客户端角度处理CQRS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司计划在后端使用CQRS架构,但作为客户端开发人员,我对如何使用请求有点困惑。下面是我想出的方法,从我的角度来看,这是不理想的:

My company is planning to use CQRS architecture on our back-end but as a client-side developer I'm a bit confused about how to consume a request. Here are the methods that I came up with, none being ideal from my point of view:


  1. 服务器等待队列被处理,在响应中返回所需的数据。 (从性能角度来看,这听起来是一个非常差的方法);

  2. 客户端发出请求,并在请求被添加到队列后返回202 Accepted客户端使用间隔系统池,直到它从后端获取必要的数据(我不是每秒执行X http请求的大粉丝);

  3. Web套接字看起来像一个完美的解决方案,因为服务器将能够推送数据到客户端一旦它被处理(我有点担心有成千上万的套接字打开整个应用程序)。

所以问题是:这个问题最好的方法是什么? (不一定是上面的一个)

So the question is: What is the best approach to this problem? (not necessarily one of the above)

推荐答案

好吧,这一切都取决于你打算在UI中做什么。

Well, it all depends on what you plan to do in the UI.

如果您有SPA,并且对活动和JavaScript中的订阅,单个WebSocket连接似乎是最自然的选择。如果连接在大多数时间是空闲的,并且服务器是异步的(以避免线程饥饿),则数千个WebSocket连接在服务器中不是问题。但是...为什么你想要一个REST接口?您可以通过WebSocket发送具有相关ID的请求,然后等待具有相同相关ID的响应以知道它何时完成,这样可以避免每个请求创建新连接的开销。

If you have a SPA and good understanding of events and subscriptions in Javascript, a single WebSocket connection seems like the most natural option. Thousands of WebSocket connections are not a problem in the server if the connections are idle most of the time, and the server is asynchronous (to avoid thread starvation). But then... why do you want a REST interface? you can send requests through the WebSocket with a correlation ID and then wait for a response with that same correlation id to know when it is done, this way you would avoid the overhead of creating new connections per each request.

如果你对AJAX更舒服,并想使用REST,那么你可以使用异步方法,但是你需要池,直到你得到 HTTP 303见其他。这不是完美的,但它可能比打开一个WebSocket只是为了这个操作。

If you are more comfortable with AJAX and want to use REST, then you can use this asynchronous approach, but you need to pool till you get the HTTP 303 See Other. It is not perfect, but it is probably better than opening a WebSocket only for the sake of that operation.

阻塞连接,直到有一个响应是一个非常糟糕的方法。网络不能被认为是可靠的,所以如果连接断开,浏览器需要知道如何找出操作结​​果。

Blocking the connection till there is a response is a very bad approach. The network cannot be considered reliable, so if the connection cuts, the browser needs to know how to find out the result o fthe operation.

这篇关于如何从客户端角度处理CQRS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆