This repository focuses on Microservice Messaging using MassTransit and RabbitMQ, and on Testing Microservices using Pact Net for contract testing. The work is based on Chapter 5 (Microservice Messaging) and Chapter 7 (Testing Microservices) from the book Pro Microservices in .NET 6.
- Introduction
- Technologies Used
- Project Structure
- Getting Started
- Running the Project
- Testing
- License
This project demonstrates two key aspects of building microservices in .NET Core:
- Microservice Messaging using MassTransit and RabbitMQ to enable asynchronous communication between microservices.
- Contract Testing using Pact Net and xUnit to ensure microservices adhere to agreed-upon communication contracts over HTTP.
These implementations follow Chapter 5 and Chapter 7 of Pro Microservices in .NET 6.
- .NET Core: A cross-platform framework for building modern applications.
- MassTransit: A message-based application framework for .NET.
- RabbitMQ: A messaging broker for communication between services.
- Pact Net: A contract testing framework that ensures services follow consumer-driven contracts.
- xUnit: A popular testing framework for .NET.
Focuses on implementing message-based communication using MassTransit and RabbitMQ. The implementation of microservice messaging includes the following projects (contained within MessageMicroservices solution):
- InvoiceMicroservice: a microservice which publishes a message about the newly created invoices (producer).
- PaymentMicroservice: a microservice which receives the message that an invoice was created (consumer). Serves as a quick example of a downstream microservice that reacts to the creation of an invoice.
- TestClient: a test client which takes the place of a monolith that interacts with the microservices to demonstrate message publishing and consumption.
MassTransit handles the communication, and RabbitMQ is used as the message broker.
Focuses on contract testing between microservices using Pact Net framework. The consumer-driven contract testing approach ensures that both services can work together by adhering to predefined communication rules. Communication between microservices is achieved either over HTTP protocol, using REST APIs, or through message queues.
-
Contract-Testing solution contains two microservice projects (OrderSvc-Consumer and DiscountSvc-Provider) and their appropriate test projects.
- ConsumerTests contains the following:
- DiscountSvcMock: a mock service which will be called from the test, instead of calling the real service
- DiscountSvcTests: a test that is reliant on the mock service. Because of the mock service, there is no need to run the service itself. Running the test will leverage Pact Net and the mock microservice, and will generate the contract file (see example).
- ProviderTests uses the information from the generated contract file (see example) to call the Discount microservice and confirm that the contract has not broken.
- ConsumerTests contains the following:
-
MessageMicroservices solution contains two microservice projects (for details see here) and their appropriate test projects:
- .NET Core SDK 6.0+
- RabbitMQ
- Docker (optional, for running RabbitMQ in a container)
- Pact Net
Clone the repository:
git clone https://github.com/LykourgosS/microservice-messaging-testing.git
cd microservice-messaging-testing
-
Navigate to the appropriate folder:
cd MessageMicroservices
-
Start RabbitMQ locally using Docker or install it directly. To run with Docker:
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
-
Launch the microservices and the test application, allowing you to observe message communication through RabbitMQ. Execute following commands in separate terminals:
dotnet run --project .\InvoiceMicroservice\InvoiceMicroservice.csproj
dotnet run --project .\PaymentMicroservice\PaymentMicroservice.csproj
dotnet run --project .\TestClient\TestClient.csproj
Note
See example video for messaging here.
Following commands will execute the Pact Net contract tests to ensure both services comply with the communication contract.
-
-
Navigate to the
Contract-Testing
folder:cd Contract-Testing
-
Run test for consumer service (to generate the contract file):
cd ConsumerTests dotnet test
-
Run provider service (i.e. DiscountSvc-Provider) in a separate terminal:
cd DiscountSvc-Provider dotnet run
-
And now that the provider service is up and running, run test for it (to check if contact has broken based on the generated contract file):
cd .. cd ProviderTests dotnet test
-
-
-
Navigate to the
MessageMicroservices
folder:cd MessageMicroservices
-
Run test for consumer service (i.e. PaymentMicroservice):
cd ConsumerTests dotnet test
-
Run test for producer service (i.e. InvoiceMicroservice):
cd .. cd ProducerTests dotnet test
-
Note
See example video for testing here.
Warning
Due to a known issue with ruby the cloned repository should be located close to the root C:\
(suggested solutions can be found here and here).
This project is licensed under the MIT License. See the LICENSE file for details.