using MassTransit; using MicoService.Demo2.Consumers; using Microservice.Common; using Nacos.AspNetCore.V2; using Nacos.V2.DependencyInjection; namespace MicoService.Demo2 { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); #region Nacos // 配置 Nacos 配置中心 builder.Host.UseNacosConfig("nacos"); builder.Services.AddNacosV2Config(builder.Configuration); // 配置 Nacos 注册发现 builder.Services.AddNacosAspNet(builder.Configuration); builder.Services.AddNacosV2Naming(builder.Configuration); #endregion // MassTransit 配置 // ============================================================ // 配置 MassTransit 总线,使用 RabbitMQ 作为消息中间件 builder.Services.AddMassTransit(x => { // 注册消费者 x.AddConsumer(); // 配置 RabbitMQ 连接 x.UsingRabbitMq((context, cfg) => { // 设置 RabbitMQ 主机连接信息 cfg.Host("rabbitmq://192.168.2.7:5672", h => { h.Username("rabbit"); h.Password("qwe123!@#"); }); // 配置消息重试机制:最多重试3次,每次间隔1秒 cfg.UseMessageRetry(r => r.Interval(3, 1000)); // 自动配置消费者端点,将消费者注册为 RabbitMQ 队列 cfg.ConfigureEndpoints(context); }); }); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); // 注册微服务通讯客户端 builder.Services.AddServiceClient(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); app.UseSwaggerUI(options => { options.SwaggerEndpoint("/openapi/v1.json", "API v1"); }); } app.UseAuthorization(); app.MapControllers(); app.Run(); } } }