首页
视频
资源
登录
原
.net core 集成事件,分布式事务解决方案
7733
人阅读
2020/10/29 13:54
总访问:
2658874
评论:
1
收藏:
0
手机
分类:
.net后台框架
![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#.net core 集成事件,分布式事务解决方案 [TOC] <br/> 简单介绍 ------------ tn>CAP是基于.Net标准的库,该库是处理分布式事务的解决方案,具有EventBus功能,它轻巧,易于使用且高效。 在构建SOA或MicroService系统的过程中,我们通常需要使用事件来集成每个服务。在此过程中,简单使用消息队列并不能保证可靠性。CAP采用与当前数据库集成的本地消息表程序来解决在分布式系统相互调用的过程中可能发生的异常。它可以确保事件消息在任何情况下都不会丢失。 您也可以将CAP用作EventBus。CAP提供了一种更简单的方法来实现事件发布和订阅。在订阅和发送过程中,您不需要继承或实现任何接口。 安装与代码构建 ------------ tn>相关资源包 ```bash DotNetCore.CAP DotNetCore.CAP.RabbitMQ DotNetCore.CAP.SqlServer DotNetCore.CAP.Dashboard ``` tn>定义相关实体 ><font style="color:#2ecc71;font-weight:bold;">OrderPaymentSucceededIntegrationEvent.cs</font> ```csharp public class OrderPaymentSucceededIntegrationEvent { public OrderPaymentSucceededIntegrationEvent(long orderId) => OrderId = orderId; public long OrderId { get; } } ``` tn>定义相关服务 ><font style="color:#2ecc71;font-weight:bold;">OrderPaymentSucceededIntegrationEvent.cs</font> ```csharp public interface ISubscriberService { void OrderPaymentSucceeded(OrderPaymentSucceededIntegrationEvent @event); } public class SubscriberService : ISubscriberService, ICapSubscribe { [CapSubscribe("OrderPaymentSucceeded")] public void OrderPaymentSucceeded(OrderPaymentSucceededIntegrationEvent @event) { //处理队列中消息的地方 } } ``` tn>注入服务 ><font style="color:#2ecc71;font-weight:bold;">Startup.cs</font> ```csharp services.AddTransient<ISubscriberService, SubscriberService>(); services.AddDbContext<TestDBContext>(option => option.UseSqlServer(Configuration.GetValue<string>("MSSQLServer"))); services.AddCap((option) => { option.UseEntityFramework<TestDBContext>(); option.UseRabbitMQ(options => { Configuration.GetSection("RabbitMQ").Bind(options); }); option.UseDashboard(options => { options.AppPath = "applicationpath"; options.PathMatch = "/cap"; }); }); ``` tn>在分布式环境中,内置的仪表板将Consul集成为节点发现,在实现网关代理功能的同时,还可以轻松查看节点或其他节点数据,就像您正在访问本地资源一样。简单点就像上面那样。 ```csharp services.AddCap(x => { //... // Register Dashboard x.UseDashboard(); // Register to Consul x.UseDiscovery(d => { d.DiscoveryServerHostName = "localhost"; d.DiscoveryServerPort = 8500; d.CurrentNodeHostName = "localhost"; d.CurrentNodePort = 5800; d.NodeId = 1; d.NodeName = "CAP No.1 Node"; }); }); ``` tn>查看配置 ><font style="color:#f1c40f;font-weight:bold;">appsettings.json</font> ```json { "MSSQLServer": "Server=xx;Database=TestDB;User ID=xx;Password=xxxxxx;", "RabbitMQ": { "HostName": "xx.xx.xx.xx", "UserName": "guest", "Password": "guest", "VirtualHost": "/", "ExchangeName": "geek_queue" }, } ``` tn>在控制器中定义消费接口 ><font style="color:#2ecc71;font-weight:bold;">WeatherForecastController.cs</font> ```csharp [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private readonly ICapPublisher _capBus; public WeatherForecastController(ICapPublisher capPublisher) { _capBus = capPublisher; } [HttpGet] public async Task<string> Get() { _capBus.Publish("OrderPaymentSucceeded",new OrderPaymentSucceededIntegrationEvent(10L)); return await Task.FromResult<string>("ok"); } } ``` 运行测试 ------------ tn>添加消息 ![](https://img.tnblog.net/arcimg/hb/276bac28e2ca4d90b2be6da433030779.png) tn>消费 ![](https://img.tnblog.net/arcimg/hb/53c8ad982cee43d4807eecc0b2cc1046.png) tn>查看 Dashboard (http://localhost:5000/cap) ![](https://img.tnblog.net/arcimg/hb/e2477b67cf8f41ecb6cd6afb2b91300f.png) tn>查看订阅列表 ![](https://img.tnblog.net/arcimg/hb/0420dda3556e43558a0f55bd73264157.png)
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
171篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
4篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
5篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术