
.net core 集成事件,分布式事务解决方案
简单介绍
CAP是基于.Net标准的库,该库是处理分布式事务的解决方案,具有EventBus功能,它轻巧,易于使用且高效。
在构建SOA或MicroService系统的过程中,我们通常需要使用事件来集成每个服务。在此过程中,简单使用消息队列并不能保证可靠性。CAP采用与当前数据库集成的本地消息表程序来解决在分布式系统相互调用的过程中可能发生的异常。它可以确保事件消息在任何情况下都不会丢失。
您也可以将CAP用作EventBus。CAP提供了一种更简单的方法来实现事件发布和订阅。在订阅和发送过程中,您不需要继承或实现任何接口。
安装与代码构建
相关资源包
DotNetCore.CAP
DotNetCore.CAP.RabbitMQ
DotNetCore.CAP.SqlServer
DotNetCore.CAP.Dashboard
定义相关实体
OrderPaymentSucceededIntegrationEvent.cs
public class OrderPaymentSucceededIntegrationEvent
{
public OrderPaymentSucceededIntegrationEvent(long orderId) => OrderId = orderId;
public long OrderId { get; }
}
定义相关服务
OrderPaymentSucceededIntegrationEvent.cs
public interface ISubscriberService
{
void OrderPaymentSucceeded(OrderPaymentSucceededIntegrationEvent @event);
}
public class SubscriberService : ISubscriberService, ICapSubscribe
{
[CapSubscribe("OrderPaymentSucceeded")]
public void OrderPaymentSucceeded(OrderPaymentSucceededIntegrationEvent @event)
{
//处理队列中消息的地方
}
}
注入服务
Startup.cs
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";
});
});
在分布式环境中,内置的仪表板将Consul集成为节点发现,在实现网关代理功能的同时,还可以轻松查看节点或其他节点数据,就像您正在访问本地资源一样。简单点就像上面那样。
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";
});
});
查看配置
appsettings.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"
},
}
在控制器中定义消费接口
WeatherForecastController.cs
[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");
}
}
运行测试
添加消息
消费
查看 Dashboard (http://localhost:5000/cap)
查看订阅列表
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
2
文章
657
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 :
好是好,这个对效率影响大不大哇,效率高不高
一个bug让程序员走上法庭 索赔金额达400亿日元
剑轩 : 有点可怕
ASP.NET Core 服务注册生命周期
剑轩 :
http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术
Session
赞![[good]](https://www.tnblog.net/layui/images/face/54.gif)