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