From 4e4a0b442c7c2113c6a04c4e103b71af47dd2492 Mon Sep 17 00:00:00 2001 From: erius Date: Sun, 28 Aug 2022 05:08:04 +0300 Subject: [PATCH] fixed rabbit mq in web api events task --- WebAPIEventsTask/WebAPIEventsTask.sln | 8 +++- .../Controllers/EventController.cs | 28 +++---------- .../WebAPIEventsTask/IMessageService.cs | 8 ++++ WebAPIEventsTask/WebAPIEventsTask/Program.cs | 4 +- .../WebAPIEventsTask/RabbitMQService.cs | 42 +++++++++++++++++++ WebAPIEventsTask/WebAPIEventsTask/Startup.cs | 7 ---- .../WebAPIEventsTask/WebAPIEventsTask.csproj | 1 + .../WebAPIEventsTask/appsettings.json | 2 +- WebAPIEventsTask/docker-compose.dcproj | 18 ++++++++ WebAPIEventsTask/docker-compose.override.yml | 13 ++++++ WebAPIEventsTask/docker-compose.yml | 14 +++++++ 11 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 WebAPIEventsTask/WebAPIEventsTask/IMessageService.cs create mode 100644 WebAPIEventsTask/WebAPIEventsTask/RabbitMQService.cs delete mode 100644 WebAPIEventsTask/WebAPIEventsTask/Startup.cs create mode 100644 WebAPIEventsTask/docker-compose.dcproj create mode 100644 WebAPIEventsTask/docker-compose.override.yml create mode 100644 WebAPIEventsTask/docker-compose.yml diff --git a/WebAPIEventsTask/WebAPIEventsTask.sln b/WebAPIEventsTask/WebAPIEventsTask.sln index 6a2a728..b633d78 100644 --- a/WebAPIEventsTask/WebAPIEventsTask.sln +++ b/WebAPIEventsTask/WebAPIEventsTask.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.2.32630.192 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAPIEventsTask", "WebAPIEventsTask\WebAPIEventsTask.csproj", "{E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebAPIEventsTask", "WebAPIEventsTask\WebAPIEventsTask.csproj", "{E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}" +EndProject +Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{B3774CDE-2560-4C27-81F9-688296719AFE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}.Debug|Any CPU.Build.0 = Debug|Any CPU {E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}.Release|Any CPU.ActiveCfg = Release|Any CPU {E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}.Release|Any CPU.Build.0 = Release|Any CPU + {B3774CDE-2560-4C27-81F9-688296719AFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B3774CDE-2560-4C27-81F9-688296719AFE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B3774CDE-2560-4C27-81F9-688296719AFE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B3774CDE-2560-4C27-81F9-688296719AFE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WebAPIEventsTask/WebAPIEventsTask/Controllers/EventController.cs b/WebAPIEventsTask/WebAPIEventsTask/Controllers/EventController.cs index 8bbbc42..d951039 100644 --- a/WebAPIEventsTask/WebAPIEventsTask/Controllers/EventController.cs +++ b/WebAPIEventsTask/WebAPIEventsTask/Controllers/EventController.cs @@ -1,7 +1,4 @@ using Microsoft.AspNetCore.Mvc; -using RabbitMQ.Client; -using RabbitMQ.Client.Events; -using System.Text; namespace WebAPIEventsTask.Controllers { @@ -11,41 +8,26 @@ namespace WebAPIEventsTask.Controllers { private readonly ILogger _logger; private readonly IConfiguration _configuration; - private static IModel rmqChannel; - private static readonly string QUEUE_NAME = "test-queue"; + private readonly IMessageService _messageService; - static EventController() - { - var connectionFactory = new ConnectionFactory { HostName = "localhost" }; - var connection = connectionFactory.CreateConnection(); - rmqChannel = connection.CreateModel(); - } - - public EventController(ILogger logger, IConfiguration configuration) + public EventController(ILogger logger, IConfiguration configuration, IMessageService messageService) { _logger = logger; _configuration = configuration; - rmqChannel.QueueDeclare(QUEUE_NAME, false, false, false); + _messageService = messageService; } [HttpPost("send")] public IActionResult SendMessage(string msg) { - rmqChannel.BasicPublish("", QUEUE_NAME, null, Encoding.UTF8.GetBytes(msg)); + _messageService.SendMessage(msg); return Ok(); } [HttpGet("receive")] public IActionResult ReceiveMessage() { - var consumer = new EventingBasicConsumer(rmqChannel); - string? msg = null; - consumer.Received += (model, ea) => - { - var body = ea.Body.ToArray(); - msg = Encoding.UTF8.GetString(body); - }; - rmqChannel.BasicConsume(QUEUE_NAME, true, consumer); + string? msg = _messageService.ReceiveMessage(); return msg == null ? NotFound() : Ok(msg); } } diff --git a/WebAPIEventsTask/WebAPIEventsTask/IMessageService.cs b/WebAPIEventsTask/WebAPIEventsTask/IMessageService.cs new file mode 100644 index 0000000..f4eff20 --- /dev/null +++ b/WebAPIEventsTask/WebAPIEventsTask/IMessageService.cs @@ -0,0 +1,8 @@ +namespace WebAPIEventsTask +{ + public interface IMessageService + { + public void SendMessage(string message); + public string? ReceiveMessage(); + } +} diff --git a/WebAPIEventsTask/WebAPIEventsTask/Program.cs b/WebAPIEventsTask/WebAPIEventsTask/Program.cs index 48863a6..2ca169d 100644 --- a/WebAPIEventsTask/WebAPIEventsTask/Program.cs +++ b/WebAPIEventsTask/WebAPIEventsTask/Program.cs @@ -1,7 +1,9 @@ +using WebAPIEventsTask; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. - +builder.Services.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); diff --git a/WebAPIEventsTask/WebAPIEventsTask/RabbitMQService.cs b/WebAPIEventsTask/WebAPIEventsTask/RabbitMQService.cs new file mode 100644 index 0000000..ed18a84 --- /dev/null +++ b/WebAPIEventsTask/WebAPIEventsTask/RabbitMQService.cs @@ -0,0 +1,42 @@ +using RabbitMQ.Client; +using System.Text; + +namespace WebAPIEventsTask +{ + public class RabbitMQService : IMessageService + { + private readonly IConfiguration _configuration; + private readonly ConnectionFactory _connectionFactory; + private readonly IConnection _connection; + private readonly IModel _channel; + + public RabbitMQService(IConfiguration configuration) + { + _configuration = configuration; + _connectionFactory = new() { Uri = new(_configuration.GetConnectionString("RabbitMQ")) }; + _connection = _connectionFactory.CreateConnection(); + _channel = _connection.CreateModel(); + _channel.QueueDeclare(queue: "msg-queue", + durable: false, + exclusive: false, + autoDelete: false); + } + + public string? ReceiveMessage() + { + var result = _channel.BasicGet("msg-queue", true); + if (result is null) return null; + var body = result.Body.ToArray(); + var msg = Encoding.UTF8.GetString(body); + return msg; + } + + public void SendMessage(string message) + { + var body = Encoding.UTF8.GetBytes(message); + _channel.BasicPublish(exchange: "", + routingKey: "msg-queue", + body: body); + } + } +} diff --git a/WebAPIEventsTask/WebAPIEventsTask/Startup.cs b/WebAPIEventsTask/WebAPIEventsTask/Startup.cs deleted file mode 100644 index 51b9c19..0000000 --- a/WebAPIEventsTask/WebAPIEventsTask/Startup.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace WebAPIEventsTask -{ - public class Startup - { - - } -} diff --git a/WebAPIEventsTask/WebAPIEventsTask/WebAPIEventsTask.csproj b/WebAPIEventsTask/WebAPIEventsTask/WebAPIEventsTask.csproj index e4a9c73..4b8e330 100644 --- a/WebAPIEventsTask/WebAPIEventsTask/WebAPIEventsTask.csproj +++ b/WebAPIEventsTask/WebAPIEventsTask/WebAPIEventsTask.csproj @@ -6,6 +6,7 @@ enable befb116a-f711-4f3f-9475-d96563d5378b Linux + ..\docker-compose.dcproj diff --git a/WebAPIEventsTask/WebAPIEventsTask/appsettings.json b/WebAPIEventsTask/WebAPIEventsTask/appsettings.json index 8654134..eae062b 100644 --- a/WebAPIEventsTask/WebAPIEventsTask/appsettings.json +++ b/WebAPIEventsTask/WebAPIEventsTask/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "RabbitMQ": "amqp://guest:guest@localhost:5672" + "RabbitMQ": "amqp://guest:guest@rabbitmq:5672" } } diff --git a/WebAPIEventsTask/docker-compose.dcproj b/WebAPIEventsTask/docker-compose.dcproj new file mode 100644 index 0000000..1e7f39f --- /dev/null +++ b/WebAPIEventsTask/docker-compose.dcproj @@ -0,0 +1,18 @@ + + + + 2.1 + Linux + b3774cde-2560-4c27-81f9-688296719afe + LaunchBrowser + {Scheme}://localhost:{ServicePort}/swagger + webapieventstask + + + + docker-compose.yml + + + + + \ No newline at end of file diff --git a/WebAPIEventsTask/docker-compose.override.yml b/WebAPIEventsTask/docker-compose.override.yml new file mode 100644 index 0000000..5e1dac7 --- /dev/null +++ b/WebAPIEventsTask/docker-compose.override.yml @@ -0,0 +1,13 @@ +version: '3.4' + +services: + webapieventstask: + environment: + - ASPNETCORE_ENVIRONMENT=Development + - ASPNETCORE_URLS=https://+:443;http://+:80 + ports: + - "80" + - "443" + volumes: + - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro + - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro \ No newline at end of file diff --git a/WebAPIEventsTask/docker-compose.yml b/WebAPIEventsTask/docker-compose.yml new file mode 100644 index 0000000..d3f970e --- /dev/null +++ b/WebAPIEventsTask/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.4' + +services: + webapieventstask: + container_name: webapi + image: ${DOCKER_REGISTRY-}webapieventstask + build: + context: . + dockerfile: WebAPIEventsTask/Dockerfile + rabbitmq: + container_name: rabbitmq + image: rabbitmq + ports: + - 5672:5672