NetCore使用DotNetCore.CAP框架
CAP 是一个EventBus,同时也是一个在微服务或者SOA系统中解决分布式事务问题的一个框架。它有助于创建可扩展,可靠并且易于更改的微服务系统。
这个项目中使用到SqlServer(数据库方面大同小异)、MongoDb、Kafka、RabbitMq以及Consul
1:引入相关Nuget就行
DotNetCore.CAP DotNetCore.CAP.Kafka DotNetCore.CAP.RabbitMQ DotNetCore.CAP.SqlServer DotNetCore.CAP.MongoDB DotNetCore.CAP.Dashboard
2:在program中使用(我的项目是基于net6的)
builder.Services.AddDbContext<AppDbContext>(opt => { opt.UseSqlServer(builder.Configuration.GetSection("CAP:SqlServer").Value); }); //Options, If you are using MongoDB builder.Services.AddSingleton<IMongoClient>(new MongoClient(builder.Configuration.GetSection("CAP:MongoDB").Value)); builder.Services.AddCap(x => { // If you are using EF, you need to add the configuration: //x.UseEntityFramework<AppDbContext>(); //Options, Notice: You don't need to config x.UseSqlServer(""") again! CAP can autodiscovery. // If you are using ADO.NET, choose to add configuration you needed: x.UseSqlServer(builder.Configuration.GetSection("CAP:SqlServer").Value); //x.UseMySql("Your ConnectionStrings"); //x.UsePostgreSql("Your ConnectionStrings"); // If you are using MongoDB, you need to add the configuration: //x.UseMongoDB(opt => { // opt.DatabaseConnection = builder.Configuration.GetSection("CAP:MongoDB").Value; //}); //注意,仅支持MongoDB 4.0+集群 // CAP support RabbitMQ,Kafka,AzureService as the MQ, choose to add configuration you needed: x.UseRabbitMQ(opt => { opt.HostName = "192.168.1.7"; opt.Port = 5672; opt.UserName = "admin"; opt.Password = "admin"; opt.VirtualHost = "/"; }); x.UseKafka(opt => { opt.Servers = "192.168.1.12:9092"; }); //x.UseRabbitMQ(builder.Configuration.GetSection("CAP:RabbitMQ").Value); x.UseDashboard(); DiscoveryOptions discoveryOptions = new DiscoveryOptions(); discoveryOptions.CurrentNodePort = 5173; builder.Configuration.Bind(discoveryOptions); // Register to Consul x.UseDiscovery(d => { d.DiscoveryServerHostName = "localhost"; d.DiscoveryServerPort = 8500; d.CurrentNodeHostName = "localhost"; d.CurrentNodePort = 5222; d.NodeId = "1"; d.NodeName = "fanlin"; d.Scheme = "http"; d.MatchPath = "/api/HealthCheck"; }); x.FailedRetryInterval = 10;//失败重试的间隔时间 x.FailedRetryCount = 10;//失败重试的次数 x.FailedThresholdCallback = info => { Console.WriteLine("Publish Message Error::" + info.Message); }; //x.UseKafka("ConnectionString"); //x.UseAzureServiceBus("ConnectionString"); //x.UseAmazonSQS(); });
3:新建PublishController,主要用于发布,代码很简单。代码如下
/// <summary> /// CAP的消费是自动消费的 /// </summary> [Route("api/[controller]")] [ApiController] public class PublishController : ControllerBase { private static string _publishName = "FanlinCAPDemo.Servces.Test"; private readonly ICapPublisher _capBus; private readonly IConfiguration _configuration; private readonly AppDbContext _appDbContext; public PublishController(ICapPublisher capPublisher, IConfiguration configuration, AppDbContext appDbContext) { _capBus = capPublisher; _configuration = configuration; _appDbContext = appDbContext; } [HttpGet] [Route("no/transaction")]//根目录 public async Task<IActionResult> WithoutTransaction() { Console.WriteLine("普通----无事务"); var user = _appDbContext.Users.Find("1"); await _capBus.PublishAsync(_publishName, user);//应该把数据写到publish表 return Ok(); } [HttpGet] [Route("adonet/transaction")] public IActionResult AdonetWithTransaction() { Console.WriteLine("普通事务---连接字符串----事务"); var user = _appDbContext.Users.Find("1"); using (var connection = new SqlConnection(_configuration.GetSection("CAP:SqlServer").Value)) { using (var transaction = connection.BeginTransaction(_capBus, true)) { //your business logic code _capBus.Publish(_publishName, user); } } return Ok(); } [HttpGet] [Route("ef/transaction")] public IActionResult EntityFrameworkWithTransaction() { Console.WriteLine("上下文---事务"); var user = _appDbContext.Users.Find("1"); //带header IDictionary<string, string?> dicHeader = new Dictionary<string, string?>(); dicHeader.Add("Husband", "Fanlin"); dicHeader.Add("Wife", "Baoting"); dicHeader.Add("SumAge", "34"); using (var trans = _appDbContext.Database.BeginTransaction(_capBus, autoCommit: true)) { //your business logic code _capBus.Publish(_publishName, user, dicHeader); } return Ok(); } }
4:新建ConsumerController 用于消费,代码很简单,需要注意,订阅需要标注特性CapSubscribe,其属性name就是发布者的_publishName,也可以顶一个多个group,在rabbitmq里面会自动新建相应的队列!代码如下
[Route("api/[controller]")] [ApiController] public class ConsumerController : ControllerBase { private readonly AppDbContext _context; private readonly IMongoClient _client; private readonly ICapPublisher _capBus; public ConsumerController(AppDbContext appDbContext, IMongoClient mongoClient, ICapPublisher iCapPublisher) { _context = appDbContext; _capBus = iCapPublisher; _client = mongoClient; var collection = _client.GetDatabase("MyCap").GetCollection<Users>("MyCap.User"); collection.InsertOne(new Users() { UserID = "3", UserName = "范小包", UserPy = "fxb", UserPwd = "1234", department = "1", userRemark = "jkl", UserWb = "abc", headImageFile = "none" }); } [NonAction] [CapSubscribe("FanlinCAPDemo.Servces.Test")] public void CheckReceivedMessage(Users users, [FromCap] CapHeader header) { Console.WriteLine($"{DateTime.Now} NoGroup Info:{users},Header:{header.Count}"); } [NonAction] [CapSubscribe("FanlinCAPDemo.Servces.Test", Group = "Group1")] public void CheckReceivedMessageGroup(Users users) { Console.WriteLine($"{DateTime.Now} NoGroup Info:{users}"); //这里不支持事务 使用事务会报错 //using (var session = _client.StartTransaction(_capBus, autoCommit: false)) //{ var collection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection"); collection.InsertOne( new BsonDocument { { "hello", "world" } }); _capBus.Publish("sample.rabbitmq.mongodb", DateTime.Now); //session.CommitTransaction(); //} } }
5:调用接口,查看kafka和rabbitmq以及相关数据库的变化
a:RabbitMQ:
这里自动新建了两个队列,如果有定义group已group属性为准,如果没有,名称是自定义的
b:Kafka
CAP自动创建了3个topic
c:SqlServer
数据库会自动生成名为cap的数据库,里面有两张表,其中publish是发布的数据,receive是接收到的数据
d:再来看Mongodb,这里是MongoDB的数据库,如果要使用事务,事务里面的数据库必须先存在
e:如果你需要使用consul,只需要启动consul.exe即可,CAP框架会自动将服务注册,访问你的服务http://ip:port/cap,然后就可以通过界面看到相关数据了
以上就是CAP矿建的简单使用,这是大神杨晓东的作品,有关文档可参考
https://github.com/dotnetcore/CAP
https://cap.dotnetcore.xyz/user-guide/zh/getting-started/introduction/