Introduction to CAP
CAP, a .Net standard library, is designed to manage distributed transactions effectively. It acts as an EventBus while promising lightweight, easy-to-use, and efficient operations. In Service-Oriented Architecture (SOA) or Microservice systems, managing event integration among services can be problematic when relying solely on message queues due to reliability issues. CAP addresses this by using a local message table integrated with existing databases to ensure robustness in message handling, guaranteeing none are lost during distributed interactions.
Architectural Overview
CAP's architecture leverages the Outbox Pattern, ensuring atomicity and resiliency in distributed systems, as detailed in Microsoft's eShop ebook. By doing so, it successfully addresses common challenges in microservice environments, providing a consistent framework for event publishing and subscriptions.
Getting Started
Installation via NuGet
CAP can be effortlessly added to a project using NuGet. Here’s the command for basic installation:
PM> Install-Package DotNetCore.CAP
CAP supports a variety of popular message queues and databases for seamless integration:
- Message Queues: Kafka, RabbitMQ, AzureServiceBus, AmazonSQS, NATS, RedisStreams, Pulsar.
- Databases: SQL Server, MySQL, PostgreSQL, MongoDB.
Configuration
Configuring CAP involves setting it up within the Startup.cs
file. It requires defining the database connectivity and selecting the appropriate message queue for the project. Here's a basic configuration example:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>();
services.AddCap(x =>
{
x.UseEntityFramework<AppDbContext>();
x.UseSqlServer("Your ConnectionStrings");
x.UseRabbitMQ("HostName");
});
}
Publishing and Subscribing
Publishing Events
CAP allows for easy event publishing by leveraging the ICapPublisher
. This can be injected into a controller to send messages, including the support for publishing delayed messages:
public class PublishController : Controller
{
private readonly ICapPublisher _capBus;
public PublishController(ICapPublisher capPublisher)
{
_capBus = capPublisher;
}
public IActionResult AdonetWithTransaction()
{
// Implementation for publishing messages
return Ok();
}
}
Subscribing to Events
Subscriptions can be handled within controller actions or business logic services. By using attributes like [CapSubscribe()]
, methods can be marked to process incoming messages:
public class PublishController : Controller
{
[CapSubscribe("xxx.services.show.time")]
public void CheckReceivedMessage(DateTime datetime)
{
Console.WriteLine(datetime);
}
}
For asynchronous message processing, methods can be defined to return a Task
and include a CancellationToken
.
Dashboard and Monitoring
CAP offers a comprehensive dashboard feature to monitor message flow and status in real-time. It can be installed and configured in your project, providing a visual representation of message transactions. This feature is enhanced with integrations like Consul and Kubernetes for node discovery and management in distributed environments.
Contribution and Licensing
The project encourages contributions through discussions, issues, and code submissions (pull requests). CAP is open-source, licensed under the MIT License, providing developers the flexibility to adapt the library to their needs.
In summary, CAP is a powerful tool for developers managing distributed systems, offering reliable event handling and a simplified interface for microservices architecture.