Microservices tests
This is a repo for microservice tests using rabbitmq.
Instructions
- Install rabbitmq
- Clone this repo
- go into the cloned project directory
cd microservices-tests
- do bundle install
bundle install
- cd into lib, where all the exectuables are, from now on, you will execute any commands in different terminals, keeping the processes (microservices) alive.
cd lib
- edit configuration.rb to set the connection url to the rabbitmq server if necessary (just in case you are using non-default values for the ports, user, password, etc.)
- run the keyboard client, so you can type messages, hit enter to send. Messages will be queued in rabbitmq until a consumer appears
ruby keyboard_client.rb
- run the gateway service which takes messages and replays them to the workers and logger through a fanout exchange.
ruby gateway.rb
- run ANY number of workers. In order to control how much time workers perform a task, just type dots "." in the message you send using the keyboard client, example: "Hello!...." will take 4 seconds to process, because it has 4 dots ".".
ruby worker.rb
- Run the receiver, which is the last stop for processed messages. Processed messages are the ones that have any digits in the message body, complying to the pattern /\d/. Processed messages are upcased.
ruby receiver.rb
- Run the logger, which will log the important interactions in the system. The other microservices send messages to the "logger" queue for this.
ruby logger.rb
- Finally, in order to not input messages manually, run:
ruby auto_client.rb
This will basically send a message to "queue_a" every second so you can see how the overall system works by looking a the various console outputs for each of the microservices.
Interesting thing you can do now
- Leave the system running and see how it behaves (basically is a message passing game)
- Using the keyboard client, put various messages with multiple dots "." to simulate load for the workers.
- Turn off services randomly and rerun them, you will see how messages are queued and processed when the microservices become available again.
- Enjoy!
Running each micro-service in docker container
This steps assume you have docker installed and working in your system.
Run RabbitMQ Server
- Create and image for rabbitmq server:
docker build -t="dockerfile/rabbitmq" github.com/dockerfile/rabbitmq
- Run a container from that image with:
run --name rabbitmq -d -p 5672:5672 -p 15672:15672 dockerfile/rabbitmq
Run microservices in docker
- Create and image for micro-services (with ruby 2.1.1 and microservices bundle) doing:
docker build -t="microservices/client" .
- Run each microservice in a terminal as in steps 7 to 11 of the Instructions part, replacing microservice.rb with the corresponding ruby file.
docker run --link rabbitmq:amq -t -i microservices/client ruby lib/microservice.rb