CAP principles
The CAP principle, also known as the CAP theorem, refers to the consistency, availability, and partition tolerance in a distributed system. The CAP principle states that these three elements can only achieve two points at the same time.It is impossible to take all three at the same time。
The essence of the CAP principle is either AP, CP, or AC, but there is no CAP. If there is no copy of the data in a distributed system, then the system must meet the strong consistency condition, because there is only a unique data, there will be no data inconsistency, at this time the two elements C and P are present, but if the system has a network partition condition or downtime, it will inevitably lead to some data being inaccessible, and the availability condition cannot be met, that is, the CP system is obtained in this case, butCAP cannot be satisfied at the same time。
Review:
DotNetCore.CAP
CAP is an event bus and implementation in a distributed system (SOA, MicroService).Final Consistency(Distributed Transactions) is an open-source C# library that is lightweight, high-performance, and easy to use.
GitHub address:The hyperlink login is visible.
Dotnet CAP has all the features of Event Bus, and CAP provides a more streamlined way to handle publishing/subscribing in EventBus.
MediatR is an in-process message subscription and publishing framework that provides the Send method for publishing to a single handler and the Publish method for publishing to multiple handlers, which is very convenient to use. Currently, .NET Framework4.5, . NET Stardand1.3、. NET Stardand 2.0 and other versions, which can be used across platforms.
ASP.NET Core uses the MediatR intermediary model
https://www.itsvse.com/thread-9272-1-1.html Architecture preview
CAP supports message queues such as Kafka, RabbitMQ, AzureServiceBus, AmazonSQS, etc., and CAP provides extensions for Sql Server, MySql, PostgreSQL, and MongoDB as database storage.
This article uses RabbitMQ and SQL Server as message queues and storage.
Install RabbitMQ
For specific installation tutorials, please refer to:
Adding an account is also omitted, please refer to:
I added the test account and cap Virtual Hosts myself, as shown below:
Otherwise, the error will be as follows:
ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
None of the specified endpoints were reachable
.NET Core integrates CAP
First, create a new ASP.NET Core project that is both the sender and the receiver. Use the nuget command to install the package like this:
In startup, configure the service method ConfigureServices as follows:
You can visit the website /cap address to view the dashboard as shown below:
Data persistence: Cap willAutomatic creation"Published" and "Received" are two local database tables
CREATE TABLE [cap]. [Published] ( [Id] [bigint] NOT NULL, [Version] [nvarchar] (20) NOT NULL, [Name] [nvarchar] (200) NOT NULL, [Content] [nvarchar] (max) NULL, [Retries] [int] NOT NULL, [Added] [datetime2] (7) NOT NULL, [ExpiresAt] [datetime2] (7) NULL, [StatusName] [nvarchar] (50) NOT NULL, CONSTRAINT [PK_cap. Published] PRIMARY KEY CLUSTERED
( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO CREATE TABLE [cap]. [Received] ( [Id] [bigint] NOT NULL, [Version] [nvarchar] (20) NOT NULL, [Name] [nvarchar] (200) NOT NULL, [Group] [nvarchar] (200) NULL, [Content] [nvarchar] (max) NULL, [Retries] [int] NOT NULL, [Added] [datetime2] (7) NOT NULL, [ExpiresAt] [datetime2] (7) NULL, [StatusName] [nvarchar] (50) NOT NULL, CONSTRAINT [PK_cap. Received] PRIMARY KEY CLUSTERED
( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO
The HomeController controller method is as follows:
When the user registers successfully, 3 messages with different topics will be sent, and then the subscriber will consume them.
When CAP starts, it will create a default consumer group, if multiple consumers of the same consumer group consume the same topic message,Only one consumer will be executed。 On the contrary,If the consumers are all in different consumer groups, all consumers are executed。
Creating a new .NET Core console project, as a subscriber (consumer), or referencing the package, the dashboard can be ignored.
If it is in the Controller, add [CapSubscribe("")] directly to subscribe to the relevant messages.
If your method is not in the Controller, then the class you subscribe to needs to inherit ICapSubscribe and then add the [CapSubscribe("")] tag. The code is as follows:
Open the subscription client and try to access http://localhost:28116/Home/UserRegister to send messages, the effect is as follows:
Both the console and the controller receiver are triggered, as shown in the figure below:
Try to throw an exception manually in the receiving message method, the code is as follows:
CAP will automatically retry the method,The number of retries after failure is 50 by default, and the retry interval after failure is 60 seconds by default, as shown in the figure below:
The framework cannot be 100% sure that the message is executed only onceTherefore, in some key scenarios, the message side pays attention to business deduplication in the process of method implementation.
Finally, attach the source code:
Tourists, if you want to see the hidden content of this post, please Reply
|