This reposity contains the source code for the Competing Consumers pattern implementation that is described in the following article.
- Docker and Docker Compose
- WSO2 Integration Studio
- Maven
- Git (optional)
The Integration Studio is the recommended tooling for WSO2 platform products as it provides a rich user experience for creating integration artefacts, debugging, and generating Docker and Kubernetes projects.
The repository contains an Integration Studio project called competing-consumers. Go ahead and import it into the Integration Studio by selecting File -> Import -> Import Existing WSO2 Project into Workspace.
Once you import the project, you'll see three modules as follows.
- competing-consumers-configs Contains MI artefacts for consuming messages from RabbitMQ.
- competing-consumers-composite-exporter Packages the above artifact module as a - deployable Composite Application Archive (CAR) file.
- competing-consumers-docker-exporter An auto-generated Docker project for the module (2). Once built, this module produces a Docker image of a MI instance that consumes messages from RabbitMQ. Required MI configurations are kept in the deployment.toml file inside this module. Once built, it'll be added to the image.
Before running the docker-compose, you need to build the competing-consumers-docker-exporter module to generate a local Docker image.
Double click on the pom.xml inside competing-consumers-docker-exporter project to open the properties page as shown below. Change the Target Repository
, Target Tag
, and other parameters accordingly.
Then, right-click on the module and select Build Docker Image option to commence the image build process.
After a successful build, you'll see a local image generated as follows.
Open a terminal and navigate to the place where you cloned the Git repository mentioned in the prerequisites.
Execute the following command to start the project.
docker-compose up --scale consumer=2
Here, we are using the --scale consumer=2
flag to create two replicas of the consumer
service which deploys a MI container.
Type docker ps
to make sure that three containers are up and running.
Login to RabbitMQ management console using http://localhost:15672 and notice that there are two consumers for a queue called tasks. Those are the two MI containers connected and waiting for messages.
In this example, we don't have a message producer. To keep things simple, let's publish some messages through the RabbitMQ management console.
Click on Publish message and copy and paste the following sample message. Also, add a property called content_type
with the value application/xml
.
Sample message:
<Message>
<ID>1</ID>
</Message>
Click on Publish Message.
Repeat the process to send 4–5 messages to the tasks queue. Use 1,2,3,4,5 as message IDs.
Then you'll notice the following lines printed in the docker-compose logs.
That indicates that RabbitMQ has dispatched messages between two consumers in a round-robin manner. consumer_1 received all the odd numbers while consumer_2 received even numbers.