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 9.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
7 years ago
5 years ago
5 years ago
4 years ago
4 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
4 years ago
7 years ago
7 years ago
6 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
6 years ago
8 years ago
5 years ago
5 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/yang-xiaodong/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, has the function of EventBus, it is lightweight, easy to use, and efficient.
  12. In the process of building an SOA or MicroService system, we usually need to use the event to integrate each service. In the process, simple use of message queue does not guarantee reliability. CAP adopts local message table program integrated with the current database to solve exceptions that 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 CAP as an EventBus. CAP provides a simpler way to implement event publishing and subscriptions. You do not need to inherit or implement any interface during subscription and sending process.
  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. CAP can be installed in your project with the following command.
  20. ```
  21. PM> Install-Package DotNetCore.CAP
  22. ```
  23. CAP supports RabbitMQ, Kafka, AzureService, AmazonSQS as message queue, following packages are available 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. PM> Install-Package DotNetCore.CAP.AmazonSQS
  29. ```
  30. CAP supports SqlServer, MySql, PostgreSql,MongoDB as event log storage.
  31. ```
  32. // select a database provider you are using, event log table will integrate into.
  33. PM> Install-Package DotNetCore.CAP.SqlServer
  34. PM> Install-Package DotNetCore.CAP.MySql
  35. PM> Install-Package DotNetCore.CAP.PostgreSql
  36. PM> Install-Package DotNetCore.CAP.MongoDB //need MongoDB 4.0+ cluster
  37. ```
  38. ### Configuration
  39. First, you need to configure CAP in your Startup.cs:
  40. ```cs
  41. public void ConfigureServices(IServiceCollection services)
  42. {
  43. //......
  44. services.AddDbContext<AppDbContext>(); //Options, If you are using EF as the ORM
  45. services.AddSingleton<IMongoClient>(new MongoClient("")); //Options, If you are using MongoDB
  46. services.AddCap(x =>
  47. {
  48. // If you are using EF, you need to add the configuration:
  49. x.UseEntityFramework<AppDbContext>(); //Options, Notice: You don't need to config x.UseSqlServer(""") again! CAP can autodiscovery.
  50. // If you are using ADO.NET, choose to add configuration you needed:
  51. x.UseSqlServer("Your ConnectionStrings");
  52. x.UseMySql("Your ConnectionStrings");
  53. x.UsePostgreSql("Your ConnectionStrings");
  54. // If you are using MongoDB, you need to add the configuration:
  55. x.UseMongoDB("Your ConnectionStrings"); //MongoDB 4.0+ cluster
  56. // CAP support RabbitMQ,Kafka,AzureService as the MQ, choose to add configuration you needed:
  57. x.UseRabbitMQ("ConnectionString");
  58. x.UseKafka("ConnectionString");
  59. x.UseAzureServiceBus("ConnectionString");
  60. x.UseAmazonSQS();
  61. });
  62. }
  63. ```
  64. ### Publish
  65. Inject `ICapPublisher` in your Controller, then use the `ICapPublisher` to send messages
  66. ```c#
  67. public class PublishController : Controller
  68. {
  69. private readonly ICapPublisher _capBus;
  70. public PublishController(ICapPublisher capPublisher)
  71. {
  72. _capBus = capPublisher;
  73. }
  74. [Route("~/adonet/transaction")]
  75. public IActionResult AdonetWithTransaction()
  76. {
  77. using (var connection = new MySqlConnection(ConnectionString))
  78. {
  79. using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true))
  80. {
  81. //your business logic code
  82. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  83. }
  84. }
  85. return Ok();
  86. }
  87. [Route("~/ef/transaction")]
  88. public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
  89. {
  90. using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
  91. {
  92. //your business logic code
  93. _capBus.Publish("xxx.services.show.time", DateTime.Now);
  94. }
  95. return Ok();
  96. }
  97. }
  98. ```
  99. ### Subscribe
  100. **In Controller Action**
  101. Add the Attribute `[CapSubscribe()]` on Action to subscribe to messages:
  102. ```c#
  103. public class PublishController : Controller
  104. {
  105. [CapSubscribe("xxx.services.show.time")]
  106. public void CheckReceivedMessage(DateTime datetime)
  107. {
  108. Console.WriteLine(datetime);
  109. }
  110. }
  111. ```
  112. **In Business Logic Service**
  113. If your subscription method is not in the Controller, then your subscribe class needs to implement `ICapSubscribe` interface:
  114. ```c#
  115. namespace BusinessCode.Service
  116. {
  117. public interface ISubscriberService
  118. {
  119. void CheckReceivedMessage(DateTime datetime);
  120. }
  121. public class SubscriberService: ISubscriberService, ICapSubscribe
  122. {
  123. [CapSubscribe("xxx.services.show.time")]
  124. public void CheckReceivedMessage(DateTime datetime)
  125. {
  126. }
  127. }
  128. }
  129. ```
  130. Then register your class that implements `ISubscriberService` in Startup.cs
  131. ```c#
  132. public void ConfigureServices(IServiceCollection services)
  133. {
  134. //Note: The injection of services needs before of `services.AddCap()`
  135. services.AddTransient<ISubscriberService,SubscriberService>();
  136. services.AddCap(x=>
  137. {
  138. //...
  139. });
  140. }
  141. ```
  142. #### Use partials for topic subscriptions
  143. To group topic subscriptions on class level you're able to define a subscription on a method as a partial. Subscriptions on the message queue will then be a combination of the topic defined on the class and the topic defined on the method. In the following example the `Create(..)` function will be invoked when receiving a message on `customers.create`
  144. ```c#
  145. [CapSubscribe("customers")]
  146. public class CustomersSubscriberService : ICapSubscribe
  147. {
  148. [CapSubscribe("create", isPartial: true)]
  149. public void Create(Customer customer)
  150. {
  151. }
  152. }
  153. ```
  154. #### Subscribe Group
  155. 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.
  156. When CAP startups, it will use the current assembly name as the default group name, if multiple same group subscribers subscribe to the same topic name, there is only one subscriber that can receive the message.
  157. Conversely, if subscribers are in different groups, they will all receive messages.
  158. In the same application, you can specify `Group` property to keep subscriptions in different subscribe groups:
  159. ```C#
  160. [CapSubscribe("xxx.services.show.time", Group = "group1" )]
  161. public void ShowTime1(DateTime datetime)
  162. {
  163. }
  164. [CapSubscribe("xxx.services.show.time", Group = "group2")]
  165. public void ShowTime2(DateTime datetime)
  166. {
  167. }
  168. ```
  169. `ShowTime1` and `ShowTime2` will be called at the same time.
  170. BTW, You can specify the default group name in the configuration:
  171. ```C#
  172. services.AddCap(x =>
  173. {
  174. x.DefaultGroup = "default-group-name";
  175. });
  176. ```
  177. ### Dashboard
  178. CAP v2.1+ provides dashboard pages, you can easily view messages that were sent and received. In addition, you can also view the message status in real time in the dashboard. Use the following command to install the Dashboard in your project.
  179. ```
  180. PM> Install-Package DotNetCore.CAP.Dashboard
  181. ```
  182. In the distributed environment, the dashboard built-in integrates [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.
  183. ```c#
  184. services.AddCap(x =>
  185. {
  186. //...
  187. // Register Dashboard
  188. x.UseDashboard();
  189. // Register to Consul
  190. x.UseDiscovery(d =>
  191. {
  192. d.DiscoveryServerHostName = "localhost";
  193. d.DiscoveryServerPort = 8500;
  194. d.CurrentNodeHostName = "localhost";
  195. d.CurrentNodePort = 5800;
  196. d.NodeId = 1;
  197. d.NodeName = "CAP No.1 Node";
  198. });
  199. });
  200. ```
  201. The default dashboard address is :[http://localhost:xxx/cap](http://localhost:xxx/cap), you can configure relative path `/cap` with `x.UseDashboard(opt =>{ opt.MatchPath="/mycap"; })`.
  202. ![dashboard](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220827302-189215107.png)
  203. ![received](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220934115-1107747665.png)
  204. ![subscibers](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004220949193-884674167.png)
  205. ![nodes](http://images2017.cnblogs.com/blog/250417/201710/250417-20171004221001880-1162918362.png)
  206. ## Contribute
  207. 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.
  208. ### License
  209. [MIT](https://github.com/dotnetcore/CAP/blob/master/LICENSE.txt)