CAP takes you easily play ASP.NETCore message queue
What CAP that?
CAP is developed by Yang Xiaodong large garden of God our plan to pull off a distributed transaction, a thousand stars is the first project (now 1656 Start) .Net Core Community in with the lightweight, easy to use, high performance characteristics.
https://github.com/dotnetcore/CAP
This blog is mainly for this ease of use, expand the narrative, take a look at how the CAP in conjunction with EF Core and RabbitMQ led easily into the white world distributed message queue.
ready
First, you need to build a RabbitMQ system set up process which will not be described, if you feel trouble, you can use my setting up of.
HostName: coderayu.cn UserName: the Guest Password: the Guest (only used as experimental, is not responsible for loss of data)
Creating Asp.Net Core project, and the introduction of package Nuget
You can run the following command to install CAP in your project.
PM> Install-Package DotNetCore.CAP
If you are using a message queue Kafka, you can:
PM> Install-Package DotNetCore.CAP.Kafka
If you are using a RabbitMQ message queue, you can:
PM> Install-Package DotNetCore.CAP.RabbitMQ
CAP provides Sql Server, MySql, PostgreSQL as a database storage expansion:
// 按需选择安装你正在使用的数据库PM> Install-Package DotNetCore.CAP.SqlServer PM> Install-Package DotNetCore.CAP.MySql PM> Install-Package DotNetCore.CAP.PostgreSql
创建DbContext
因为我采用的是EF Core,所以首先要创建一个DbContext上下文,代码如下:
Startup配置
首先需要在ConfigureServices函数中进行相关服务的注入,对应的操作和功能解释如下:
最后还要再Congiure中启用CAP中间件
利用EF Core生成CAP数据库
再程序包管理控制台中依此输入以下命令行
PM> Add-Migration Init
PM> update-database
如果成成功执行,那么打开数据库,就可以看到用来存储CAP发送和接收数据的表格了。
表格中每列的含义如下:
消息的发送和订阅
我们直接在ValuesController的基础上进行改造。
在 Controller 中注入 ICapPublisher
然后使用 ICapPublisher
进行消息发送
发送消息
订阅消息
Run
启动程序后,首先看到CAP启动成功
紧随其后,消费者也就是我们的订阅方法在RabbitMQ服务器上注册成功。
发送消息,发送成功,如下
发送后,立即在控制台看到了订阅方法输出的结果。
消息的失败重试
在订阅方法中,如果抛出异常,那么CAP就会认为该条消息处理失败,会自动进行重试,重试次数在前方已经进行了配置。
我们把订阅方法做一个改动,打印接收的信息到控制台中,并抛出异常
可以看到,立即进行了三次重试
But in front of us fail to set the number of retries is five times why here only three times to retry it? Xiaodong is not to be called up to change a BUG it ? of course not.
Observed, CAP retry three times before is carried out immediately, but later retry is carried out from time to time, when in the course of a distributed communication, there may be a problem does not solve the immediate repair, it may be too a certain amount of time, the system automatically restored, such as network jitter.
CAP dashboard
The success of the five messages sent, received successfully dealt with three, two treatment failure, treatment failed task, we can re-direct consumption in the panel, can be described as very convenient.
At the same time, failure to process messages, click the message ID, you can view the contents of the message and the reason for the exception.
CAP so strong, so big on this message queue product operation So Easy, we learned CAP, may also boasted that, I also know it distributed processing tasks .
Thank Xiaodong developed such a strong project, and thank .Net Core Community.
Reference CAP Github wiki
https://github.com/dotnetcore/CAP/wiki
This blog Demo Code
https://github.com/liuzhenyulive/CAP.Demo
related articles:
.NetCore Cap combined with RabbitMQ implement the message Subscribe
Preliminary using .NET Core micro-services CAP of the open source project
Distributed transaction, EventBus solution: CAP document [Chinese]
Original Address: https://www.cnblogs.com/CoderAyu/p/9527012.html
.NET community news, good depth article, article No. Welcome to the public summary http://www.csharpkit.com