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

7 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # CAP
  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. [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/dotnetcore/CAP/master/LICENSE.txt)
  5. CAP һڷֲʽϵͳSOAMicroServiceʵһԵĿ⣬ʹáܵص㡣
  6. ## ԤOverView
  7. CAP һ ASP.NET Core ĿʹõĿ⣬Ȼ ASP.NET Core On .NET Framework С
  8. ԰ CAP һ EventBusΪ EventBus йܣCAPṩ˸Ӽ򻯵ķʽ EventBus еķͶġ
  9. CAP Ϣ־ûĹܣķ崻ʱԱ֤ϢĿɿԡCAPṩ˻Microsoft DI Producer Service Ժҵ޷ϣ֧ǿһԵ
  10. CAPASP.NET Core ΢ܹеһʾͼ
  11. ![](http://images2015.cnblogs.com/blog/250417/201706/250417-20170630143600289-1065294295.png)
  12. > ͼʵִ߲û룬ִ߲CAPڲʵ֡
  13. ## Getting Started
  14. ### NuGet δ
  15. Ŀаװ CAP
  16. Ϣʹõ Kafka Ļԣ
  17. ```
  18. PM> Install-Package DotNetCore.CAP.Kafka
  19. ```
  20. Ϣʹõ RabbitMQ Ļԣ
  21. ```
  22. PM> Install-Package DotNetCore.CAP.RabbitMQ
  23. ```
  24. CAP Ĭṩ Entity Framwork Ϊݿ洢
  25. ```
  26. PM> Install-Package DotNetCore.CAP.EntityFrameworkCore
  27. ```
  28. ### Configuration
  29. CAP Startup.cs ļУ£
  30. ```cs
  31. public void ConfigureServices(IServiceCollection services)
  32. {
  33. ......
  34. services.AddDbContext<AppDbContext>();
  35. services.AddCap()
  36. .AddEntityFrameworkStores<AppDbContext>()
  37. .AddKafka(x => x.Servers = "localhost:9453");
  38. }
  39. public void Configure(IApplicationBuilder app)
  40. {
  41. .....
  42. app.UseCap();
  43. }
  44. ```
  45. ###
  46. Controller ע `ICapProducerService` Ȼʹ `ICapProducerService` Ϣ
  47. ```cs
  48. public class PublishController : Controller
  49. {
  50. private readonly ICapProducerService _producer;
  51. public PublishController(ICapProducerService producer)
  52. {
  53. _producer = producer;
  54. }
  55. [Route("~/checkAccount")]
  56. public async Task<IActionResult> PublishMessage()
  57. {
  58. //ָ͵Ϣͷ
  59. await _producer.SendAsync("xxx.services.account.check", new Person { Name = "Foo", Age = 11 });
  60. return Ok();
  61. }
  62. }
  63. ```
  64. ###
  65. **Action Method**
  66. Action Attribute Ϣ
  67. ʹõ Kafak ʹ `[KafkaTopic()]`, RabbitMQ ʹ `[RabbitMQTopic()]`
  68. ```cs
  69. public class PublishController : Controller
  70. {
  71. private readonly ICapProducerService _producer;
  72. public PublishController(ICapProducerService producer)
  73. {
  74. _producer = producer;
  75. }
  76. [NoAction]
  77. [KafkaTopic("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 У㶩ĵҪ̳ `IConsumerService`
  88. ```cs
  89. namespace xxx.Service
  90. {
  91. public interface ISubscriberService
  92. {
  93. public void CheckReceivedMessage(Person person);
  94. }
  95. public class SubscriberService: ISubscriberService, IConsumerService
  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. ׵򵥵ķ֮һDzۺ⣨issueҲͨύ Pull Request ס
  113. ### License
  114. MIT