working version

This commit is contained in:
kct949 2023-02-15 22:59:10 +01:00
parent 2af8c1ba25
commit 4b537d2aad
6 changed files with 42 additions and 9 deletions

View File

@ -0,0 +1,15 @@
using System.Linq;
using System.Text;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
public static class Utils
{
public static string CalculateMd5Hash(string input)
{
using var md5 = System.Security.Cryptography.MD5.Create();
var inputBytes = Encoding.ASCII.GetBytes(input);
var hashBytes = md5.ComputeHash(inputBytes);
return Convert.ToHexString(hashBytes);
}
}

View File

@ -34,6 +34,9 @@ public class EventBusKafka : IEventBus, IDisposable
var message = new Message<string, string> { Key = eventName, Value = jsonMessage };
var kafkaHandle =
new DependentProducerBuilder<string, string>(_persistentConnection.Handle).Build();
Console.WriteLine($"Publishing event: {eventName}\n Content: {Utils.CalculateMd5Hash(jsonMessage)}");
kafkaHandle.ProduceAsync(TopicName, message);
}

View File

@ -1,3 +1,4 @@
using System.Security.Cryptography;
using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
@ -14,16 +15,15 @@ public class KafkaConsumerBackgroundService : BackgroundService
private const string AutofacScopeName = "eshop_event_bus";
private const string TopicName = "eshop_event_bus";
public KafkaConsumerBackgroundService(IConfiguration config,
public KafkaConsumerBackgroundService(IConfiguration configuration,
IEventBusSubscriptionsManager subsManager,
ILifetimeScope autofac,
ILogger<KafkaConsumerBackgroundService> logger)
{
var consumerConfig = new ConsumerConfig();
config.GetSection("Kafka:ConsumerSettings").Bind(consumerConfig);
configuration.GetSection("Kafka:ConsumerSettings").Bind(consumerConfig);
_kafkaConsumer = new ConsumerBuilder<string, string>(consumerConfig).Build();
_subsManager = subsManager;
_autofac = autofac;
_logger = logger;
@ -45,6 +45,9 @@ public class KafkaConsumerBackgroundService : BackgroundService
var consumeResult = _kafkaConsumer.Consume(cancellationToken);
var eventName = consumeResult.Message.Key;
var messageContent = consumeResult.Message.Value;
Console.WriteLine($"Consumed event: {eventName}\n Content: {Utils.CalculateMd5Hash(messageContent)}");
if (!_subsManager.HasSubscriptionsForEvent(eventName))
{
@ -71,7 +74,7 @@ public class KafkaConsumerBackgroundService : BackgroundService
var handler = scope.ResolveOptional(subscription.HandlerType);
if (handler == null) continue;
var eventType = _subsManager.GetEventTypeByName(eventName);
var integrationEvent = JsonSerializer.Deserialize(consumeResult.Message.Value,
var integrationEvent = JsonSerializer.Deserialize(messageContent,
eventType,
new JsonSerializerOptions
{

View File

@ -14,12 +14,13 @@
"ConnectionString": "127.0.0.1",
"AzureServiceBusEnabled": false,
"EventBusConnection": "localhost",
"KafkaEnabled": true,
"Kafka": {
"ProducerSettings": {
"BootstrapServers": "localhost:9092"
"BootstrapServers": "broker:9092"
},
"ConsumerSettings": {
"BootstrapServers": "localhost:9092",
"BootstrapServers": "broker:9092",
"GroupId": "basket-group-id"
}
}

View File

@ -26,5 +26,15 @@
"Name": "eshop",
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret"
},
"KafkaEnabled": true,
"Kafka": {
"ProducerSettings": {
"BootstrapServers": "broker:9092"
},
"ConsumerSettings": {
"BootstrapServers": "broker:9092",
"GroupId": "basket-group-id"
}
}
}

View File

@ -33,6 +33,7 @@ services:
- "6379:6379"
volumes:
- eshop-basketdata:/data
rabbitmq:
ports:
- "15672:15672"
@ -47,8 +48,8 @@ services:
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_INTERNAL://broker:29092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:9092,PLAINTEXT_HOST://localhost:29092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1