Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

5 роки тому
5 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
5 роки тому
7 роки тому
7 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
7 роки тому
6 роки тому
6 роки тому
7 роки тому
7 роки тому
6 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
7 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
7 роки тому
6 роки тому
8 роки тому
6 роки тому
7 роки тому
6 роки тому
6 роки тому
6 роки тому
8 роки тому
8 роки тому
8 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # CAP                       [中文](https://github.com/dotnetcore/CAP/blob/master/README.zh-cn.md)
  2. [![Travis branch](https://img.shields.io/travis/dotnetcore/CAP/master.svg?label=travis-ci)](https://travis-ci.org/dotnetcore/CAP)
  3. [![AppVeyor](https://ci.appveyor.com/api/projects/status/v8gfh6pe2u2laqoa/branch/master?svg=true)](https://ci.appveyor.com/project/yuleyule66/cap/branch/master)
  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 Core Community](https://img.shields.io/badge/member%20project%20of-NCC-9e20c9.svg)](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 is a library based on .Net standard, which is a solution to deal with distributed transactions, also has the function of EventBus, it is lightweight, easy to use, and efficiently.
  9. ## Overview
  10. In the process of building an SOA or MicroService system, we usually need to use the event to integrate each services. In the process, the simple use of message queue does not guarantee the reliability. CAP is adopted the local message table program integrated with the current database to solve the exception may occur in the process of the distributed system calling each other. It can ensure that the event messages are not lost in any case.
  11. You can also use the CAP as an EventBus. The CAP provides a simpler way to implement event publishing and subscriptions. You do not need to inherit or implement any interface during the process of subscription and sending.
  12. This is a diagram of the CAP working in the ASP.NET Core MicroService architecture:
  13. ![cap.png](http://docs.dotnet-china.org/CAP/img/architecture.png)
  14. > CAP implements the Outbox Pattern described in the [eShop ebook](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).
  15. ## Getting Started
  16. ### NuGet
  17. You can run the following command to install the CAP in your project.
  18. ```
  19. PM> Install-Package DotNetCore.CAP
  20. ```
  21. CAP supports RabbitMQ,Kafka and AzureService as message queue, select the packages you need to install:
  22. ```
  23. PM> Install-Package DotNetCore.CAP.Kafka
  24. PM> Install-Package DotNetCore.CAP.RabbitMQ
  25. PM> Install-Package DotNetCore.CAP.AzureServiceBus
  26. ```
  27. CAP supports SqlServer, MySql, PostgreSql,MongoDB as event log storage.
  28. ```
  29. // select a database provider you are using, event log table will integrate into.
  30. PM> Install-Package DotNetCore.CAP.SqlServer
  31. PM> Install-Package DotNetCore.CAP.MySql
  32. PM> Install-Package DotNetCore.CAP.PostgreSql
  33. PM> Install-Package DotNetCore.CAP.MongoDB //need MongoDB 4.0+ cluster
  34. ```
  35. ### Configuration
  36. First,You need to config CAP in your Startup.cs:
  37. ```cs
  38. public void ConfigureServices(IServiceCollection services)
  39. {
  40. //......
  41. services.AddDbContext<AppDbContext>(); //Options, If you are using EF as the ORM
  42. services.AddSingleton<IMongoClient>(new MongoClient("")); //Options, If you are using MongoDB
  43. services.AddCap(x =>
  44. {
  45. // If you are using EF, you need to add the configuration:
  46. x.UseEntityFramework<AppDbContext>(); //Options, Notice: You don't need to config x.UseSqlServer(""") again! CAP can autodiscovery.
  47. // If you are using ADO.NET, choose to add configuration you needed:
  48. x.UseSqlServer("Your ConnectionStrings");
  49. x.UseMySql("Your ConnectionStrings");
  50. x.UsePostgreSql("Your ConnectionStrings");
  51. // If you are using MongoDB, you need to add the configuration:
  52. x.UseMongoDB("Your ConnectionStrings"); //MongoDB 4.0+ cluster
  53. // CAP support RabbitMQ,Kafka,AzureService as the MQ, choose to add configuration you needed:
  54. x.UseRabbitMQ("ConnectionString");
  55. x.UseKafka("ConnectionString");
  56. x.UseAzureServiceBus("ConnectionString");
  57. });
  58. }
  59. ```
  60. ### Publish
  61. Inject `ICapPublisher` in your Controller, then use the `ICapPublisher` to send message
  62. ```c#
  63. public class PublishController : Controller
  64. {
  65. private readonly ICapPublisher _capBus;
  66. public PublishController(ICapPublisher capPublisher)
  67. {
  68. _capBus = capPublisher;
  69. }
  70. [Route("~/adonet/transaction")]
  71. public IActionResult AdonetWithTransaction()
  72. {
  73. using (var connection = new MySqlConnection(ConnectionString))
  74. {
  75. using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true))
  76. {
  77. //your business logic code
  78. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  79. }
  80. }
  81. return Ok();
  82. }
  83. [Route("~/ef/transaction")]
  84. public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
  85. {
  86. using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
  87. {
  88. //your business logic code
  89. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  90. }
  91. return Ok();
  92. }
  93. }
  94. ```
  95. ### Subscribe
  96. **In Controller Action**
  97. Add the Attribute `[CapSubscribe()]` on Action to subscribe message:
  98. ```c#
  99. public class PublishController : Controller
  100. {
  101. [CapSubscribe("xxx.services.show.time")]
  102. public void CheckReceivedMessage(DateTime datetime)
  103. {
  104. Console.WriteLine(datetime);
  105. }
  106. }
  107. ```
  108. **In Business Logic Service**
  109. If your subscribe method is not in the Controller,then your subscribe class need to Inheritance `ICapSubscribe`:
  110. ```c#
  111. namespace BusinessCode.Service
  112. {
  113. public interface ISubscriberService
  114. {
  115. public void CheckReceivedMessage(DateTime datetime);
  116. }
  117. public class SubscriberService: ISubscriberService, ICapSubscribe
  118. {
  119. [CapSubscribe("xxx.services.show.time")]
  120. public void CheckReceivedMessage(DateTime datetime)
  121. {
  122. }
  123. }
  124. }
  125. ```
  126. Then inject your `ISubscriberService` class in Startup.cs
  127. ```c#
  128. public void ConfigureServices(IServiceCollection services)
  129. {
  130. //Note: The injection of services needs before of `services.AddCap()`
  131. services.AddTransient<ISubscriberService,SubscriberService>();
  132. services.AddCap(x=>
  133. {
  134. //...
  135. });
  136. }
  137. ```
  138. #### Subscribe Group
  139. The concept of a subscription group is similar to that of a consumer group in Kafka. it is the same as the broadcast mode in the message queue, which is used to process the same message between multiple different microservice instances.
  140. When CAP startup, it will use the current assembly name as the default group name, if multiple same group subscribers subscribe the same topic name, there is only one subscriber can receive the message.
  141. Conversely, if subscribers are in different groups, they will all receive messages.
  142. In the same application, you can specify the `Group` property to keep they are in different subscribe groups:
  143. ```C#
  144. [CapSubscribe("xxx.services.show.time", Group = "group1" )]
  145. public void ShowTime1(DateTime datetime)
  146. {
  147. }
  148. [CapSubscribe("xxx.services.show.time", Group = "group2")]
  149. public void ShowTime2(DateTime datetime)
  150. {
  151. }
  152. ```
  153. `ShowTime1` and `ShowTime2` will be called at the same time.
  154. BTW, You can specify the default group name in the configuration :
  155. ```C#
  156. services.AddCap(x =>
  157. {
  158. x.DefaultGroup = "default-group-name";
  159. });
  160. ```
  161. ### Dashboard
  162. CAP v2.1+ provides the dashboard pages, you can easily view the sent and received messages. In addition, you can also view the message status in real time on the dashboard.
  163. In the distributed environment, the dashboard built-in integrated [Consul](http://consul.io) as a node discovery, while the realization of the gateway agent function, you can also easily view the node or other node data, It's like you are visiting local resources.
  164. ```c#
  165. services.AddCap(x =>
  166. {
  167. //...
  168. // Register Dashboard
  169. x.UseDashboard();
  170. // Register to Consul
  171. x.UseDiscovery(d =>
  172. {
  173. d.DiscoveryServerHostName = "localhost";
  174. d.DiscoveryServerPort = 8500;
  175. d.CurrentNodeHostName = "localhost";
  176. d.CurrentNodePort = 5800;
  177. d.NodeId = 1;
  178. d.NodeName = "CAP No.1 Node";
  179. });
  180. });
  181. ```
  182. The default dashboard address is :[http://localhost:xxx/cap](http://localhost:xxx/cap) , you can also change the `cap` suffix to others with `d.MatchPath` configuration options.
  183. ![dashboard](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220827302-189215107.png)
  184. ![received](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220934115-1107747665.png)
  185. ![subscibers](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220949193-884674167.png)
  186. ![nodes](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004221001880-1162918362.png)
  187. ## Contribute
  188. One of the easiest ways to contribute is to participate in discussions and discuss issues. You can also contribute by submitting pull requests with code changes.
  189. ### License
  190. [MIT](https://github.com/dotnetcore/CAP/blob/master/LICENSE.txt)