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.

5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
7 年之前
7 年之前
7 年之前
7 年之前
5 年之前
5 年之前
6 年之前
7 年之前
7 年之前
6 年之前
7 年之前
7 年之前
7 年之前
6 年之前
7 年之前
7 年之前
6 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
6 年之前
7 年之前
7 年之前
7 年之前
7 年之前
6 年之前
7 年之前
7 年之前
6 年之前
8 年之前
7 年之前
6 年之前
6 年之前
8 年之前
8 年之前
8 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <p align="center">
  2. <img height="140" src="https://cap.dotnetcore.xyz/img/logo.svg">
  3. </p>
  4. # CAP                       [中文](https://github.com/dotnetcore/CAP/blob/master/README.zh-cn.md)
  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/v8gfh6pe2u2laqoa/branch/master?svg=true)](https://ci.appveyor.com/project/yuleyule66/cap/branch/master)
  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 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.
  12. 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.
  13. 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.
  14. ## Architecture overview
  15. ![cap.png](https://cap.dotnetcore.xyz/img/architecture-new.png)
  16. > 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).
  17. ## Getting Started
  18. ### NuGet
  19. You can run the following command to install the CAP in your project.
  20. ```
  21. PM> Install-Package DotNetCore.CAP
  22. ```
  23. CAP supports RabbitMQ,Kafka and AzureService as message queue, select the packages you need to install:
  24. ```
  25. PM> Install-Package DotNetCore.CAP.Kafka
  26. PM> Install-Package DotNetCore.CAP.RabbitMQ
  27. PM> Install-Package DotNetCore.CAP.AzureServiceBus
  28. ```
  29. CAP supports SqlServer, MySql, PostgreSql,MongoDB as event log storage.
  30. ```
  31. // select a database provider you are using, event log table will integrate into.
  32. PM> Install-Package DotNetCore.CAP.SqlServer
  33. PM> Install-Package DotNetCore.CAP.MySql
  34. PM> Install-Package DotNetCore.CAP.PostgreSql
  35. PM> Install-Package DotNetCore.CAP.MongoDB //need MongoDB 4.0+ cluster
  36. ```
  37. ### Configuration
  38. First,You need to config CAP in your Startup.cs:
  39. ```cs
  40. public void ConfigureServices(IServiceCollection services)
  41. {
  42. //......
  43. services.AddDbContext<AppDbContext>(); //Options, If you are using EF as the ORM
  44. services.AddSingleton<IMongoClient>(new MongoClient("")); //Options, If you are using MongoDB
  45. services.AddCap(x =>
  46. {
  47. // If you are using EF, you need to add the configuration:
  48. x.UseEntityFramework<AppDbContext>(); //Options, Notice: You don't need to config x.UseSqlServer(""") again! CAP can autodiscovery.
  49. // If you are using ADO.NET, choose to add configuration you needed:
  50. x.UseSqlServer("Your ConnectionStrings");
  51. x.UseMySql("Your ConnectionStrings");
  52. x.UsePostgreSql("Your ConnectionStrings");
  53. // If you are using MongoDB, you need to add the configuration:
  54. x.UseMongoDB("Your ConnectionStrings"); //MongoDB 4.0+ cluster
  55. // CAP support RabbitMQ,Kafka,AzureService as the MQ, choose to add configuration you needed:
  56. x.UseRabbitMQ("ConnectionString");
  57. x.UseKafka("ConnectionString");
  58. x.UseAzureServiceBus("ConnectionString");
  59. });
  60. }
  61. ```
  62. ### Publish
  63. Inject `ICapPublisher` in your Controller, then use the `ICapPublisher` to send message
  64. ```c#
  65. public class PublishController : Controller
  66. {
  67. private readonly ICapPublisher _capBus;
  68. public PublishController(ICapPublisher capPublisher)
  69. {
  70. _capBus = capPublisher;
  71. }
  72. [Route("~/adonet/transaction")]
  73. public IActionResult AdonetWithTransaction()
  74. {
  75. using (var connection = new MySqlConnection(ConnectionString))
  76. {
  77. using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true))
  78. {
  79. //your business logic code
  80. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  81. }
  82. }
  83. return Ok();
  84. }
  85. [Route("~/ef/transaction")]
  86. public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
  87. {
  88. using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
  89. {
  90. //your business logic code
  91. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  92. }
  93. return Ok();
  94. }
  95. }
  96. ```
  97. ### Subscribe
  98. **In Controller Action**
  99. Add the Attribute `[CapSubscribe()]` on Action to subscribe message:
  100. ```c#
  101. public class PublishController : Controller
  102. {
  103. [CapSubscribe("xxx.services.show.time")]
  104. public void CheckReceivedMessage(DateTime datetime)
  105. {
  106. Console.WriteLine(datetime);
  107. }
  108. }
  109. ```
  110. **In Business Logic Service**
  111. If your subscribe method is not in the Controller,then your subscribe class need to Inheritance `ICapSubscribe`:
  112. ```c#
  113. namespace BusinessCode.Service
  114. {
  115. public interface ISubscriberService
  116. {
  117. public void CheckReceivedMessage(DateTime datetime);
  118. }
  119. public class SubscriberService: ISubscriberService, ICapSubscribe
  120. {
  121. [CapSubscribe("xxx.services.show.time")]
  122. public void CheckReceivedMessage(DateTime datetime)
  123. {
  124. }
  125. }
  126. }
  127. ```
  128. Then inject your `ISubscriberService` class in Startup.cs
  129. ```c#
  130. public void ConfigureServices(IServiceCollection services)
  131. {
  132. //Note: The injection of services needs before of `services.AddCap()`
  133. services.AddTransient<ISubscriberService,SubscriberService>();
  134. services.AddCap(x=>
  135. {
  136. //...
  137. });
  138. }
  139. ```
  140. #### Subscribe Group
  141. 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.
  142. 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.
  143. Conversely, if subscribers are in different groups, they will all receive messages.
  144. In the same application, you can specify the `Group` property to keep they are in different subscribe groups:
  145. ```C#
  146. [CapSubscribe("xxx.services.show.time", Group = "group1" )]
  147. public void ShowTime1(DateTime datetime)
  148. {
  149. }
  150. [CapSubscribe("xxx.services.show.time", Group = "group2")]
  151. public void ShowTime2(DateTime datetime)
  152. {
  153. }
  154. ```
  155. `ShowTime1` and `ShowTime2` will be called at the same time.
  156. BTW, You can specify the default group name in the configuration :
  157. ```C#
  158. services.AddCap(x =>
  159. {
  160. x.DefaultGroup = "default-group-name";
  161. });
  162. ```
  163. ### Dashboard
  164. 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.
  165. 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.
  166. ```c#
  167. services.AddCap(x =>
  168. {
  169. //...
  170. // Register Dashboard
  171. x.UseDashboard();
  172. // Register to Consul
  173. x.UseDiscovery(d =>
  174. {
  175. d.DiscoveryServerHostName = "localhost";
  176. d.DiscoveryServerPort = 8500;
  177. d.CurrentNodeHostName = "localhost";
  178. d.CurrentNodePort = 5800;
  179. d.NodeId = 1;
  180. d.NodeName = "CAP No.1 Node";
  181. });
  182. });
  183. ```
  184. 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.
  185. ![dashboard](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220827302-189215107.png)
  186. ![received](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220934115-1107747665.png)
  187. ![subscibers](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220949193-884674167.png)
  188. ![nodes](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004221001880-1162918362.png)
  189. ## Contribute
  190. 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.
  191. ### License
  192. [MIT](https://github.com/dotnetcore/CAP/blob/master/LICENSE.txt)