Query sharing between services

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.

Is it allowed in axon?
if allowed, does it follow axon best practices ?
if not allowed/doesn’t follow best practices, what is alternate solution ?

Hi,

This is totally fine by my opinion!
Axon is using three types of messages: commands, events, and queries. They represent the API of your services/bounded contexts.
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. Does not matter what type of messages you are using to integrate this services. It is more important to make this dependency unidirectional (the only B is depending on A, but not the other way around).

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 queries from the Upstream service (A) and maybe translate the response to Commands in an automated fashion, similar to Saga/Event Handlers I have mentioned. I have to admit that whenever I integrated two services over the Query API I used the response to render it as a View of the current service, but this does not mean you should not automate this.

Best,
Ivan

If I have understood correctly then yes, issuing a query from one service when the @QueryHandler is in another service should work fine.

Check out the repo here which I found to be very beneficial: https://github.com/idugalic/digital-restaurant

In particular, see the **drestaurant-microservices-rest-2** project. The example I’ll point you to is the FindCourierQuery

  • FindCourierQuery
    • QueryHandler is in the query service (see here for code).
    • Issuing a query (in this case a subscription query) in a Controller in the command courier service (see here for code).

N.B. this uses the subscription query functionality of Axon.

Check out the documentation here for other types of query.

Also check out the source code for the QueryGateway to see the documentation of each method. In particular see the query method which I think is what you are looking for.

Hi Ivan and vab2048 ,

Thanks for the response. In my current project i don’t have scope to add an “anti-corruption layer component”. 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.

Note that all services are running independently on different hosts and ports and the queryhandler is written in service B.

so basically the structure 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 qurery for findCourierByIdQuery.
Service B has the findCourierByIdQuery query handler implementtaion.

Hi rohit,

Are you connecting to axon server?

Regards
Siddu

yes, all services are well connected to axon server. 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).

Hi rohit,

Queryhandling can be handled in any services.
Is that are you using any configuration to configure Querygateway?

Regards
Siddu

Hi Siddu,

I am using spring constructor based injection to instantiate querygateway.

Hi rohit,

I was facing same issue.
I just removed configuration of querygateway and I just autowired querygateway. And that’s worked for me.

Thanks
Siddu

Hi siddu,

i have also autowired the querygateway, and this is working if the queryhandler is present in the same service. but not working if i fire query from some other service.

What do you get (if anything) in your logs?
In the past whenever I have had an issue like this it has been related to serialization problems.

Are you using jackson or xstream and is the serializer able to serialize/deserialize the query correctly?