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.zh-cn.md 3.9 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
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # CAP                       [English](https://github.com/dotnetcore/CAP/blob/master/README.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/4mpe0tbu7n126vyw?svg=true)](https://ci.appveyor.com/project/yuleyule66/cap)
  4. [![NuGet](https://img.shields.io/nuget/vpre/DotNetCore.CAP.svg)](https://www.nuget.org/packages/DotNetCore.CAP/)
  5. [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/dotnetcore/CAP/master/LICENSE.txt)
  6. CAP 是一个在分布式系统(SOA、MicroService)中实现最终一致性的库,它具有轻量级、易使用、高性能等特点。
  7. ## 预览(OverView)
  8. CAP 是在一个 ASP.NET Core 项目中使用的库,当然他可以用于 ASP.NET Core On .NET Framework 中。
  9. 你可以把 CAP 看成是一个 EventBus,因为它具有 EventBus 的所有功能,并且 CAP 提供了更加简化的方式来处理 EventBus 中的发布和订阅。
  10. CAP 具有消息持久化的功能,当你的服务进行重启或者宕机时它可以保证消息的可靠性。CAP提供了基于Microsoft DI 的 Publisher Service 服务,它可以和你的业务服务进行无缝结合,并且支持强一致性的事务。
  11. 这是CAP集在ASP.NET Core 微服务架构中的一个示意图:
  12. ![](http://images2015.cnblogs.com/blog/250417/201707/250417-20170705175827128-1203291469.png)
  13. > 图中实线部分代表用户代码,虚线部分代表CAP内部实现。
  14. ## Getting Started
  15. ### NuGet
  16. 你可以运行以下下命令在你的项目中安装 CAP。
  17. 如果你的消息队列使用的是 Kafka 的话,你可以:
  18. ```
  19. PM> Install-Package DotNetCore.CAP.Kafka -Pre
  20. ```
  21. 如果你的消息队列使用的是 RabbitMQ 的话,你可以:
  22. ```
  23. PM> Install-Package DotNetCore.CAP.RabbitMQ -Pre
  24. ```
  25. CAP 默认提供了 Entity Framwork 作为数据库存储:
  26. ```
  27. PM> Install-Package DotNetCore.CAP.EntityFrameworkCore -Pre
  28. ```
  29. ### Configuration
  30. 首先配置CAP到 Startup.cs 文件中,如下:
  31. ```cs
  32. public void ConfigureServices(IServiceCollection services)
  33. {
  34. ......
  35. services.AddDbContext<AppDbContext>();
  36. services.AddCap()
  37. .AddEntityFrameworkStores<AppDbContext>()
  38. .AddKafka(x => x.Servers = "localhost:9092");
  39. }
  40. public void Configure(IApplicationBuilder app)
  41. {
  42. .....
  43. app.UseCap();
  44. }
  45. ```
  46. ### 发布
  47. 在 Controller 中注入 `ICapPublisher` 然后使用 `ICapPublisher` 进行消息发送
  48. ```cs
  49. public class PublishController : Controller
  50. {
  51. private readonly ICapPublisher _publisher;
  52. public PublishController(ICapPublisher publisher)
  53. {
  54. _publisher = publisher;
  55. }
  56. [Route("~/checkAccount")]
  57. public async Task<IActionResult> PublishMessage()
  58. {
  59. //指定发送的消息头和内容
  60. await _publisher.PublishAsync("xxx.services.account.check", new Person { Name = "Foo", Age = 11 });
  61. return Ok();
  62. }
  63. }
  64. ```
  65. ### 订阅
  66. **Action Method**
  67. 在 Action 上添加 CapSubscribeAttribute 来订阅相关消息。
  68. ```cs
  69. public class PublishController : Controller
  70. {
  71. private readonly ICapPublisher _publisher;
  72. public PublishController(ICapPublisher publisher)
  73. {
  74. _publisher = publisher;
  75. }
  76. [NoAction]
  77. [CapSubscribe("xxx.services.account.check")]
  78. public async Task CheckReceivedMessage(Person person)
  79. {
  80. Console.WriteLine(person.Name);
  81. Console.WriteLine(person.Age);
  82. return Task.CompletedTask;
  83. }
  84. }
  85. ```
  86. **Service Method**
  87. 如果你的订阅方法没有位于 Controller 中,则你订阅的类需要继承 `ICapSubscribe`:
  88. ```cs
  89. namespace xxx.Service
  90. {
  91. public interface ISubscriberService
  92. {
  93. public void CheckReceivedMessage(Person person);
  94. }
  95. public class SubscriberService: ISubscriberService, ICapSubscribe
  96. {
  97. [KafkaTopic("xxx.services.account.check")]
  98. public void CheckReceivedMessage(Person person)
  99. {
  100. }
  101. }
  102. }
  103. ```
  104. 然后在 Startup.cs 中的 `ConfigureServices()` 中注入你的 `ISubscriberService` 类
  105. ```cs
  106. public void ConfigureServices(IServiceCollection services)
  107. {
  108. services.AddTransient<ISubscriberService,SubscriberService>();
  109. }
  110. ```
  111. ## 贡献
  112. 贡献的最简单的方法之一就是是参与讨论和讨论问题(issue)。你也可以通过提交的 Pull Request 代码变更作出贡献。
  113. ### License
  114. MIT