Ocelot集成Nacos注册中心组件
softlgl => https://github.com/softlgl/Ocelot.Provider.Nacos
public static IOcelotBuilder AddNacosDiscovery(this IOcelotBuilder builder)
{
builder.Services.AddNacosAspNet(builder.Configuration);
builder.Services.AddSingleton<ServiceDiscoveryFinderDelegate>(NacosProviderFactory.Get);
builder.Services.AddSingleton<OcelotMiddlewareConfigurationDelegate>(NacosMiddlewareConfigurationProvider.Get);
return builder;
}
/// <summary>
/// 使用基于Nacos配置管理和服务注册发现的Ocelot
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IOcelotBuilder AddOcelotWithNacos(this IServiceCollection services,string ocelotDataId= "Ocelot", string ocelotGroup= "Gateway")
{
INacosConfigService configService = services.BuildServiceProvider().GetService<INacosConfigService>();
IConfiguration configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
FileConfiguration fileConfiguration = new FileConfiguration();
try
{
var data = configService.GetConfig(ocelotDataId, ocelotGroup, 6000).Result;
fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(data);
}
catch(Exception ex)
{
throw new Exception("Nacos config fetch failure.", ex);
}
IOcelotBuilder builder = new NacosOcelotBuilder(services, configuration, fileConfiguration);
return builder.AddNacosDiscovery();
}
DI 注入
public void ConfigureServices(IServiceCollection services)
{
...
services.AddOptions();
services.AddLogging();
services.Configure<NacosSdkOptions>(configuration.GetSection("NacosConfig"));
services.AddSingleton<INacosConfigService>(p =>
{
return new NacosConfigService(p.GetService<ILoggerFactory>(), p.GetService<IOptions<NacosSdkOptions>>());
});
services.AddOcelotWithNacos();
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseOcelot(configuration).Wait();
app.UseOcelotNacosConfigureListeners();
}