Try PubSub+

Sample code for request/reply design by using Spring Cloud Stream

vadivelanvadivelan Member Posts: 1

Hi, I am new to solace and my application is using solace request/reply by using old approach.

I have planned to use spring cloud stream in my project for the below scenarios.

please refer the below use case and help me with samples:

I have three microservice services in my application

ServiceA -- > Request the message to ServiceB processing and waiting for the response.
ServiceB --> Read the payload from topic/queue and apply the some business rule validation.
ServiceB is publish the message to ServiceC if business validation is success else ServiceB is reply the error message to ServiceA
ServiceC --> ServiceC is read the message and apply some business process then send the final response to ServiceA.

Answers

  • girigiri Member, Administrator, Employee Posts: 29 admin

    Hi @vadivelan, your requirement fits the classic request-response messaging pattern.

    Synchronous request-response may not fit in the scheme of SCS as it is more geared for asynchronous interactions.

    However, request-response can be implemented asynchronously, with a response being returned at some unknown later time but the request, response, and any other intermediate messages all carry the same correlation-id. This is something that can be controlled in the code. This is a “sync over async”, or “sync/async” pattern.

  • girigiri Member, Administrator, Employee Posts: 29 admin

    And of course, you can use the solace header property 'solace_replyTo' to control message routing. Please check out https://github.com/SolaceProducts/solace-spring-cloud/tree/master/solace-spring-cloud-starters/solace-spring-cloud-stream-starter for more information.

  • marcmarc Member, Administrator, Moderator, Employee Posts: 504 admin

    @giri is correct, request-reply isn't explicitly supported in Spring Cloud Stream but you can kind of roll your own.

    I haven't yet done it myself, but it should work if you do this (I don't think I'm missing anything..):

    • Use the solace_correlationId and solace_replyTo headers
    • Have you requestor pre-define a replyTo topic space and subscribe to it..for example: domain/app/app-instance/reply/> (Follow topic best practices that you can find elsewhere)
    • Set the solace_correlationId to a unique value and the solace_replyToin the msg header to something like domain/app/app-instance/reply/<correlation-id>
    • Have the replier respond to the solace_replyTo topic using the "dynamic publishing" capabilities of spring cloud stream which you can learn more about in this codelab. I'd probably use the BindersHeaders.TARGET_DESTINATION option since each response would be on a unique topic...no reason to have spring cache the channel info.

    Hope that helps!

Sign In or Register to comment.