From 9a2327a6a9c9f94fd56b6fc4d6b0a08a2ae71bbb Mon Sep 17 00:00:00 2001 From: Savorboard Date: Fri, 20 Oct 2017 18:10:56 +0800 Subject: [PATCH] update samples. --- .../Sample.Kafka.SqlServer/AppDbContext.cs | 2 +- .../CmsContentSerializer.cs | 50 +++++++++++++++ .../Controllers/ValuesController.cs | 62 +++++++++++++++---- samples/Sample.Kafka.SqlServer/Program.cs | 16 +---- .../Sample.Kafka.SqlServer.csproj | 2 +- samples/Sample.Kafka.SqlServer/Startup.cs | 27 ++++---- 6 files changed, 114 insertions(+), 45 deletions(-) create mode 100644 samples/Sample.Kafka.SqlServer/CmsContentSerializer.cs diff --git a/samples/Sample.Kafka.SqlServer/AppDbContext.cs b/samples/Sample.Kafka.SqlServer/AppDbContext.cs index 5dd398b..78d6492 100644 --- a/samples/Sample.Kafka.SqlServer/AppDbContext.cs +++ b/samples/Sample.Kafka.SqlServer/AppDbContext.cs @@ -8,7 +8,7 @@ namespace Sample.Kafka.SqlServer protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { //optionsBuilder.UseSqlServer("Server=192.168.2.206;Initial Catalog=Sample.Kafka.SqlServer;User Id=cmswuliu;Password=h7xY81agBn*Veiu3;MultipleActiveResultSets=True"); - optionsBuilder.UseSqlServer("Server=DESKTOP-M9R8T31;Initial Catalog=Sample.Kafka.SqlServer;User Id=sa;Password=P@ssw0rd;MultipleActiveResultSets=True"); + optionsBuilder.UseSqlServer("Server=192.168.2.206;Initial Catalog=TestCap;User Id=cmswuliu;Password=h7xY81agBn*Veiu3;MultipleActiveResultSets=True"); } } } diff --git a/samples/Sample.Kafka.SqlServer/CmsContentSerializer.cs b/samples/Sample.Kafka.SqlServer/CmsContentSerializer.cs new file mode 100644 index 0000000..dbbd736 --- /dev/null +++ b/samples/Sample.Kafka.SqlServer/CmsContentSerializer.cs @@ -0,0 +1,50 @@ +using System; +using DotNetCore.CAP.Abstractions; +using DotNetCore.CAP.Models; +using Newtonsoft.Json; + +namespace Sample.RabbitMQ.SqlServer +{ + public class MessageContent : CapMessage + { + [JsonProperty("id")] + public override string Id { get; set; } + + [JsonProperty("createdTime")] + public override DateTime Timestamp { get; set; } + + [JsonProperty("msgBody")] + public override string Content { get; set; } + + [JsonProperty("callbackTopicName")] + public override string CallbackName { get; set; } + } + + public class MyMessagePacker : IMessagePacker + { + private readonly IContentSerializer _serializer; + + public MyMessagePacker(IContentSerializer serializer) + { + _serializer = serializer; + } + + public string Pack(CapMessage obj) + { + var content = new MessageContent + { + Id = obj.Id, + Content = obj.Content, + CallbackName = obj.CallbackName, + Timestamp = obj.Timestamp + }; + return _serializer.Serialize(content); + } + + public CapMessage UnPack(string packingMessage) + { + return _serializer.DeSerialize(packingMessage); + } + } +} + diff --git a/samples/Sample.Kafka.SqlServer/Controllers/ValuesController.cs b/samples/Sample.Kafka.SqlServer/Controllers/ValuesController.cs index f3c2426..77a28b3 100644 --- a/samples/Sample.Kafka.SqlServer/Controllers/ValuesController.cs +++ b/samples/Sample.Kafka.SqlServer/Controllers/ValuesController.cs @@ -4,18 +4,36 @@ using System.Threading.Tasks; using DotNetCore.CAP; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; namespace Sample.Kafka.SqlServer.Controllers { public class Person { - public int Id { get; set; } + [JsonProperty("id")] + public string Id { get; set; } + + [JsonProperty("uname")] public string Name { get; set; } - public int Age { get; set; } + public HAHA Haha { get; set; } + + public override string ToString() + { + return "Name:" + Name + ";Id:" + Id + "Haha:" + Haha?.ToString(); + } + } + + public class HAHA + { + [JsonProperty("id")] + public string Id { get; set; } + + [JsonProperty("uname")] + public string Name { get; set; } public override string ToString() { - return "Name:" + Name + ";Age:" + Age; + return "Name:" + Name + ";Id:" + Id; } } @@ -32,23 +50,43 @@ namespace Sample.Kafka.SqlServer.Controllers _dbContext = dbContext; } + [Route("~/publish")] public IActionResult PublishMessage() { + var p = new Person + { + Id = Guid.NewGuid().ToString(), + Name = "杨晓东", + Haha = new HAHA + { + Id = Guid.NewGuid().ToString(), + Name = "1-1杨晓东", + } + }; - _capBus.Publish("sample.rabbitmq.sqlserver.order.check", DateTime.Now); + _capBus.Publish("wl.yxd.test", p, "wl.yxd.test.callback"); - //var person = new Person - //{ - // Name = "杨晓东", - // Age = 11, - // Id = 23 - //}; - //_capBus.Publish("sample.rabbitmq.mysql33333", person); + //_capBus.Publish("wl.cj.test", p); return Ok(); } + [CapSubscribe("wl.yxd.test.callback")] + public void KafkaTestCallback(Person p) + { + Console.WriteLine("回调内容:" + p); + } + + + [CapSubscribe("wl.cj.test")] + public string KafkaTestReceived(Person person) + { + Console.WriteLine(person); + Debug.WriteLine(person); + return "this is callback message"; + } + [Route("~/publishWithTrans")] public async Task PublishMessageWithTransaction() { @@ -61,7 +99,7 @@ namespace Sample.Kafka.SqlServer.Controllers return Ok(); } - [CapSubscribe("sample.rabbitmq.mysql33333",Group ="Test.Group")] + [CapSubscribe("sample.rabbitmq.mysql33333", Group = "Test.Group")] public void KafkaTest22(Person person) { var aa = _dbContext.Database; diff --git a/samples/Sample.Kafka.SqlServer/Program.cs b/samples/Sample.Kafka.SqlServer/Program.cs index 0cba6ca..c8cd00a 100644 --- a/samples/Sample.Kafka.SqlServer/Program.cs +++ b/samples/Sample.Kafka.SqlServer/Program.cs @@ -8,21 +8,7 @@ namespace Sample.Kafka.SqlServer { public class Program { - - //var config = new ConfigurationBuilder() - // .AddCommandLine(args) - // .AddEnvironmentVariables("ASPNETCORE_") - // .Build(); - - //var host = new WebHostBuilder() - // .UseConfiguration(config) - // .UseKestrel() - // .UseContentRoot(Directory.GetCurrentDirectory()) - // .UseIISIntegration() - // .UseStartup() - // .Build(); - - //host.Run(); + public static void Main(string[] args) { BuildWebHost(args).Run(); diff --git a/samples/Sample.Kafka.SqlServer/Sample.Kafka.SqlServer.csproj b/samples/Sample.Kafka.SqlServer/Sample.Kafka.SqlServer.csproj index 769fa47..f569d66 100644 --- a/samples/Sample.Kafka.SqlServer/Sample.Kafka.SqlServer.csproj +++ b/samples/Sample.Kafka.SqlServer/Sample.Kafka.SqlServer.csproj @@ -2,7 +2,7 @@ netcoreapp2.0 - Sample.RabbitMQ.SqlServer + Sample.Kafka.SqlServer NU1701 NU1701 diff --git a/samples/Sample.Kafka.SqlServer/Startup.cs b/samples/Sample.Kafka.SqlServer/Startup.cs index 75b7e58..bc15d25 100644 --- a/samples/Sample.Kafka.SqlServer/Startup.cs +++ b/samples/Sample.Kafka.SqlServer/Startup.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Sample.RabbitMQ.SqlServer; namespace Sample.Kafka.SqlServer { @@ -15,28 +16,22 @@ namespace Sample.Kafka.SqlServer services.AddCap(x => { x.UseEntityFramework(); - x.UseKafka("192.168.1.26:9092"); + x.UseKafka("192.168.2.215:9092"); x.UseDashboard(); - x.UseDiscovery(d => - { - d.DiscoveryServerHostName = "localhost"; - d.DiscoveryServerPort = 8500; - d.CurrentNodeHostName = "localhost"; - d.CurrentNodePort = 5820; - d.NodeName = "CAP 2号节点"; - }); - }); - services.AddSession(); + //x.UseDiscovery(d => + //{ + // d.DiscoveryServerHostName = "localhost"; + // d.DiscoveryServerPort = 8500; + // d.CurrentNodeHostName = "localhost"; + // d.CurrentNodePort = 5820; + // d.NodeName = "CAP 2号节点"; + //}); + }).AddMessagePacker(); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { - loggerFactory.AddConsole(); - loggerFactory.AddDebug(); - - app.UseSession(); - app.UseMvc(); app.UseCap();