You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.zh-cn.md 5.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # CAP                       [English](https://github.com/dotnetcore/CAP/blob/develop/README.md)
  2. [![Travis branch](https://img.shields.io/travis/dotnetcore/CAP/develop.svg?label=travis-ci)](https://travis-ci.org/dotnetcore/CAP)
  3. [![AppVeyor](https://ci.appveyor.com/api/projects/status/4mpe0tbu7n126vyw?svg=true)](https://ci.appveyor.com/project/yuleyule66/cap)
  4. [![NuGet](https://img.shields.io/nuget/v/DotNetCore.CAP.svg)](https://www.nuget.org/packages/DotNetCore.CAP/)
  5. [![NuGet Preview](https://img.shields.io/nuget/vpre/DotNetCore.CAP.svg?label=nuget-pre)](https://www.nuget.org/packages/DotNetCore.CAP/)
  6. [![Member project of .NET China Foundation](https://img.shields.io/badge/member_project_of-.NET_CHINA-red.svg?style=flat&colorB=9E20C8)](https://github.com/dotnetcore)
  7. [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/dotnetcore/CAP/master/LICENSE.txt)
  8. CAP 是一个在分布式系统(SOA、MicroService)中实现最终一致性的库,它具有轻量级、易使用、高性能等特点。
  9. 你可以在这里[CAP Wiki](https://github.com/dotnetcore/CAP/wiki)看到更多详细资料。
  10. ## 预览(OverView)
  11. CAP 是在一个 ASP.NET Core 项目中使用的库,当然他可以用于 ASP.NET Core On .NET Framework 中。
  12. 你可以把 CAP 看成是一个 EventBus,因为它具有 EventBus 的所有功能,并且 CAP 提供了更加简化的方式来处理 EventBus 中的发布和订阅。
  13. CAP 具有消息持久化的功能,当你的服务进行重启或者宕机时它可以保证消息的可靠性。CAP提供了基于Microsoft DI 的 Publisher Service 服务,它可以和你的业务服务进行无缝结合,并且支持强一致性的事务。
  14. 这是CAP集在ASP.NET Core 微服务架构中的一个示意图:
  15. ![](http://images2015.cnblogs.com/blog/250417/201707/250417-20170705175827128-1203291469.png)
  16. > 图中实线部分代表用户代码,虚线部分代表CAP内部实现。
  17. ## Getting Started
  18. ### NuGet
  19. 你可以运行以下下命令在你的项目中安装 CAP。
  20. ```
  21. PM> Install-Package DotNetCore.CAP
  22. ```
  23. 如果你的消息队列使用的是 Kafka 的话,你可以:
  24. ```
  25. PM> Install-Package DotNetCore.CAP.Kafka
  26. ```
  27. 如果你的消息队列使用的是 RabbitMQ 的话,你可以:
  28. ```
  29. PM> Install-Package DotNetCore.CAP.RabbitMQ
  30. ```
  31. CAP 默认提供了 Sql Server 的扩展作为数据库存储(MySql的正在开发中):
  32. ```
  33. PM> Install-Package DotNetCore.CAP.SqlServer
  34. ```
  35. ### Configuration
  36. 首先配置CAP到 Startup.cs 文件中,如下:
  37. ```cs
  38. public void ConfigureServices(IServiceCollection services)
  39. {
  40. ......
  41. services.AddDbContext<AppDbContext>();
  42. services.AddCap(x =>
  43. {
  44. // 如果你的 SqlServer 使用的 EF 进行数据操作,你需要添加如下配置:
  45. // 注意: 你不需要再次配置 x.UseSqlServer(""")
  46. x.UseEntityFramework<AppDbContext>();
  47. // 如果你使用的Dapper,你需要添加如下配置:
  48. x.UseSqlServer("数据库连接字符串");
  49. // 如果你使用的 RabbitMQ 作为MQ,你需要添加如下配置:
  50. x.UseRabbitMQ("localhost");
  51. //如果你使用的 Kafka 作为MQ,你需要添加如下配置:
  52. x.UseKafka("localhost");
  53. });
  54. }
  55. public void Configure(IApplicationBuilder app)
  56. {
  57. .....
  58. app.UseCap();
  59. }
  60. ```
  61. ### 发布
  62. 在 Controller 中注入 `ICapPublisher` 然后使用 `ICapPublisher` 进行消息发送
  63. ```cs
  64. public class PublishController : Controller
  65. {
  66. private readonly ICapPublisher _publisher;
  67. public PublishController(ICapPublisher publisher)
  68. {
  69. _publisher = publisher;
  70. }
  71. [Route("~/checkAccount")]
  72. public async Task<IActionResult> PublishMessage()
  73. {
  74. //指定发送的消息头和内容
  75. await _publisher.PublishAsync("xxx.services.account.check", new Person { Name = "Foo", Age = 11 });
  76. return Ok();
  77. }
  78. [Route("~/checkAccountWithTrans")]
  79. public async Task<IActionResult> PublishMessageWithTransaction([FromServices]AppDbContext dbContext)
  80. {
  81. using (var trans = dbContext.Database.BeginTransaction())
  82. {
  83. await _publisher.PublishAsync("xxx.services.account.check", new Person { Name = "Foo", Age = 11 });
  84. trans.Commit();
  85. }
  86. return Ok();
  87. }
  88. }
  89. ```
  90. ### 订阅
  91. **Action Method**
  92. 在 Action 上添加 CapSubscribeAttribute 来订阅相关消息。
  93. ```cs
  94. public class PublishController : Controller
  95. {
  96. private readonly ICapPublisher _publisher;
  97. public PublishController(ICapPublisher publisher)
  98. {
  99. _publisher = publisher;
  100. }
  101. [NoAction]
  102. [CapSubscribe("xxx.services.account.check")]
  103. public async Task CheckReceivedMessage(Person person)
  104. {
  105. Console.WriteLine(person.Name);
  106. Console.WriteLine(person.Age);
  107. return Task.CompletedTask;
  108. }
  109. }
  110. ```
  111. **Service Method**
  112. 如果你的订阅方法没有位于 Controller 中,则你订阅的类需要继承 `ICapSubscribe`:
  113. ```cs
  114. namespace xxx.Service
  115. {
  116. public interface ISubscriberService
  117. {
  118. public void CheckReceivedMessage(Person person);
  119. }
  120. public class SubscriberService: ISubscriberService, ICapSubscribe
  121. {
  122. [CapSubscribe("xxx.services.account.check")]
  123. public void CheckReceivedMessage(Person person)
  124. {
  125. }
  126. }
  127. }
  128. ```
  129. 然后在 Startup.cs 中的 `ConfigureServices()` 中注入你的 `ISubscriberService` 类
  130. ```cs
  131. public void ConfigureServices(IServiceCollection services)
  132. {
  133. services.AddTransient<ISubscriberService,SubscriberService>();
  134. }
  135. ```
  136. ## 贡献
  137. 贡献的最简单的方法之一就是是参与讨论和讨论问题(issue)。你也可以通过提交的 Pull Request 代码变更作出贡献。
  138. ### License
  139. MIT