Skip to content

This repository focuses on Microservice Messaging using MassTransit and RabbitMQ, and on Testing Microservices using Pact Net for contract testing in .NET 6+. (mpsp)

License

Notifications You must be signed in to change notification settings

LykourgosS/microservice-messaging-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microservice Messaging and Contract Testing in .NET Core

.NET RabbitMQ Conventional Commits

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.

Table of Contents

  1. Introduction
  2. Technologies Used
  3. Project Structure
  4. Getting Started
  5. Running the Project
  6. Testing
  7. License

Introduction

This project demonstrates two key aspects of building microservices in .NET Core:

  1. Microservice Messaging using MassTransit and RabbitMQ to enable asynchronous communication between microservices.
  2. 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.

Technologies Used

  • .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.

Project Structure

Microservice Messaging

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.

Testing Microservices

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.

  • Over HTTP Protocol

    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.
  • Through Message Queues

    MessageMicroservices solution contains two microservice projects (for details see here) and their appropriate test projects:

Getting Started

Prerequisites

Installation

Clone the repository:

git clone https://github.com/LykourgosS/microservice-messaging-testing.git
cd microservice-messaging-testing

Running the Project

  1. Navigate to the appropriate folder:

    cd MessageMicroservices
  2. 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
  3. 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.

Testing

Following commands will execute the Pact Net contract tests to ensure both services comply with the communication contract.

  • Over HTTP Protocol

    1. Navigate to the Contract-Testing folder:

      cd Contract-Testing
    2. Run test for consumer service (to generate the contract file):

      cd ConsumerTests
      dotnet test
    3. Run provider service (i.e. DiscountSvc-Provider) in a separate terminal:

      cd DiscountSvc-Provider
      dotnet run
    4. 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
  • Through Message Queues

    1. Navigate to the MessageMicroservices folder:

      cd MessageMicroservices
    2. Run test for consumer service (i.e. PaymentMicroservice):

      cd ConsumerTests
      dotnet test
    3. 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).

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

This repository focuses on Microservice Messaging using MassTransit and RabbitMQ, and on Testing Microservices using Pact Net for contract testing in .NET 6+. (mpsp)

Topics

Resources

License

Stars

Watchers

Forks

Languages