No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

README.zh-cn.md 9.4 KiB

hace 7 años
hace 4 años
hace 7 años
hace 5 años
hace 7 años
hace 5 años
hace 6 años
hace 7 años
hace 7 años
hace 3 años
hace 7 años
hace 5 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 3 años
hace 7 años
hace 3 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 6 años
hace 7 años
hace 6 años
hace 7 años
hace 6 años
hace 6 años
hace 3 años
hace 3 años
hace 6 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 6 años
hace 6 años
hace 7 años
hace 7 años
hace 7 años
hace 6 años
hace 6 años
hace 7 años
hace 6 años
hace 6 años
hace 7 años
hace 7 años
hace 7 años
hace 6 años
hace 7 años
hace 3 años
hace 4 años
hace 4 años
hace 6 años
hace 7 años
hace 7 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <p align="center">
  2. <img height="140" src="https://cap.dotnetcore.xyz/img/logo.svg">
  3. </p>
  4. # CAP                     [English](https://github.com/dotnetcore/CAP/blob/master/README.md)
  5. [![Travis branch](https://img.shields.io/travis/dotnetcore/CAP/develop.svg?label=travis-ci)](https://travis-ci.org/dotnetcore/CAP)
  6. [![AppVeyor](https://ci.appveyor.com/api/projects/status/v8gfh6pe2u2laqoa?svg=true)](https://ci.appveyor.com/project/yang-xiaodong/cap)
  7. [![NuGet](https://img.shields.io/nuget/v/DotNetCore.CAP.svg)](https://www.nuget.org/packages/DotNetCore.CAP/)
  8. [![NuGet Preview](https://img.shields.io/nuget/vpre/DotNetCore.CAP.svg?label=nuget-pre)](https://www.nuget.org/packages/DotNetCore.CAP/)
  9. [![Member project of .NET Core Community](https://img.shields.io/badge/member%20project%20of-NCC-9e20c9.svg)](https://github.com/dotnetcore)
  10. [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/dotnetcore/CAP/master/LICENSE.txt)
  11. CAP 是一个基于 .NET Standard 的 C# 库,它是一种处理分布式事务的解决方案,同样具有 EventBus 的功能,它具有轻量级、易使用、高性能等特点。
  12. 你可以在这里 [CAP docs](http://cap.dotnetcore.xyz) 看到更多详细资料。
  13. 你可以在这里看到 [CAP 视频教程](https://www.cnblogs.com/savorboard/p/cap-video-1.html),学习如何在项目中集成CAP。
  14. 在我们构建 SOA 或者 微服务系统的过程中,我们通常需要使用事件来对各个服务进行集成,在这过程中简单的使用消息队列并不能保证数据的最终一致性,
  15. CAP 采用的是和当前数据库集成的本地消息表的方案来解决在分布式系统互相调用的各个环节可能出现的异常,它能够保证任何情况下事件消息都是不会丢失的。
  16. 你同样可以把 CAP 当做 EventBus 来使用,CAP提供了一种更加简单的方式来实现事件消息的发布和订阅,在订阅以及发布的过程中,你不需要继承或实现任何接口。
  17. 这是 CAP 集在ASP.NET Core 微服务架构中的一个示意图:
  18. ## 架构预览
  19. ![architecture.png](https://cap.dotnetcore.xyz/img/architecture-new.png)
  20. > CAP 实现了 [eShop 电子书](https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/subscribe-events#designing-atomicity-and-resiliency-when-publishing-to-the-event-bus) 中描述的发件箱模式
  21. ## Getting Started
  22. ### NuGet
  23. 你可以运行以下下命令在你的项目中安装 CAP。
  24. ```
  25. PM> Install-Package DotNetCore.CAP
  26. ```
  27. CAP 支持 Kafka、RabbitMQ、AzureServiceBus、AmazonSQS 等消息队列,你可以按需选择下面的包进行安装:
  28. ```
  29. PM> Install-Package DotNetCore.CAP.Kafka
  30. PM> Install-Package DotNetCore.CAP.RabbitMQ
  31. PM> Install-Package DotNetCore.CAP.AzureServiceBus
  32. PM> Install-Package DotNetCore.CAP.AmazonSQS
  33. ```
  34. CAP 提供了 Sql Server, MySql, PostgreSQL,MongoDB 的扩展作为数据库存储:
  35. ```
  36. // 按需选择安装你正在使用的数据库
  37. PM> Install-Package DotNetCore.CAP.SqlServer
  38. PM> Install-Package DotNetCore.CAP.MySql
  39. PM> Install-Package DotNetCore.CAP.PostgreSql
  40. PM> Install-Package DotNetCore.CAP.MongoDB
  41. ```
  42. ### Configuration
  43. 首先配置CAP到 Startup.cs 文件中,如下:
  44. ```c#
  45. public void ConfigureServices(IServiceCollection services)
  46. {
  47. ......
  48. services.AddDbContext<AppDbContext>();
  49. services.AddCap(x =>
  50. {
  51. //如果你使用的 EF 进行数据操作,你需要添加如下配置:
  52. x.UseEntityFramework<AppDbContext>(); //可选项,你不需要再次配置 x.UseSqlServer 了
  53. //如果你使用的ADO.NET,根据数据库选择进行配置:
  54. x.UseSqlServer("数据库连接字符串");
  55. x.UseMySql("数据库连接字符串");
  56. x.UsePostgreSql("数据库连接字符串");
  57. //如果你使用的 MongoDB,你可以添加如下配置:
  58. x.UseMongoDB("ConnectionStrings"); //注意,仅支持MongoDB 4.0+集群
  59. //CAP支持 RabbitMQ、Kafka、AzureServiceBus、AmazonSQS 等作为MQ,根据使用选择配置:
  60. x.UseRabbitMQ("ConnectionStrings");
  61. x.UseKafka("ConnectionStrings");
  62. x.UseAzureServiceBus("ConnectionStrings");
  63. x.UseAmazonSQS();
  64. });
  65. }
  66. ```
  67. ### 发布
  68. 在 Controller 中注入 `ICapPublisher` 然后使用 `ICapPublisher` 进行消息发送
  69. ```c#
  70. public class PublishController : Controller
  71. {
  72. private readonly ICapPublisher _capBus;
  73. public PublishController(ICapPublisher capPublisher)
  74. {
  75. _capBus = capPublisher;
  76. }
  77. //不使用事务
  78. [Route("~/without/transaction")]
  79. public IActionResult WithoutTransaction()
  80. {
  81. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  82. return Ok();
  83. }
  84. //Ado.Net 中使用事务,自动提交
  85. [Route("~/adonet/transaction")]
  86. public IActionResult AdonetWithTransaction()
  87. {
  88. using (var connection = new MySqlConnection(ConnectionString))
  89. {
  90. using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true))
  91. {
  92. //业务代码
  93. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  94. }
  95. }
  96. return Ok();
  97. }
  98. //EntityFramework 中使用事务,自动提交
  99. [Route("~/ef/transaction")]
  100. public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
  101. {
  102. using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
  103. {
  104. //业务代码
  105. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  106. }
  107. return Ok();
  108. }
  109. }
  110. ```
  111. ### 订阅
  112. **Action Method**
  113. 在 Action 上添加 CapSubscribeAttribute 来订阅相关消息。
  114. ```c#
  115. public class PublishController : Controller
  116. {
  117. [CapSubscribe("xxx.services.show.time")]
  118. public void CheckReceivedMessage(DateTime datetime)
  119. {
  120. Console.WriteLine(datetime);
  121. }
  122. }
  123. ```
  124. **Service Method**
  125. 如果你的订阅方法没有位于 Controller 中,则你订阅的类需要继承 `ICapSubscribe`:
  126. ```c#
  127. namespace xxx.Service
  128. {
  129. public interface ISubscriberService
  130. {
  131. void CheckReceivedMessage(DateTime datetime);
  132. }
  133. public class SubscriberService: ISubscriberService, ICapSubscribe
  134. {
  135. [CapSubscribe("xxx.services.show.time")]
  136. public void CheckReceivedMessage(DateTime datetime)
  137. {
  138. }
  139. }
  140. }
  141. ```
  142. 然后在 Startup.cs 中的 `ConfigureServices()` 中注入你的 `ISubscriberService` 类
  143. ```c#
  144. public void ConfigureServices(IServiceCollection services)
  145. {
  146. //注意: 注入的服务需要在 `services.AddCap()` 之前
  147. services.AddTransient<ISubscriberService,SubscriberService>();
  148. services.AddCap(x=>{});
  149. }
  150. ```
  151. #### 订阅者组
  152. 订阅者组的概念类似于 Kafka 中的消费者组,它和消息队列中的广播模式相同,用来处理不同微服务实例之间同时消费相同的消息。
  153. 当CAP启动的时候,她将创建一个默认的消费者组,如果多个相同消费者组的消费者消费同一个Topic消息的时候,只会有一个消费者被执行。
  154. 相反,如果消费者都位于不同的消费者组,则所有的消费者都会被执行。
  155. 相同的实例中,你可以通过下面的方式来指定他们位于不同的消费者组。
  156. ```C#
  157. [CapSubscribe("xxx.services.show.time", Group = "group1" )]
  158. public void ShowTime1(DateTime datetime)
  159. {
  160. }
  161. [CapSubscribe("xxx.services.show.time", Group = "group2")]
  162. public void ShowTime2(DateTime datetime)
  163. {
  164. }
  165. ```
  166. `ShowTime1` 和 `ShowTime2` 处于不同的组,他们将会被同时调用。
  167. PS,你可以通过下面的方式来指定默认的消费者组名称:
  168. ```C#
  169. services.AddCap(x =>
  170. {
  171. x.DefaultGroup = "default-group-name";
  172. });
  173. ```
  174. ### Dashboard
  175. CAP 同时提供了仪表盘(Dashboard)功能,你可以很方便的查看发出和接收到的消息。 除此之外,你还可以在仪表盘中实时查看发送或者接收到的消息。
  176. 使用一下命令安装 Dashboard:
  177. ```
  178. PM> Install-Package DotNetCore.CAP.Dashboard
  179. ```
  180. 在分布式环境中,仪表盘内置集成了 [Consul](http://consul.io) 作为节点的注册发现,同时实现了网关代理功能,你同样可以方便的查看本节点或者其他节点的数据,它就像你访问本地资源一样。
  181. ```c#
  182. services.AddCap(x =>
  183. {
  184. //...
  185. // 注册 Dashboard
  186. x.UseDashboard();
  187. // 注册节点到 Consul
  188. x.UseDiscovery(d =>
  189. {
  190. d.DiscoveryServerHostName = "localhost";
  191. d.DiscoveryServerPort = 8500;
  192. d.CurrentNodeHostName = "localhost";
  193. d.CurrentNodePort = 5800;
  194. d.NodeId = 1;
  195. d.NodeName = "CAP No.1 Node";
  196. });
  197. });
  198. ```
  199. 仪表盘默认的访问地址是:[http://localhost:xxx/cap](http://localhost:xxx/cap),你可以在`d.MatchPath`配置项中修改`cap`路径后缀为其他的名字。
  200. ![dashboard](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220827302-189215107.png)
  201. ![received](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220934115-1107747665.png)
  202. ![subscibers](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220949193-884674167.png)
  203. ![nodes](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004221001880-1162918362.png)
  204. ## 贡献
  205. 贡献的最简单的方法之一就是是参与讨论和讨论问题(issue)。你也可以通过提交的 Pull Request 代码变更作出贡献。
  206. ### License
  207. MIT