Working communication via kafka
This commit is contained in:
@ -1,15 +1,19 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Confluent.Kafka;
|
||||
using Serializers;
|
||||
using Model;
|
||||
|
||||
class Producer
|
||||
{
|
||||
public static async Task Main (string[] args)
|
||||
{
|
||||
var config = new ProducerConfig { BootstrapServers = "localhost:9092" };
|
||||
var config = new ProducerConfig { BootstrapServers = "localhost:29092" };
|
||||
|
||||
using (var producerBuilder = new ProducerBuilder<string, DataModel>(config).Build())
|
||||
using (var producerBuilder = new ProducerBuilder<string, DataModel>(config)
|
||||
.SetValueSerializer(new JsonSerializer<DataModel>())
|
||||
.Build())
|
||||
{
|
||||
try
|
||||
{
|
||||
|
22
producer/Serializer/JsonSerializer.cs
Normal file
22
producer/Serializer/JsonSerializer.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Confluent.Kafka;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Serializers
|
||||
{
|
||||
public class JsonSerializer<T> : ISerializer<T>, IDeserializer<T>
|
||||
{
|
||||
public byte[] Serialize(T data, SerializationContext context)
|
||||
{
|
||||
return JsonSerializer.SerializeToUtf8Bytes(data);
|
||||
}
|
||||
|
||||
public T Deserialize(ReadOnlySpan<byte> data, bool isNull, SerializationContext context)
|
||||
{
|
||||
if (!isNull)
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(data);
|
||||
}
|
||||
throw new Exception ("Data cannot be null");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user