初始化
This commit is contained in:
1332
消费/EnergyConsumption.cs
Normal file
1332
消费/EnergyConsumption.cs
Normal file
File diff suppressed because it is too large
Load Diff
123
消费/Program.cs
Normal file
123
消费/Program.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using BLWData.Entity;
|
||||
using Confluent.Kafka;
|
||||
using static Confluent.Kafka.ConfigPropertyNames;
|
||||
|
||||
namespace 消费
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
RunConsumers().Wait();
|
||||
Console.WriteLine("Hello, World!");
|
||||
Console.ReadKey();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//private const string TopicName = "firsttopic";
|
||||
private const string TopicName = "blwlog4BaoJing-rcu-udppackage-topic";
|
||||
private const string BootstrapServers = "43.138.217.154:9092";
|
||||
private const string GroupId = "parallel-consumer-group";
|
||||
private const int ConsumerCount = 3; // 启动3个消费者
|
||||
|
||||
public static async Task RunConsumers()
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
var consumers = new List<Task>();
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
consumers.Add(Task.Run(() => StartConsumer(cts.Token)));
|
||||
}
|
||||
|
||||
await Task.WhenAll(consumers);
|
||||
|
||||
//await StartConsumer(cts.Token);
|
||||
}
|
||||
|
||||
private static async Task StartConsumer(CancellationToken cancellationToken)
|
||||
{
|
||||
//var config = new ConsumerConfig
|
||||
//{
|
||||
// BootstrapServers = BootstrapServers,
|
||||
// GroupId = GroupId,
|
||||
// AutoOffsetReset = AutoOffsetReset.Earliest,
|
||||
// SecurityProtocol = SecurityProtocol.SaslPlaintext,
|
||||
// SaslMechanism = SaslMechanism.Plain,
|
||||
// EnableAutoCommit = true, // 手动提交偏移量
|
||||
// EnablePartitionEof = true,
|
||||
// AllowAutoCreateTopics = true,
|
||||
// SaslUsername = "aaa",
|
||||
// SaslPassword = "aaa123"
|
||||
//};
|
||||
|
||||
var config = new ConsumerConfig
|
||||
{
|
||||
GroupId = "laravel-consumer-kafka-group",
|
||||
AutoOffsetReset = AutoOffsetReset.Earliest,
|
||||
//BootstrapServers = BootstrapServers,
|
||||
BootstrapServers = "43.138.217.154:9092",
|
||||
SecurityProtocol = SecurityProtocol.SaslPlaintext,
|
||||
SaslMechanism = SaslMechanism.Plain,
|
||||
EnableAutoCommit = true, // 手动提交偏移量
|
||||
SaslUsername = "baojing",
|
||||
SaslPassword = "blwmaigong##2025"
|
||||
|
||||
};
|
||||
|
||||
var consumer = new ConsumerBuilder<string, byte[]>(config).Build();
|
||||
//var consumer = new ConsumerBuilder<string, byte[]>(config).SetErrorHandler((_, e) => Console.WriteLine($"消费者错误: {e.Reason}")).SetPartitionsAssignedHandler((c, partitions) =>
|
||||
// {
|
||||
// Console.WriteLine($"消费者分配到分区: {string.Join(", ", partitions)}");
|
||||
// }).Build();
|
||||
consumer.Subscribe(TopicName);
|
||||
|
||||
try
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
var consumeResult = consumer.Consume(cancellationToken);
|
||||
|
||||
var k = consumeResult.Message.Key;
|
||||
var v = consumeResult.Message.Value;
|
||||
EnergyConsumption ese = new EnergyConsumption();
|
||||
|
||||
if (k.Equals(""))
|
||||
{
|
||||
|
||||
}
|
||||
var SSA = EnergyConsumption.Parser.ParseFrom(v);
|
||||
if (consumeResult.IsPartitionEOF)
|
||||
{
|
||||
Console.WriteLine($"消费者1到达分区末尾: {consumeResult.TopicPartitionOffset}");
|
||||
continue;
|
||||
}
|
||||
|
||||
Console.WriteLine($"消费者1收到消息: [分区{consumeResult.Partition}] " +
|
||||
$"偏移量{consumeResult.Offset}: {consumeResult.Message.Value}");
|
||||
|
||||
// 手动提交偏移量
|
||||
//consumer.Commit(consumeResult);
|
||||
}
|
||||
catch (ConsumeException e)
|
||||
{
|
||||
Console.WriteLine($"消费者消费错误: {e.Error.Reason}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
consumer.Close();
|
||||
}
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
消费/消费.csproj
Normal file
16
消费/消费.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<StartupObject>消费.Program</StartupObject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Confluent.Kafka" Version="2.11.1" />
|
||||
<PackageReference Include="Google.Protobuf" Version="3.33.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user