- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
Introduction
Redis is an in-memory data structure store, commonly used as a message broker, cache, or database. It has support to and you can integrate with that using the Paramore.Brighter.MessagingGateway.Redis package.
Requirement
Before continuing about Redis configuration, let's recap what we already know about Brighter.
Request (Command/Event)
Define messages using IRequest:
public class Greeting() : Event(Guid.NewGuid())
{
public string Name { get; set; } = string.Empty;
}
Translates between Brighter messages and your app objects:
public class GreetingMapper : IAmAMessageMapper<Greeting>
{
public Message MapToMessage(Greeting request)
{
var header = new MessageHeader();
header.Id = request.Id;
header.TimeStamp = DateTime.UtcNow;
header.Topic = "greeting.topic"; // The target topic to be publish
header.MessageType = MessageType.MT_EVENT;
var body = new MessageBody(JsonSerializer.Serialize(request));
return new Message(header, body);
}
public Greeting MapToRequest(Message message)
{
return JsonSerializer.Deserialize<Greeting>(message.Body.Bytes)!;
}
}
Request Handler
Processes incoming messages:
public class GreetingHandler(ILogger<GreetingHandler> logger) : RequestHandler<Greeting>
{
public override Greeting Handle(Greeting command)
{
logger.LogInformation("Hello {Name}", command.Name);
return base.Handle(command);
}
}
Configuring Brighter with Redis
1. Connection Setup
Define Redis connection details:
var connection = new RedisMessagingGatewayConfiguration
{
RedisConnectionString = "localhost:6379?connectTimeout=1000&sendTimeout=1000&",
MaxPoolSize = 10,
MessageTimeToLive = TimeSpan.FromMinutes(10)
};
2. Redis Subscription
Subscribe to a queue:
.AddServiceActivator(opt =>
{
opt.Subscriptions = [
new RedisSubscription<Greeting>(
new SubscriptionName("kafka.greeting.subscription"),
new ChannelName("greeting.queue"),
new RoutingKey("greeting.topic"),
makeChannels: OnMissingChannel.Create
),
];
opt.ChannelFactory = new ChannelFactory(
new RedisMessageConsumerFactory(connection)
);
})
3. Redis Producer Configuration
Publish events to a queue:
.UseExternalBus(new RedisProducerRegistryFactory(connection, [
new RedisMessagePublication
{
Topic = new RoutingKey("greeting.topic"),
MakeChannels = OnMissingChannel.Create
}
}).Create());
Limitations and Considerations
1. ServiceStack.Redis Licensing
Brighter's Redis integration relies on ServiceStack.Redis.Core, which imposes a concurrent message consumption limit without a commercial license. This can bottleneck high-throughput systems.
2. No Redis Streams Support (Yet):
Brighter currently uses Redis lists for queuing, not the more modern Redis Streams (which offers persistence and stream processing). A future update will add Redis Streams support for better scalability.
Conclusion
Integrating Brighter with Redis enables lightweight, scalable messaging in .NET applications. By leveraging Redis’ in-memory speed and Brighter's abstraction layer.
For production use, validate Redis configurations against Brighter's latest documentation, as support for Redis Streams and alternative clients may evolve.
Reference
Redis is an in-memory data structure store, commonly used as a message broker, cache, or database. It has support to and you can integrate with that using the Paramore.Brighter.MessagingGateway.Redis package.
Requirement
- .NET 8 or superior
- A .NET project with these NuGet packages
- : Enables Redis integration.
- : Enable register Brighter with Microsoft DI.
- : Hosts Brighter as a background service.
- : For structured logging (optional but recommended).
Before continuing about Redis configuration, let's recap what we already know about Brighter.
Request (Command/Event)
Define messages using IRequest:
public class Greeting() : Event(Guid.NewGuid())
{
public string Name { get; set; } = string.Empty;
}
- Commands: Single-recipient operations (e.g., SendEmail).
- Events: Broadcast notifications (e.g., OrderShipped).
Translates between Brighter messages and your app objects:
public class GreetingMapper : IAmAMessageMapper<Greeting>
{
public Message MapToMessage(Greeting request)
{
var header = new MessageHeader();
header.Id = request.Id;
header.TimeStamp = DateTime.UtcNow;
header.Topic = "greeting.topic"; // The target topic to be publish
header.MessageType = MessageType.MT_EVENT;
var body = new MessageBody(JsonSerializer.Serialize(request));
return new Message(header, body);
}
public Greeting MapToRequest(Message message)
{
return JsonSerializer.Deserialize<Greeting>(message.Body.Bytes)!;
}
}
Request Handler
Processes incoming messages:
public class GreetingHandler(ILogger<GreetingHandler> logger) : RequestHandler<Greeting>
{
public override Greeting Handle(Greeting command)
{
logger.LogInformation("Hello {Name}", command.Name);
return base.Handle(command);
}
}
Configuring Brighter with Redis
1. Connection Setup
Define Redis connection details:
var connection = new RedisMessagingGatewayConfiguration
{
RedisConnectionString = "localhost:6379?connectTimeout=1000&sendTimeout=1000&",
MaxPoolSize = 10,
MessageTimeToLive = TimeSpan.FromMinutes(10)
};
2. Redis Subscription
Subscribe to a queue:
.AddServiceActivator(opt =>
{
opt.Subscriptions = [
new RedisSubscription<Greeting>(
new SubscriptionName("kafka.greeting.subscription"),
new ChannelName("greeting.queue"),
new RoutingKey("greeting.topic"),
makeChannels: OnMissingChannel.Create
),
];
opt.ChannelFactory = new ChannelFactory(
new RedisMessageConsumerFactory(connection)
);
})
3. Redis Producer Configuration
Publish events to a queue:
.UseExternalBus(new RedisProducerRegistryFactory(connection, [
new RedisMessagePublication
{
Topic = new RoutingKey("greeting.topic"),
MakeChannels = OnMissingChannel.Create
}
}).Create());
Limitations and Considerations
1. ServiceStack.Redis Licensing
Brighter's Redis integration relies on ServiceStack.Redis.Core, which imposes a concurrent message consumption limit without a commercial license. This can bottleneck high-throughput systems.
2. No Redis Streams Support (Yet):
Brighter currently uses Redis lists for queuing, not the more modern Redis Streams (which offers persistence and stream processing). A future update will add Redis Streams support for better scalability.
Conclusion
Integrating Brighter with Redis enables lightweight, scalable messaging in .NET applications. By leveraging Redis’ in-memory speed and Brighter's abstraction layer.
For production use, validate Redis configurations against Brighter's latest documentation, as support for Redis Streams and alternative clients may evolve.
Reference