.Net 简单使用 CAP + RabbitMQ 完成事件的发布与订阅

简介

CAP 是一个EventBus,同时也是一个在微服务或者SOA系统中解决分布式事务问题的一个框架【官网

使用
  • 第一步:创建发布者(Angel.Cap.Publisher)与订阅者(Angel.Cap.Subscriber)两个应用程序,在Nuget上引用主要依赖:DotNetCore.CAP、DotNetCore.CAP.RabbitMQ;用的是Sql Server,还要引用后面三个引用
    在这里插入图片描述
  • 第二步:搭建发布者 Angel.Cap.Publisher
  1. 添加数据库连接字符串
 "ConnectionStrings": {
    //连接字符串
    "AngelDBContext": "data source=.;initial catalog=AngelCapDB;User ID=sa;Password=sa;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
  }
  1. 添加DBContext
 public class PubDBContext : DbContext
 {
   

     public PubDBContext()
     {
   

     }
     public PubDBContext(DbContextOptions<PubDBContext> options) : base(options)
     {
   

     }
     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
     {
   
         base.OnConfiguring(optionsBuilder);

     }
 }
  1. Configure和ConfigureServices使用
public void ConfigureServices(IServiceCollection services)
{
   
    services.AddControllersWithViews();

    //注入数据库
    services.AddDbContext<PubDBContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:AngelDBContext"]));

    //注册cap事件
    services.AddCap(x =>
    {
   
        x.UseEntityFramework<PubDBContext>();
        x.UseRabbitMQ(rb =>
        {
   
            rb.HostName = "localhost";
            rb.UserName = "admin";
            rb.Password = "admin";
            rb.Port = 5672;
            rb.VirtualHost = 
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值