.net core 集成事件,分布式事务解决方案 电脑版发表于:2020/10/29 13:54 ![.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)