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.md 8.1 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
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # CAP                       [中文](https://github.com/dotnetcore/CAP/blob/develop/README.zh-cn.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 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. ![](http://images2015.cnblogs.com/blog/250417/201707/250417-20170705175827128-1203291469.png)
  14. > The solid line in the figure represents the user code, and the dotted line represents the internal implementation of the CAP.
  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. If you want use Kafka to send integrating event, installing by:
  22. ```
  23. PM> Install-Package DotNetCore.CAP.Kafka
  24. ```
  25. If you want use RabbitMQ to send integrating event, installing by:
  26. ```
  27. PM> Install-Package DotNetCore.CAP.RabbitMQ
  28. ```
  29. CAP supports SqlServer, MySql, PostgreSql 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. ```
  36. ### Configuration
  37. First,You need to config CAP in your Startup.cs:
  38. ```cs
  39. public void ConfigureServices(IServiceCollection services)
  40. {
  41. //......
  42. services.AddDbContext<AppDbContext>();
  43. services.AddCap(x =>
  44. {
  45. // If you are using EF, you need to add the following configuration:
  46. // Notice: You don't need to config x.UseSqlServer(""") again! CAP can autodiscovery.
  47. x.UseEntityFramework<AppDbContext>();
  48. // If you are using ado.net,you need to add the configuration:
  49. x.UseSqlServer("Your ConnectionStrings");
  50. x.UseMySql("Your ConnectionStrings");
  51. x.UsePostgreSql("Your ConnectionStrings");
  52. // If you are using RabbitMQ, you need to add the configuration:
  53. x.UseRabbitMQ("localhost");
  54. // If you are using Kafka, you need to add the configuration:
  55. x.UseKafka("localhost");
  56. });
  57. }
  58. public void Configure(IApplicationBuilder app)
  59. {
  60. //.....
  61. app.UseCap();
  62. }
  63. ```
  64. ### Publish
  65. Inject `ICapPublisher` in your Controller, then use the `ICapPublisher` to send message
  66. ```c#
  67. public class PublishController : Controller
  68. {
  69. [Route("~/publishWithTransactionUsingEF")]
  70. public async Task<IActionResult> PublishMessageWithTransactionUsingEF([FromServices]AppDbContext dbContext, [FromServices]ICapPublisher publisher)
  71. {
  72. using (var trans = dbContext.Database.BeginTransaction())
  73. {
  74. // your business code
  75. //If you are using EF, CAP will automatic discovery current environment transaction, so you do not need to explicit pass parameters.
  76. //Achieving atomicity between original database operation and the publish event log thanks to a local transaction.
  77. await publisher.PublishAsync("xxx.services.account.check", new Person { Name = "Foo", Age = 11 });
  78. trans.Commit();
  79. }
  80. return Ok();
  81. }
  82. [Route("~/publishWithTransactionUsingAdonet")]
  83. public async Task<IActionResult> PublishMessageWithTransactionUsingAdonet([FromServices]ICapPublisher publisher)
  84. {
  85. var connectionString = "";
  86. using (var sqlConnection = new SqlConnection(connectionString))
  87. {
  88. sqlConnection.Open();
  89. using (var sqlTransaction = sqlConnection.BeginTransaction())
  90. {
  91. // your business code
  92. publisher.Publish("xxx.services.account.check", new Person { Name = "Foo", Age = 11 }, sqlTransaction);
  93. sqlTransaction.Commit();
  94. }
  95. }
  96. return Ok();
  97. }
  98. }
  99. ```
  100. ### Subscribe
  101. **Action Method**
  102. Add the Attribute `[CapSubscribe()]` on Action to subscribe message:
  103. ```c#
  104. public class PublishController : Controller
  105. {
  106. [CapSubscribe("xxx.services.account.check")]
  107. public async Task CheckReceivedMessage(Person person)
  108. {
  109. Console.WriteLine(person.Name);
  110. Console.WriteLine(person.Age);
  111. return Task.CompletedTask;
  112. }
  113. }
  114. ```
  115. **Service Method**
  116. If your subscribe method is not in the Controller,then your subscribe class need to Inheritance `ICapSubscribe`:
  117. ```c#
  118. namespace xxx.Service
  119. {
  120. public interface ISubscriberService
  121. {
  122. public void CheckReceivedMessage(Person person);
  123. }
  124. public class SubscriberService: ISubscriberService, ICapSubscribe
  125. {
  126. [CapSubscribe("xxx.services.account.check")]
  127. public void CheckReceivedMessage(Person person)
  128. {
  129. }
  130. }
  131. }
  132. ```
  133. Then inject your `ISubscriberService` class in Startup.cs
  134. ```c#
  135. public void ConfigureServices(IServiceCollection services)
  136. {
  137. //Note: The injection of services needs before of `services.AddCap()`
  138. services.AddTransient<ISubscriberService,SubscriberService>();
  139. services.AddCap(x=>{});
  140. }
  141. ```
  142. ### Dashboard
  143. CAP 2.1 and above 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.
  144. 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.
  145. ```c#
  146. services.AddCap(x =>
  147. {
  148. //...
  149. // Register Dashboard
  150. x.UseDashboard();
  151. // Register to Consul
  152. x.UseDiscovery(d =>
  153. {
  154. d.DiscoveryServerHostName = "localhost";
  155. d.DiscoveryServerPort = 8500;
  156. d.CurrentNodeHostName = "localhost";
  157. d.CurrentNodePort = 5800;
  158. d.NodeId = 1;
  159. d.NodeName = "CAP No.1 Node";
  160. });
  161. });
  162. ```
  163. 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.
  164. ![dashboard](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220827302-189215107.png)
  165. ![received](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220934115-1107747665.png)
  166. ![subscibers](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220949193-884674167.png)
  167. ![nodes](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004221001880-1162918362.png)
  168. ## Contribute
  169. 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.
  170. ### License
  171. [MIT](https://github.com/dotnetcore/CAP/blob/master/LICENSE.txt)