一:相关依赖包
DotNetCore.CAP
DotNetCore.CAP.Dashboard
DotNetCore.CAP.InMemoryStorage
DotNetCore.CAP.Kafka
DotNetCore.CAP.MySql
DotNetCore.CAP.RabbitMQ
配置
// 8、添加事件总线cap
services.AddCap(x =>
{
// 8.1 使用内存存储消息(消息发送失败处理)
//x.UseInMemoryStorage();
// 8.2 使用EntityFramework进行存储操作
x.UseEntityFramework<SeckillAggregateServicesContext>();
// 8.3 使用sqlserver进行事务处理
x.UseMySql(Configuration.GetConnectionString("DefaultConnection"));
// 8.4 使用RabbitMQ进行事件中心处理
x.UseRabbitMQ(rb =>
{
// rb.HostName = "localhost"; // 本地主机
//rb.HostName = "172.18.0.3";// 远程主机
rb.HostName = "10.96.0.3";// K8s集群service
rb.UserName = "guest";
rb.Password = "guest";
rb.Port = 5672;
rb.VirtualHost = "/";
});
// 7.4 配置定时器尽早启动
// x.FailedRetryInterval = 2;
x.FailedRetryCount = 5; // 3 次失败 3分钟
// 8.5添加cap后台监控页面(人工处理)
x.UseDashboard();
});
生产者
/// <summary>
/// 3、创建订单(缓存库存扣减 + 消息队列)
/// </summary>
/// <param name="orderDto"></param>
[HttpPost]
public PaymentDto CreateOrder(SysUser sysUser, [FromForm] OrderPo orderPo)
{
// 1、创建订单号
string orderSn = OrderUtil.GetOrderCode();
// 2、扣减库存(缓存)
seckillStockCache.SubtractSeckillStock(orderPo.ProductId, orderPo.ProductCount);
// 3、发送订单消息到rabbitmq
SendOrderCreateMessage(sysUser.UserId, orderSn, orderPo);
// 6、创建支付信息
PaymentDto paymentDto = new PaymentDto();
paymentDto.OrderSn = orderSn;
paymentDto.OrderTotalPrice = orderPo.OrderTotalPrice;
paymentDto.UserId = sysUser.UserId;
paymentDto.ProductId = orderPo.ProductId;
paymentDto.ProductName = orderPo.ProductName;
return paymentDto;
}
/// <summary>
/// 3.1 发送创建订单消息
/// </summary>
/// <param name="ProductId"></param>
/// <param name="ProductCount"></param>
private void SendOrderCreateMessage(int userId,string orderSn, OrderPo orderPo)
{
var configuration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<OrderPo, Order>();
});
IMapper mapper = configuration.CreateMapper();
// 2、设置订单
Order order = mapper.Map<OrderPo, Order>(orderPo);
order.OrderSn = orderSn;
order.OrderType = "1";// 订单类型(1、为秒杀订单)
order.UserId = userId;
// 3、设置订单项
OrderItem orderItem = new OrderItem();
orderItem.ItemCount = orderPo.ProductCount;
orderItem.ItemPrice = orderPo.OrderTotalPrice;
orderItem.ItemTotalPrice = orderPo.OrderTotalPrice;
orderItem.ProductUrl = orderPo.ProductUrl;
orderItem.ProductId = orderPo.ProductId;
orderItem.OrderSn = orderSn;
List<OrderItem> orderItems = new List<OrderItem>();
orderItems.Add(orderItem);
order.OrderItems = orderItems;
// 4、发送订单消息
capPublisher.Publish<Order>("seckill.order", order);
}
消费者
/// <summary>
/// 创建订单
/// </summary>
/// <param name="Order"></param>
/// <returns></returns>
[NonAction]
[CapSubscribe("seckill.order")]
public ActionResult<Order> CapPostOrder(Order Order)
{
// 1、创建订单
Order.Createtime = new DateTime();
OrderService.Create(Order);
return CreatedAtAction("GetOrder", new { id = Order.Id }, Order);
}
二:优化效果
单台服务器:5000并发处理总时间12秒,0异常。
单台服务器:10000并发处理总时间18秒,0异常。
注释,这性能在数据库里,性能并不会高多少,需要把mq任务存储在缓存里,但会造成丢失,后续篇幅会说解决方案。