Skip to content

integration redis setup

Brian Greco edited this page Mar 22, 2023 · 3 revisions

IMAGERedis: Plugin Setup

The NetFusion.Integration.Redis plugin is based on StackExchange.Redis Nuget and provides access to the database and allows publishing commands using Redis pub/sub.

dotnet add package NetFusion.Integration.Redis

IMAGE The same IMessagingService methods used to dispatch in-process domain-events are used for dispatching to Redis pub/sub.. This allows changing an application's behavior without having to change the calling code and provides a consistent API for all messaging implementations (RabbitMQ, Azure ServiceBus, Redis...). The responsibility of how a message is published is moved from the code dispatching the message to a routing based on the message's type.


Supported Features

  • Based on StackExchange.Redis library
  • Bootstraps like all other plugins providing consistency between microservices
  • Publishing of Domain-Events to channels
  • Provides access to services that can be injected into other components

Adding Redis Plugin to Microservice

The following assumes the Microservice template, provided by NetFusion, was used to create the initial microservice based solution. Execute the following to create a solution using the template. First, install the template by executing the following:

dotnet new install NetFusion.Templates

Create a directory for the example:

mkdir Examples.Redis

Execute the following to generate the solution within the directory:

cd Examples.Redis
dotnet new netfusion-microservice -p 5003
cd src/
dotnet build

The solution containing the completed Redis examples can be found here:

NetFusion-Examples/Examples/Source/Examples.Redis

Add RabbitMQ Nuget Package

Since the use of RabbitMQ is an infrastructure concern, add a reference to the NetFusion.Infrastructure.Redis Nuget containing the plugin to the infrastructure project as follows:

dotnet add ./src/Components/Examples.Redis.Infra/Examples.Redis.Infra.csproj package NetFusion.Integration.Redis

Register Plugin

Edit the Program.cs file for the Microservice's WebApi project to add the plugin to the composite-application. Add the following lines as shown below in the complete code listing:

// Add Plugins to the Composite-Container:
builder.Services.CompositeContainer(builder.Configuration, new SerilogExtendedLogger())
    .AddRedis()
    .Compose();

Add Serialization Nuget Package

The Redis plugin requires an implementation of ISerializationManager to be registered for serializing and deserializing sent and received messages based on content-type.

dotnet add ./src/Components/Examples.Redis.Infra/Examples.Redis.Infra.csproj package NetFusion.Services.Serialization

The registration can be done in the Compose method which is the last place services can be registered as shown below in the complete code listing. After registering the plugin and adding the serialization manager, the completed bootstrap code is a follows:

Completed Bootstrap Code

// Add Plugins to the Composite-Container:
builder.Services.CompositeContainer(builder.Configuration, new SerilogExtendedLogger())
    .AddSettings()
    .AddRedis()
    .AddPlugin<InfraPlugin>()
    .AddPlugin<AppPlugin>()
    .AddPlugin<DomainPlugin>()
    .AddPlugin<WebApiPlugin>()
    .Compose(s =>
    { 
        s.AddSingleton<ISerializationManager, SerializationManager>();
    });

Configuration Settings

Define the configuration section within app settings.json used by the plugin:

{
  "logging": {
    "seqUrl": "http://seq:5341"
  },
  "netfusion": {
    "redis": {
      "connections": {
        "testDb": {
          "defaultDatabaseId": 1,
          "endPoints": [
            {
              "host": "localhost",
              "port": "6389"
            }
          ]
        }
      }
    }
  }
}

Database Connections

Each database is identified by a name and used within code to reference a specific database. Redis uses numbers to identify and partition the server into separate databases. While this is optional in Redis it is required by the plugin. The above indicates that the database named 'testdb' will be associated with the database having an identifier of 1.

Running SEQ and Redis in Docker

The above configuration file specifies connections based on instances of SEQ and RabbitMQ running within Docker. To run the examples locally, the following script can be executed to deploy both services to docker:

cd NetFusion-Examples/Examples/Source/Examples.Redis/docker
docker-compose up -d

When completed, the following can be executed to remove the services running in Docker:

cd NetFusion-Examples/Examples/Source/Examples.Redis/docker
docker-compose down
docker volume rm dev-seq_data
docker volume rm dev-redis_data

The SEQ log Web interface can be found here: http://localhost:8051/

The Redis interface can be found here: http://localhost:8082

The example microservice used within the examples can be executed as follows:

cd Examples.Redis/src/Examples.Redis.WebApi/
dotnet run
Clone this wiki locally