简介
CAP 是一个EventBus,同时也是一个在微服务或者SOA系统中解决分布式事务问题的一个框架【官网】
使用
- 第一步:创建发布者(Angel.Cap.Publisher)与订阅者(Angel.Cap.Subscriber)两个应用程序,在Nuget上引用主要依赖:DotNetCore.CAP、DotNetCore.CAP.RabbitMQ;用的是Sql Server,还要引用后面三个引用
- 第二步:搭建发布者 Angel.Cap.Publisher
- 添加数据库连接字符串
"ConnectionStrings": {
//连接字符串
"AngelDBContext": "data source=.;initial catalog=AngelCapDB;User ID=sa;Password=sa;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
}
- 添加DBContext
public class PubDBContext : DbContext
{
public PubDBContext()
{
}
public PubDBContext(DbContextOptions<PubDBContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
}
- 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 =