perf:优化kafka
This commit is contained in:
parent
25c78d784f
commit
cfa2f5b7b7
@ -1,31 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0-preview.3.25171.5" />
|
||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.1" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\src\JiShe.CollectBus.KafkaProducer\JiShe.CollectBus.Kafka.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -1,79 +0,0 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using Confluent.Kafka;
|
||||
using JiShe.CollectBus.Kafka.AdminClient;
|
||||
using JiShe.CollectBus.Kafka.Consumer;
|
||||
using JiShe.CollectBus.Kafka.Producer;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using System.Text.Json;
|
||||
using static Confluent.Kafka.ConfigPropertyNames;
|
||||
|
||||
|
||||
|
||||
// 构建配置
|
||||
var config = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
// 直接读取配置项
|
||||
var greeting = config["ServerTagName"];
|
||||
Console.WriteLine(greeting); // 输出: Hello, World!
|
||||
|
||||
|
||||
// 创建服务容器
|
||||
var services = new ServiceCollection();
|
||||
// 注册 IConfiguration 实例
|
||||
services.AddSingleton<IConfiguration>(config);
|
||||
|
||||
// 初始化日志
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(config) // 从 appsettings.json 读取配置
|
||||
.CreateLogger();
|
||||
|
||||
// 配置日志系统
|
||||
services.AddLogging(logging =>
|
||||
{
|
||||
logging.ClearProviders();
|
||||
logging.AddSerilog();
|
||||
});
|
||||
services.AddSingleton<IAdminClientService, AdminClientService>();
|
||||
services.AddTransient(typeof(IProducerService<,>), typeof(ProducerService<,>));
|
||||
//services.AddSingleton(typeof(IConsumerService<,>), typeof(ConsumerService<,>));
|
||||
|
||||
// 构建ServiceProvider
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
// 获取日志记录器工厂
|
||||
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
logger.LogInformation("程序启动");
|
||||
|
||||
var adminClientService = serviceProvider.GetRequiredService<IAdminClientService>();
|
||||
|
||||
string topic = "test-topic";
|
||||
//await adminClientService.DeleteTopicAsync(topic);
|
||||
// 创建 topic
|
||||
await adminClientService.CreateTopicAsync(topic, 3, 3);
|
||||
|
||||
|
||||
var producerService = serviceProvider.GetRequiredService<IProducerService<Null, string>>();
|
||||
int i = 1;
|
||||
while (i <= 10)
|
||||
{
|
||||
await producerService.ProduceAsync(topic, JsonSerializer.Serialize(new { topic = topic, val = i }));
|
||||
i++;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
var key = Console.ReadKey(intercept: true); // intercept:true 隐藏按键显示
|
||||
|
||||
if (key.Key == ConsoleKey.Escape)
|
||||
{
|
||||
Console.WriteLine("\n程序已退出");
|
||||
break;
|
||||
}
|
||||
}
|
||||
(serviceProvider as IDisposable)?.Dispose();
|
||||
@ -1,134 +0,0 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [
|
||||
"Serilog.Sinks.Console",
|
||||
"Serilog.Sinks.File"
|
||||
],
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Volo.Abp": "Warning",
|
||||
"Hangfire": "Warning",
|
||||
"DotNetCore.CAP": "Warning",
|
||||
"Serilog.AspNetCore": "Information",
|
||||
"Microsoft.EntityFrameworkCore": "Warning",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "Console"
|
||||
},
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "./logs/logs-.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"App": {
|
||||
"SelfUrl": "http://localhost:44315",
|
||||
"CorsOrigins": "http://localhost:4200,http://localhost:3100"
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"Default": "mongodb://admin:admin02023@118.190.144.92:37117,118.190.144.92:37119,118.190.144.92:37120/JiSheCollectBus?authSource=admin&maxPoolSize=400&minPoolSize=10&waitQueueTimeoutMS=5000",
|
||||
"Kafka": "192.168.1.9:29092,192.168.1.9:39092,192.168.1.9:49092",
|
||||
"PrepayDB": "server=118.190.144.92;database=jishe.sysdb;uid=sa;pwd=admin@2023;Encrypt=False;Trust Server Certificate=False",
|
||||
"EnergyDB": "server=118.190.144.92;database=db_energy;uid=sa;pwd=admin@2023;Encrypt=False;Trust Server Certificate=False"
|
||||
},
|
||||
"Redis": {
|
||||
"Configuration": "192.168.1.9:6380,password=1q2w3e!@#,syncTimeout=30000,abortConnect=false,connectTimeout=30000,allowAdmin=true",
|
||||
"DefaultDB": "14",
|
||||
"HangfireDB": "15"
|
||||
},
|
||||
"Jwt": {
|
||||
"Audience": "JiShe.CollectBus",
|
||||
"SecurityKey": "dzehzRz9a8asdfasfdadfasdfasdfafsdadfasbasdf=",
|
||||
"Issuer": "JiShe.CollectBus",
|
||||
"ExpirationTime": 2
|
||||
},
|
||||
"HealthCheck": {
|
||||
"IsEnable": true,
|
||||
"MySql": {
|
||||
"IsEnable": true
|
||||
},
|
||||
"Pings": {
|
||||
"IsEnable": true,
|
||||
"Host": "https://www.baidu.com/",
|
||||
"TimeOut": 5000
|
||||
}
|
||||
},
|
||||
"SwaggerConfig": [
|
||||
{
|
||||
"GroupName": "Basic",
|
||||
"Title": "【后台管理】基础模块",
|
||||
"Version": "V1"
|
||||
},
|
||||
{
|
||||
"GroupName": "Business",
|
||||
"Title": "【后台管理】业务模块",
|
||||
"Version": "V1"
|
||||
}
|
||||
],
|
||||
"Cap": {
|
||||
"RabbitMq": {
|
||||
"HostName": "118.190.144.92",
|
||||
"UserName": "collectbus",
|
||||
"Password": "123456",
|
||||
"Port": 5672
|
||||
}
|
||||
},
|
||||
"Kafka": {
|
||||
"BootstrapServers": "192.168.1.9:29092,192.168.1.9:39092,192.168.1.9:49092",
|
||||
"EnableAuthorization": false,
|
||||
"SecurityProtocol": "SASL_PLAINTEXT",
|
||||
"SaslMechanism": "PLAIN",
|
||||
"SaslUserName": "lixiao",
|
||||
"SaslPassword": "lixiao1980"
|
||||
//"Topic": {
|
||||
// "ReplicationFactor": 3,
|
||||
// "NumPartitions": 1000
|
||||
//}
|
||||
},
|
||||
//"Kafka": {
|
||||
// "Connections": {
|
||||
// "Default": {
|
||||
// "BootstrapServers": "192.168.1.9:29092,192.168.1.9:39092,192.168.1.9:49092"
|
||||
// // "SecurityProtocol": "SASL_PLAINTEXT",
|
||||
// // "SaslMechanism": "PLAIN",
|
||||
// // "SaslUserName": "lixiao",
|
||||
// // "SaslPassword": "lixiao1980",
|
||||
// }
|
||||
// },
|
||||
// "Consumer": {
|
||||
// "GroupId": "JiShe.CollectBus"
|
||||
// },
|
||||
// "Producer": {
|
||||
// "MessageTimeoutMs": 6000,
|
||||
// "Acks": -1
|
||||
// },
|
||||
// "Topic": {
|
||||
// "ReplicationFactor": 3,
|
||||
// "NumPartitions": 1000
|
||||
// },
|
||||
// "EventBus": {
|
||||
// "GroupId": "JiShe.CollectBus",
|
||||
// "TopicName": "DefaultTopicName"
|
||||
// }
|
||||
//},
|
||||
"IoTDBOptions": {
|
||||
"UserName": "root",
|
||||
"Password": "root",
|
||||
"ClusterList": [ "192.168.1.9:6667" ],
|
||||
"PoolSize": 2,
|
||||
"DataBaseName": "energy",
|
||||
"OpenDebugMode": true,
|
||||
"UseTableSessionPoolByDefault": false
|
||||
},
|
||||
"ServerTagName": "JiSheCollectBus",
|
||||
"KafkaReplicationFactor": 3,
|
||||
"NumPartitions": 30
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user