diff --git a/CAP.sln b/CAP.sln index e1e20b4..26769fb 100644 --- a/CAP.sln +++ b/CAP.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26430.13 +VisualStudioVersion = 15.0.26430.14 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9B2AE124-6636-4DE9-83A3-70360DABD0C4}" EndProject @@ -19,6 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution ConfigureMSDTC.ps1 = ConfigureMSDTC.ps1 LICENSE.txt = LICENSE.txt README.md = README.md + README.zh-cn.md = README.zh-cn.md EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{9E5A7F49-8E31-4A71-90CC-1DA9AEDA99EE}" @@ -36,11 +37,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.EntityFramew EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{3A6B6931-A123-477A-9469-8B468B5385AF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Kafka", "samples\Sample.Kafka\Sample.Kafka.csproj", "{2F095ED9-5BC9-4512-9013-A47685FB2508}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Kafka", "samples\Sample.Kafka\Sample.Kafka.csproj", "{2F095ED9-5BC9-4512-9013-A47685FB2508}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCore.CAP.Kafka", "src\DotNetCore.CAP.Kafka\DotNetCore.CAP.Kafka.csproj", "{C42CDE33-0878-4BA0-96F2-4CB7C8FDEAAD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.Kafka", "src\DotNetCore.CAP.Kafka\DotNetCore.CAP.Kafka.csproj", "{C42CDE33-0878-4BA0-96F2-4CB7C8FDEAAD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCore.CAP.RabbitMQ", "src\DotNetCore.CAP.RabbitMQ\DotNetCore.CAP.RabbitMQ.csproj", "{9961B80E-0718-4280-B2A0-271B003DE26B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.RabbitMQ", "src\DotNetCore.CAP.RabbitMQ\DotNetCore.CAP.RabbitMQ.csproj", "{9961B80E-0718-4280-B2A0-271B003DE26B}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{10C0818D-9160-4B80-BB86-DDE925B64D43}" ProjectSection(SolutionItems) = preProject diff --git a/README.zh-cn.md b/README.zh-cn.md new file mode 100644 index 0000000..9f9274c --- /dev/null +++ b/README.zh-cn.md @@ -0,0 +1,171 @@ +# CAP +[![Travis branch](https://img.shields.io/travis/dotnetcore/CAP/master.svg?label=travis-ci)](https://travis-ci.org/dotnetcore/CAP) +[![Travis](https://travis-ci.org/dotnetcore/CAP.svg?branch=master)](https://travis-ci.org/dotnetcore/CAP) +[![AppVeyor](https://ci.appveyor.com/api/projects/status/4mpe0tbu7n126vyw?svg=true)](https://ci.appveyor.com/project/yuleyule66/cap) +[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/dotnetcore/CAP/master/LICENSE.txt) + +CAP ÊÇÒ»¸öÔÚ·Ö²¼Ê½ÏµÍ³£¨SOA¡¢MicroService£©ÖÐʵÏÖ×îÖÕÒ»ÖÂÐԵĿ⣬Ëü¾ßÓÐÇáÁ¿¼¶¡¢Ò×ʹÓᢸßÐÔÄܵÈÌص㡣 + +## Ô¤ÀÀ£¨OverView£© + +CAP ÊÇÔÚÒ»¸ö ASP.NET Core ÏîÄ¿ÖÐʹÓõĿ⣬µ±È»Ëû¿ÉÒÔÓÃÓÚ ASP.NET Core On .NET Framework ÖС£ + +Äã¿ÉÒÔ°Ñ CAP ¿´³ÉÊÇÒ»¸ö EventBus£¬ÒòΪËü¾ßÓÐ EventBus µÄËùÓй¦ÄÜ£¬²¢ÇÒCAPÌṩÁ˸ü¼Ó¼ò»¯µÄ·½Ê½À´´¦Àí EventBus Öеķ¢²¼ºÍ¶©ÔÄ¡£ + +CAP ¾ßÓÐÏûÏ¢³Ö¾Ã»¯µÄ¹¦ÄÜ£¬µ±ÄãµÄ·þÎñ½øÐÐÖØÆô»òÕßå´»úʱËü¿ÉÒÔ±£Ö¤ÏûÏ¢µÄ¿É¿¿ÐÔ¡£CAPÌṩÁË»ùÓÚMicrosoft DI µÄ Producer Service ·þÎñ£¬Ëü¿ÉÒÔºÍÄãµÄÒµÎñ·þÎñ½øÐÐÎÞ·ì½áºÏ£¬²¢ÇÒÖ§³ÖÇ¿Ò»ÖÂÐÔµÄÊÂÎñ¡£ + +ÕâÊÇCAP¼¯ÔÚASP.NET Core ΢·þÎñ¼Ü¹¹ÖеÄÒ»¸öʾÒâͼ£º + +![](http://images2015.cnblogs.com/blog/250417/201706/250417-20170630143600289-1065294295.png) + +> ͼÖÐʵÏß²¿·Ö´ú±íÓû§´úÂ룬ÐéÏß²¿·Ö´ú±íCAPÄÚ²¿ÊµÏÖ¡£ + +## Getting Started + +### NuGet ÔÝδ·¢²¼ + +Äã¿ÉÒÔÔËÐÐÒÔÏÂÏÂÃüÁîÔÚÄãµÄÏîÄ¿Öа²×° CAP¡£ + +Èç¹ûÄãµÄÏûÏ¢¶ÓÁÐʹÓõÄÊÇ Kafka µÄ»°£¬Äã¿ÉÒÔ£º + +``` +PM> Install-Package DotNetCore.CAP.Kafka +``` + +Èç¹ûÄãµÄÏûÏ¢¶ÓÁÐʹÓõÄÊÇ RabbitMQ µÄ»°£¬Äã¿ÉÒÔ£º + +``` +PM> Install-Package DotNetCore.CAP.RabbitMQ +``` + +CAP ĬÈÏÌṩÁË Entity Framwork ×÷ΪÊý¾Ý¿â´æ´¢£º + +``` +PM> Install-Package DotNetCore.CAP.EntityFrameworkCore +``` + +### Configuration + +Ê×ÏÈÅäÖÃCAPµ½ Startup.cs ÎļþÖУ¬ÈçÏ£º + +```cs +public void ConfigureServices(IServiceCollection services) +{ + ...... + + services.AddDbContext(); + + services.AddCap() + .AddEntityFrameworkStores() + .AddKafka(x => x.Servers = "localhost:9453"); +} + +public void Configure(IApplicationBuilder app) +{ + ..... + + app.UseCap(); +} + +``` + +### ·¢²¼ + +ÔÚ Controller ÖÐ×¢Èë `ICapProducerService` È»ºóʹÓà `ICapProducerService` ½øÐÐÏûÏ¢·¢ËÍ + +```cs +public class PublishController : Controller +{ + private readonly ICapProducerService _producer; + + public PublishController(ICapProducerService producer) + { + _producer = producer; + } + + + [Route("~/checkAccount")] + public async Task PublishMessage() + { + //Ö¸¶¨·¢Ë͵ÄÏûϢͷºÍÄÚÈÝ + await _producer.SendAsync("xxx.services.account.check", new Person { Name = "Foo", Age = 11 }); + + return Ok(); + } +} + +``` + +### ¶©ÔÄ + +**Action Method** + +ÔÚActionÉÏÌí¼Ó Attribute À´¶©ÔÄÏà¹ØÏûÏ¢¡£ + +Èç¹ûÄãʹÓõÄÊÇ Kafak ÔòʹÓà `[KafkaTopic()]`, Èç¹ûÊÇ RabbitMQ ÔòʹÓà `[RabbitMQTopic()]` + +```cs +public class PublishController : Controller +{ + private readonly ICapProducerService _producer; + + public PublishController(ICapProducerService producer) + { + _producer = producer; + } + + + [NoAction] + [KafkaTopic("xxx.services.account.check")] + public async Task CheckReceivedMessage(Person person) + { + Console.WriteLine(person.Name); + Console.WriteLine(person.Age); + return Task.CompletedTask; + } +} + +``` + +**Service Method** + +Èç¹ûÄãµÄ¶©ÔÄ·½·¨Ã»ÓÐλÓÚ Controller ÖУ¬ÔòÄ㶩ÔĵÄÀàÐèÒª¼Ì³Ð `IConsumerService`£º + +```cs + +namespace xxx.Service +{ + public interface ISubscriberService + { + public void CheckReceivedMessage(Person person); + } + + + public class SubscriberService: ISubscriberService, IConsumerService + { + [KafkaTopic("xxx.services.account.check")] + public void CheckReceivedMessage(Person person) + { + + } + } +} + +``` + +È»ºóÔÚ Startup.cs ÖÐµÄ `ConfigureServices()` ÖÐ×¢ÈëÄãµÄ `ISubscriberService` Àà + +```cs +public void ConfigureServices(IServiceCollection services) +{ + services.AddTransient(); +} +``` + +## ¹±Ï× + +¹±Ï×µÄ×î¼òµ¥µÄ·½·¨Ö®Ò»¾ÍÊÇÊDzÎÓëÌÖÂÛºÍÌÖÂÛÎÊÌ⣨issue£©¡£ÄãÒ²¿ÉÒÔͨ¹ýÌá½»µÄ Pull Request ´úÂë±ä¸ü×÷³ö¹±Ïס£ + +### License + +MIT diff --git a/samples/Sample.Kafka/Controllers/ValuesController.cs b/samples/Sample.Kafka/Controllers/ValuesController.cs index 468ab37..44ddc00 100644 --- a/samples/Sample.Kafka/Controllers/ValuesController.cs +++ b/samples/Sample.Kafka/Controllers/ValuesController.cs @@ -24,18 +24,26 @@ namespace Sample.Kafka.Controllers } public string ServerPath => ((IHostingEnvironment)HttpContext.RequestServices.GetService(typeof(IHostingEnvironment))).ContentRootPath; - [KafkaTopic("zzwl.topic.finace.callBack", IsOneWay = true, GroupOrExchange = "test")] - [NonAction] - public void KafkaTest() + [KafkaTopic("zzwl.topic.finace.callBack", GroupOrExchange = "test")] + public void KafkaTest(Person person) { - Console.WriteLine("kafka test invoked"); + Console.WriteLine(person.Name); + Console.WriteLine(person.Age); + } [Route("~/send")] public async Task SendTopic() { - await _producer.SendAsync("zzwl.topic.finace.callBack", "{\"msgBody\":\"{\\\"dealno\\\":null,\\\"businesstype\\\":\\\"1\\\",\\\"serialno\\\":\\\"435ldfhj345\\\",\\\"bankno\\\":\\\"650001\\\",\\\"amt\\\":20.0,\\\"virtualstatus\\\":1,\\\"paystatus\\\":1}\",\"callbackTopicName\":\"zzwl.topic.finace.callBack\",\"createId\":null,\"retryLimit\":0}"); + await _producer.SendAsync("zzwl.topic.finace.callBack", new Person { Name = "Test", Age = 11 }); return Ok(); } + + public class Person + { + public string Name { get; set; } + + public int Age { get; set; } + } } } \ No newline at end of file diff --git a/samples/Sample.Kafka/Startup.cs b/samples/Sample.Kafka/Startup.cs index 4f30119..d234b37 100644 --- a/samples/Sample.Kafka/Startup.cs +++ b/samples/Sample.Kafka/Startup.cs @@ -24,17 +24,17 @@ namespace Sample.Kafka public void ConfigureServices(IServiceCollection services) { services.AddDbContext(); - - services.AddConsistency() - .AddEntityFrameworkStores() - .AddRabbitMQ(x => - { - x.HostName = "192.168.2.206"; - x.UserName = "admin"; - x.Password = "123123"; - }) - .AddKafka(x => x.Servers = ""); + services.AddCap() + .AddEntityFrameworkStores() + .AddRabbitMQ(x => + { + x.HostName = "192.168.2.206"; + x.UserName = "admin"; + x.Password = "123123"; + }); + //.AddKafka(x => x.Servers = ""); + // Add framework services. services.AddMvc(); } diff --git a/test/Shared/MessageManagerTestBase.cs b/test/Shared/MessageManagerTestBase.cs index 2825191..d026487 100644 --- a/test/Shared/MessageManagerTestBase.cs +++ b/test/Shared/MessageManagerTestBase.cs @@ -21,7 +21,7 @@ namespace DotNetCore.CAP.Test protected virtual void SetupMessageServices(IServiceCollection services, object context = null) { services.AddSingleton(); - services.AddConsistency(); + services.AddCap(); AddMessageStore(services, context); services.AddSingleton>(new TestLogger());