Mico.Demo/MicoService.Demo/Program.cs

100 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using MassTransit;
using MassTransit.Message;
using Microservice.Common;
using Microservice.Common.Services;
using Nacos.AspNetCore.V2;
using Nacos.V2.DependencyInjection;
namespace MicoService.Demo
{
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.AddRequestClient<CheckInventoryRequest>();
// 配置 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();
// 注册用户服务
builder.Services.AddScoped<IUserService, UserService>();
// 注册业务逻辑服务
//builder.Services.AddScoped<IOrderInventoryNotificationService, OrderInventoryNotificationService>();
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();
}
}
}