CAP

  • CAP docs看到更多详细资料。
  • CAP 视频教程,学习如何在项目中集成CAP。
  • GitHub源码:https://github.com/dotnetcore/cap
  • 示例代码:https://github.com/dotnetcore/CAP/tree/master/samples

    最低配置

    CAP至少要你配置一个消息队列和一个事件存储,如果你想快速开始你可以使用下面的配置:

    1. var builder = WebApplication.CreateBuilder(args);
    2. builder.Services.AddCap(x =>
    3. {
    4. x.UseInMemoryMessageQueue(); //配置一个消息队列
    5. x.UseInMemoryStorage(); //配置一个事件存储
    6. });

    自定义配置

    在 AddCap 中 CapOptions 对象是用来存储配置相关信息,默认情况下它们都具有一些默认值,有些时候你可能需要自定义。

    1. /// <summary>
    2. /// 可用于配置系统的所有选项
    3. /// </summary>
    4. public class CapOptions
    5. {
    6. public CapOptions()
    7. {
    8. //成功消息的过期时间(秒),24*3600 秒(1天后)
    9. SucceedMessageExpiredAfter = 24 * 3600;
    10. //失败消息的过期时间(秒),15*24*3600 秒(15天后)
    11. FailedMessageExpiredAfter = 15 * 24 * 3600;
    12. //配置每次重试的间隔时间
    13. FailedRetryInterval = 60;
    14. //重试的最大次数
    15. FailedRetryCount = 50;
    16. //消费者线程并行处理消息的线程数
    17. ConsumerThreadCount = 1;
    18. ProducerThreadCount = 1;
    19. Extensions = new List<ICapOptionsExtension>();
    20. //用于给消息指定版本来隔离不同版本服务的消息,常用于A/B测试或者多服务版本的场景
    21. Version = "v1";
    22. //默认的消费者组的名字
    23. DefaultGroupName = "cap.queue." + Assembly.GetEntryAssembly()?.GetName().Name.ToLower();
    24. //收集器删除已经过期消息的时间间隔
    25. CollectorCleaningInterval = 300;
    26. UseDispatchingPerGroup = false;
    27. }
    28. }