共享跨服务的轴突查询

huangapple go评论78阅读模式
英文:

sharing axon queries across services

问题

我有三个不同的服务(A、B和C)正在运行,所有这些服务都连接到AxonServer 4.3.3。除了它们之外,我还有一个API服务,其中包含所有事件,以便可以在所有服务之间共享。当事件被触发时(假设由服务A触发),其他服务(B和C)会监听并相应地做出反应。

现在我也想共享查询,以便当一个服务(假设为A)想要获取属于另一个服务(假设为B)的一些信息时,它可以直接触发相应的查询,该查询将被服务B监听并返回信息。

  1. 在Axon中允许这样做吗?(即在服务之间共享查询,类似于事件的做法)
  2. 如果允许,它是否遵循Axon的最佳实践?
  3. 如果不允许/不遵循最佳实践,有什么替代解决方案?

更新

我简单地将查询添加到了common-api服务中,并且从服务A中触发,如下所示:

  1. 当我使用queryGateway.query(findCourierByIdQuery, responseType)时:我收到了查询处理程序未找到的异常。
  2. 当我使用queryGateway.subscriptionQuery(findCourierByIdQuery, initialResponseType, updateResponseType)时:我什么都没收到。
  3. 当我使用queryGateway.scatterGather(findCourierByIdQuery, responseType, timeout, timeUnit)时:我得到了一个空流。

当我从服务B本身触发findCourierByIdQuery时,查询处理程序被调用,并且我得到了正确的响应。

我的观察是,只有在查询从相同的组件(在这种情况下是服务B)触发时,查询处理程序才会被调用,如果我从其他组件(服务A)触发查询,则不会被调用。

请注意,所有服务都独立运行在不同的主机和端口上,但连接到同一个AxonServer,查询处理程序编写在服务B中。

因此,基本上实现类似于以下内容:

  • findCourierByIdQuery 在common api服务中被定义。
  • 服务A和B都依赖于common api服务。
  • 服务A执行findCourierByIdQuery的查询。
  • 服务B具有findCourierByIdQuery的查询处理程序实现。
英文:

I have three different services(A,B & C) running and all are connected to axonserver 4.3.3. Apart from them i an api service which contains all events so that it can be shared among all services. when an event is fired(lets say by service A) it is being listened by other services(B & C) and they react accordingly.

Now i want to share queries as well so that when a service(Lets say A) wants some information which belong to another services(lets say B), it can directly fire corresponding query which will listened by service B and will return the info.

  1. Is it allowed in axon(i.e., sharing query among services, like we do for events)?
  2. If allowed, does it follow axon best practices ?
  3. If not allowed/doesn't follow best practices, what is alternate solution ?

> UPDATE

I simply added queries to the common-api service and fired it from Service A as follows:

  1. when i used queryGateway.query( findCourierByIdQuery, responseType): i got queryhandler not found exception
  2. when i used queryGateway.subscriptionQuery( findCourierByIdQuery, initialResponseType, updateResponseType): i got nothing
  3. when i used queryGateway.scatterGather( findCourierByIdQuery, responseType, timeout, timeUnit): i got an empty stream

when i fired the findCourierByIdQuery from service B itself, the queryhandler was invoked and i got the proper response.

My observation is that, the query-handler only gets called if the query has been fired from the same component(service B in this case), it is not getting called if i fire the query from some other component(service A).

Note that all services are running independently on different hosts and ports, but connected to the same axonserver and the queryhandler is written in service B.

so basically the implementation is something like this:

  • findCourierByIdQuery is defined in common api service.
  • Service A and B has the dependency of common api service.
  • service A does the query for findCourierByIdQuery.
  • Service B has the findCourierByIdQuery query handler implementation.

答案1

得分: 5

这在我看来是完全可以的!

Axon正在使用三种类型的消息:命令、事件和查询。它们代表了您的服务的API。
下游服务(在您的情况下为B&C)可以发送命令,订阅事件,或者发送(和订阅)上游服务(在您的情况下为A)的查询。简单地说,B&C依赖于A。如果可能的话,重要的是使这种依赖关系是单向的(只有B依赖于A,而不是相反的)。

通常,在下游服务(B&C)的边缘上,您希望有一个“防腐层组件”。它可以将上游服务(A)中的事件转换为自己的命令,并且可以将自己的事件转换为上游服务(A)的命令。这是一个Saga或常规事件处理程序(处理器)。

如果您对查询感兴趣,那么您的“防腐层组件”将使用查询网关从上游服务(A)发出(订阅)查询,并有望以自动方式将响应转换为命令,类似于我提到的Saga/事件处理程序。通常,这种通过查询API的集成不是完全自动化的:“从查询到命令的转换涉及用户”。在这种情况下,服务B(下游)正在使用查询响应(来自服务A)来呈现视图,用户可以从该视图中发出下一个命令(服务B)

在下游系统中拥有防腐层组件将使您的服务具有自治性。

祝好,
伊万

英文:

This is totally fine in my opinion!

Axon is using three types of messages: commands, events, and queries. They represent the API of your services.
Downstream service (in your case B&C) can send Commands, subscribe to Events, or send (and subscribe) to Queries of the Upstream service (in your case A). Simply, B&C are depending on A. It is important to make this dependency unidirectional (the only B is depending on A, but not the other way around) if possible.

Usually, you want to have an anti-corruption layer component at the edge of the Downstream services (B&C). It can translate events from Upstream service (A) to its own commands and it can translate its own events to the command of the Upstream service (A). This is a Saga or a regular Event Handler (Processor).

If you are interested in Queries than your anti-corruption layer component will be using a query gateway to issue (subscribe to) queries from the Upstream service (A) and hopefully translate the response to Commands in an automated fashion, similar to Saga/Event Handlers I have mentioned. Usually, this integration over the Query API is not fully automated: translation from the query to command involves the user. In this case service B (downstream) is using query response (of service A) to render the view from which the user can issue next command (service B)

Having the anti-corruption layer component in the downstream system will make your services autonomous.

Best,
Ivan

huangapple
  • 本文由 发表于 2020年8月18日 12:10:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63461764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定