This article is a mirror article of machine translation, please click here to jump to the original article.

View: 57382|Reply: 3

[ASP.NET] ASP.NET Core (five) is based on CAP distributed transactions

[Copy link]
Posted on 2021-3-17 18:12:50 | | | |
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

2PC &&&3PC of distributed transactions
https://www.itsvse.com/thread-9591-1-1.html

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:

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html


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:

【Practical Combat】RabbitMQ installation tutorial under Windows
https://www.itsvse.com/thread-4630-1-1.html

【Practice】RabbitMQ installs the web management plugin
https://www.itsvse.com/thread-4631-1-1.html
Adding an account is also omitted, please refer to:

【Practice】rabbitMQ console to add account information
https://www.itsvse.com/thread-4655-1-1.html
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.

Interface idempotency issues
https://www.itsvse.com/thread-6010-1-1.html

Finally, attach the source code:

Tourists, if you want to see the hidden content of this post, pleaseReply






Previous:CSV online conversion of Markdown syntax
Next:HttpClient POST directly to send byte byte (gzip) compression requests
Posted on 2021-3-18 18:11:20 |
Come and take a look, thanks for sharing
Posted on 2021-9-22 20:42:18 |
Learn to learn...
Posted on 2021-12-31 15:08:21 |
Learning
Very good, very nice
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com