options:消息序列化增加NewtonsoftJson和ProtoBuf的支持

This commit is contained in:
hardstylewyl 2025-05-13 11:23:21 +08:00
parent 8dcff695a4
commit 99ba220951
5 changed files with 42 additions and 11 deletions

View File

@ -1,3 +1,6 @@
//#define ProtoBuf
#define NewtonsoftJson
using System.Globalization;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
@ -6,6 +9,9 @@ using Confluent.Kafka.Core.Consumer;
using Confluent.Kafka.Core.Producer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using SJZY.Expand.ABP.Core.Logger;
using SJZY.Track.Application.Helpers;
using SJZY.Track.Application.Services.Warning.Signals;
@ -78,11 +84,22 @@ public static class WarningCalcServiceExtension
reportFuncWrapper.Debounce();
});
//bb.WithProtobufNetDeserializer();
#if ProtoBuf
bb.WithProtobufNetDeserializer();
#elif NewtonsoftJson
bb.WithNewtonsoftJsonDeserializer(builder =>
builder.WithNullValueHandling(NullValueHandling.Ignore) // Ignores null values
.WithReferenceLoopHandling(ReferenceLoopHandling.Ignore) // Ignores reference loops
.WithContractResolver(new CamelCasePropertyNamesContractResolver()) // Camel case property names
);
#else
bb.WithJsonCoreDeserializer(bc =>
bc.WithReferenceHandler(ReferenceHandler.IgnoreCycles) // Handles circular references
.WithPropertyNamingPolicy(JsonNamingPolicy.CamelCase) // Uses camelCase naming
.WithDefaultIgnoreCondition(JsonIgnoreCondition.WhenWritingNull));
.WithDefaultIgnoreCondition(JsonIgnoreCondition.WhenWritingNull)
);
#endif
return (bb.Build() as IKafkaConsumer<TKey, TValue>)!;
});
}
@ -137,13 +154,22 @@ public static class WarningCalcServiceExtension
reportFuncWrapper.Debounce();
});
#if ProtoBuf
// 2000w条消息数据包压缩从 2g -> 1g
// bb.WithProtobufNetSerializer();
bb.WithProtobufNetSerializer();
#elif NewtonsoftJson
bb.WithNewtonsoftJsonSerializer(builder =>
builder.WithNullValueHandling(NullValueHandling.Ignore) // Ignores null values
.WithReferenceLoopHandling(ReferenceLoopHandling.Ignore) // Ignores reference loops
.WithContractResolver(new CamelCasePropertyNamesContractResolver()) // Camel case property names
);
#else
bb.WithJsonCoreSerializer(bc =>
bc.WithReferenceHandler(ReferenceHandler.IgnoreCycles) // Handles circular references
.WithPropertyNamingPolicy(JsonNamingPolicy.CamelCase) // Uses camelCase naming
.WithDefaultIgnoreCondition(JsonIgnoreCondition.WhenWritingNull));
.WithDefaultIgnoreCondition(JsonIgnoreCondition.WhenWritingNull)
);
#endif
return (bb.Build() as IKafkaProducer<TKey, TValue>)!;
});
}

View File

@ -9,10 +9,12 @@
<ItemGroup>
<PackageReference Include="BSI.Stack.Rabbit" Version="2.1.4" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.18" />
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.9.4" />
<PackageReference Include="Confluent.Kafka.Core" Version="1.0.0" />
<PackageReference Include="Confluent.Kafka.Core.Serialization.JsonCore" Version="1.0.0" />
<PackageReference Include="Confluent.Kafka.Core.Serialization.NewtonsoftJson" Version="1.0.0" />
<PackageReference Include="Confluent.Kafka.Core.Serialization.ProtobufNet" Version="1.0.0" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.18" />
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.9.4" />
<PackageReference Include="Savorboard.CAP.InMemoryMessageQueue" Version="8.2.1"/>
<PackageReference Include="SJZY.Expand.AliyunOSS" Version="7.0.17" />
<PackageReference Include="SJZY.InterfaceRelay.Application.Contracts" Version="7.2.5-alpha2" />

View File

@ -1,4 +1,4 @@
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate;
namespace SJZY.Track.Application.Services.Warning.Signals;
@ -20,6 +20,7 @@ public readonly struct WarningSignal
// public int OrderType { get; set; }
// public int SubSystemId { get; set; }
[System.Text.Json.Serialization.JsonConstructor]
[JsonConstructor]
public WarningSignal(WarningSignalType type, int orderId, string esId, int trackStatusId, int trackContentId)
{

View File

@ -1,4 +1,4 @@
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace SJZY.Track.Application.Services.Warning.Signals;
@ -10,6 +10,7 @@ public readonly struct WarningSignalKey : IEquatable<WarningSignalKey>
public WarningSignalType Type { get; }
public int OrderId { get; }
[System.Text.Json.Serialization.JsonConstructor]
[JsonConstructor]
public WarningSignalKey(WarningSignalType type, int orderId)
{

View File

@ -1,6 +1,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Confluent.Kafka;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace SJZY.Track.Application.Services.Warning.Signals;
@ -20,6 +20,7 @@ public struct WarningSignalMetadata : IEquatable<WarningSignalMetadata>
/// <summary> 最后一次出错的原因 </summary>
public string LastErrorReason { get; set; }
[System.Text.Json.Serialization.JsonIgnore]
[JsonIgnore]
public Message<WarningSignalKey, WarningSignal> OwenMessage { get; set; }