简体   繁体   English

API 网关 Ocelot.Net Core 6.1 安装

[英]API Gateway Ocelot .Net Core 6.1 Setup

The.Net 6 have removed the Start up Class and i am not able to find out how to configure Ocelot in new.Net 6 structure. The.Net 6 已经删除了 Start up Class,我无法找到如何在新的.Net 6 结构中配置 Ocelot。 I have found two methos我找到了两种方法

 using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddOcelot()// 1.ocelot.json goes where?
// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot(); // 2.what is the use of this

Let me know Please请让我知道

Add json file called ocelot.json in your project.在您的项目中添加名为ocelot.json的 json 文件。

Then do configure like this in Program.cs :然后在Program.cs中进行这样的配置:

IConfiguration configuration = new ConfigurationBuilder()
                            .AddJsonFile("ocelot.json")
                            .Build();

var builder = WebApplication.CreateBuilder(args);
//.....
builder.Services.AddOcelot(configuration);

var app = builder.Build();

//........
app.UseOcelot();

//......

You probably already have solved this, so this is meant for all the other developers looking for a solution to this.您可能已经解决了这个问题,所以这意味着所有其他正在寻找解决方案的开发人员。 Below are two ways of adding the Ocelot configuration.下面是两种添加 Ocelot 配置的方法。

  1. Add a new JSON file in your project named ocelot.json and add your configuration for ocelot inside.在名为ocelot.json的项目中添加一个新的JSON file ,并在其中添加 ocelot 的配置。
  2. The file ocelot.json has to be registered in Program.cs in order for Ocelot to load the configuration for your API Gateway. ocelot.json文件必须在Program.cs中注册,以便 Ocelot 加载 API 网关的配置。

Below are two examples of how you can register your Ocelot configuration.以下是如何注册 Ocelot 配置的两个示例。

1. Adding Ocelot configuration without environment check 1.添加Ocelot配置不带环境检查


using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

IConfiguration configuration = new ConfigurationBuilder()
                            .AddJsonFile("ocelot.json")
                            .Build();

builder.Services.AddOcelot(configuration);

var app = builder.Build();

await app.UseOcelot();

app.MapGet("/", () => "Hello World!");

app.Run();

As you can see we load the configuration from ocelot.json by using .ConfigurationBuilder() .如您所见,我们使用 .ConfigurationBuilder .ConfigurationBuilder()从 ocelot.json 加载配置。 We then parse the configuration to the method for adding Ocelot to the service container before registering it's middleware.然后我们将配置解析为在注册它的中间件之前将 Ocelot 添加到服务容器的方法。

2. Add Ocelot configuration for the current environment 2.为当前环境添加Ocelot配置

I tend to have multiple environments for production, testing, local development, etc... instead of re-writing/updating the configuration loader with the specific configuration file for Ocelot, we can do it by checking what environment we are running.我倾向于拥有用于生产、测试、本地开发等的多个环境……我们可以通过检查正在运行的环境来完成,而不是使用 Ocelot 的特定配置文件重新编写/更新配置加载程序。


using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

IConfiguration configuration = new ConfigurationBuilder()
                            .AddJsonFile($"ocelot.{builder.Environment.EnvironmentName}.json", true, true)
                            .Build();

builder.Services.AddOcelot(configuration);

var app = builder.Build();

await app.UseOcelot();

app.MapGet("/", () => "Hello World!");

app.Run();

In the code above we use IHostEnvironment to get the current environment name.在上面的代码中,我们使用IHostEnvironment来获取当前环境名称。 We can then use string interpolation to dynamically insert the environment name into the string of our ocelot configuration file.然后我们可以使用字符串插值将环境名称动态插入到我们的 ocelot 配置文件的字符串中。

For this to work, you would have to add a new configuration file for each environment like this:为此,您必须为每个环境添加一个新的配置文件,如下所示:

ocelot.json
├─ ocelot.Development.json
├─ ocelot.Local.json
├─ ocelot.Test.json

You need to declare direct from your program.cs you add your Ocelot json file in bulder.configuration, than in services add the Ocelot reference, and in the end start the intance app.Ocelot().wait();您需要直接从您的 program.cs 声明您在 bulder.configuration 中添加您的 Ocelot json 文件,然后在服务中添加 Ocelot 引用,最后启动实例 app.Ocelot().wait();

Here is an example, hope it helps下面是一个例子,希望对你有帮助

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile("ocelot.json");

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot();

var app = builder.Build();

//if (app.Environment.IsDevelopment())
//{
    app.UseSwagger();
    app.UseSwaggerUI();
//}

app.UseHttpsRedirection();

app.UseOcelot().Wait();

app.UseAuthorization();

app.MapControllers();

app.Run();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM