Code Monkey home page Code Monkey logo

spring-boot-rabbitmq-scs-best-practices's Introduction

Spring Boot - best practices example with RabbitMQ and Spring Cloud Stream

Messaging is often implemented with Spring using outdated/deprecated patterns.

This repo attempts to show an easy way to use RabbitMQ using latest best practices of Spring Boot & Spring Cloud Stream.

The Java code is not RabbitMQ specific. It is bound to a RabbitMQ service through configuration (resources/application.yml) and by declaring the dependency spring-cloud-stream-binder-rabbit.

This app includes:

Install & Run instructions

  • A running RabbitMQ server. (The app will automatically connect to it)
  • ./mvnw clean install
    • mvnw is a Maven wrapper included with Spring Initializr.
  • ./mvnw spring-boot:run

To use the message publisher, POST to /text with the parameter text.

Example: curl -d "text=hi" -X POST http://localhost:8080/text -v

The POST publishes a message to the exchange consumed by the Function bean textToTextWrapperProcessor. This bean is configured for its return value to be published as a message to the exchange which the Consumer bean wrappedTextConsumer consumes from.

See readme-images/example-app-log.txt for an example of running the app with a successful POST receive and messaging, followed by a failed message consumption attempt & dead-lettering.

Dead letter queue

If an exception is thrown during consumption of a message, and autoBindDlq is enabled, the message will be consumed and a copy will be published to a dead-letter queue.

This could happen if the bound function throws an exception or Spring fails to marshall the payload into the object type defined in the signature. The following message is not JSON and will thus fail:

publishing invalid message payload screenshot

This results in the DLQ receiving the failure as a message:

the DLQ

The message:

the DLQ message

Enable with: spring.cloud.stream.rabbit.bindings.<channelName>.consumer.autoBindDlq=true (see resources/application.yml for example)

How to re-create a similar app

  • Spring Initializr
    • Very convenient tool to quickly create a Spring Boot project.
  • Add dependencies spring-cloud-stream and a binder such as spring-cloud-stream-binder-rabbit
  • Add a Function bean and include it in the configured definitions: spring.cloud.stream.function.definition: beanName

To bind to a particular exchange & queue:

spring:
  cloud:
    stream:
      function:
        definition: beanName
      bindings:
        beanName-in-0:
          # Creates a queue named `text.scsExample` bound to `text` exchange
          #  (queue name format is "[destination].[group]")
          destination: text  # name of the AMQP exchange to bind to
          group: scsExample
        beanName-out-0:
          # name of the AMQP exchange to publish to
          # Does not create a queue. If no queues exist but you wish to view published
          #   messages, you can manually create a queue bound to
          #   this exchange in the RabbitMQ UI.
          destination: text.wrapped

Exchange & queue names conventions suggestions

  • Name exchanges after the type of data that is published to them.
  • Avoid the use of environment names in queue or exchange names.
    • Environments should be separated by using multiple virtual hosts or multiple actual RabbitMQ servers.
  • Consider naming queues by combining the exchange name and the name of the app that declared the queue.
    • Example: a device exchange consumed by a Device Service app would be named device.device-service

References

Contributions

Any corrections or suggestions are welcome. Please raise a Github issue or contact the owner directly.

spring-boot-rabbitmq-scs-best-practices's People

Contributors

starlightdreamer avatar

Stargazers

csyangchsh avatar

Watchers

 avatar

Forkers

breedu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.