Browse Source

Add capability to optionally set kafka message key via new header (#498)

master
Andrew Jaffie 4 years ago
committed by GitHub
parent
commit
0838a23da1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 1 deletions
  1. +1
    -1
      src/DotNetCore.CAP.Kafka/ITransport.Kafka.cs
  2. +2
    -0
      src/DotNetCore.CAP.Kafka/KafkaConsumerClient.cs
  3. +10
    -0
      src/DotNetCore.CAP.Kafka/KafkaHeaders.cs

+ 1
- 1
src/DotNetCore.CAP.Kafka/ITransport.Kafka.cs View File

@@ -43,7 +43,7 @@ namespace DotNetCore.CAP.Kafka
var result = await producer.ProduceAsync(message.GetName(), new Message<string, byte[]>
{
Headers = headers,
Key = message.GetId(),
Key = message.Headers.TryGetValue(KafkaHeaders.KafkaKey, out string kafkaMessageKey) && !string.IsNullOrEmpty(kafkaMessageKey) ? kafkaMessageKey : message.GetId(),
Value = message.Body
});



+ 2
- 0
src/DotNetCore.CAP.Kafka/KafkaConsumerClient.cs View File

@@ -62,6 +62,8 @@ namespace DotNetCore.CAP.Kafka
}
headers.Add(Messages.Headers.Group, _groupId);

headers.Add(KafkaHeaders.KafkaKey, consumerResult.Key);

if (_kafkaOptions.CustomHeaders != null)
{
var customHeaders = _kafkaOptions.CustomHeaders(consumerResult);


+ 10
- 0
src/DotNetCore.CAP.Kafka/KafkaHeaders.cs View File

@@ -0,0 +1,10 @@
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace DotNetCore.CAP.Kafka
{
public static class KafkaHeaders
{
public const string KafkaKey = "cap-kafka-key";
}
}

Loading…
Cancel
Save