Search code examples
rabbitmqstompspring-cloud-stream

Change default exchangeType as fanout through Spring cloud stream or rabbitmq server config


I have two microservices Student and Teacher

in Student microservice I have created MessageSink for exchange XYZ

@Input("XYZ")
SubscribableChannel xyz();

and in Teacher microservice I have configured exchange XYZ as a fanout

application.properties

 spring.cloud.stream.rabbit.bindings.XYZ.producer.exchangeType=fanout
 spring.cloud.stream.bindings.XYZ.contentType=application/json

But problem I am facing here is Student service is starting before Teacher service and it is creating XYZ exchange with type Topic.

To tackle this I have added exchangeType in both the services (i.e consumer as well as producer). As services are growing in numbers, these configurations are growing in numbers.

I want to change the default exchangeType as fanout, so following are few questions.

  1. is there any way to change default exchangeType as fanout instead of topic in spring-cloud-stream?
  2. Is there any way to change default exchangeType through rabbit-mq configuration?
  3. As a work around I planning to keep exchangeType as topic and routing key #. But the problem with this approach is stomp client is creating queue for every browser host with queue name as stomp-subscription-randomString and blank routing key. So is there any way to provide routing key while subscribing to stream? I am using /exchange/exchangeName as exchange URL

Solution

  • There is some documentation on the matter: https://docs.spring.io/spring-cloud-stream/docs/Elmhurst.RELEASE/reference/htmlsingle/#_configuration_options_4

    You may consider to play with this option:

    exchangeType

    The exchange type: direct, fanout or topic for non-partitioned destinations and direct or topic for partitioned destinations.

    Default: topic.

    Therefore it will look in our config like:

    spring.cloud.stream.rabbit.bindings.XYZ.consumer.exchangeType=fanout
    

    There are some other options, like: declareExchange=false and bindingRoutingKey to consider as well.