.NET Core API网关Ocelot(一)【概览,开始】

概览

Ocelot的目标是使用.NET运行面向微服务/面向服务的架构,需要统一的入口点进入他们的系统。

特别是与IdentityServer引用和承载令牌轻松集成。

Ocelot是一组按特定顺序排列的中间件。

OcelotHttpRequest对象操作到其配置指定的状态,直到它到达请求构建器中间件,在该中间件中,它创建一个HttpRequestMessage对象,该对象用于向下游服务发出请求。发出请求的中间件是Ocelot管道中的最后一件事。它不会调用下一个中间件。来自下游服务的响应存储在每个请求范围的存储库中,并在请求返回Ocelot管道时进行检索。有一个中间件将HttpResponseMessage映射到HttpResponse对象并返回给客户端。基本上它具有许多其他功能。


开始

Ocelot仅适用于.NET Core,目前是为netstandard2.0构建的。 如果Ocelot适合您,那么这个文档可能会有用。

.NET Core 2.1

使用nuget安装Ocelot及其依赖项。 您需要创建一个netstandard2.0项目并将其打包到其中。 然后按照下面的“启动”和“配置”部分启动并运行。

Install-Package Ocelot

所有版本都可以在这里找到

配置

以下是一个非常基本的ocelot.json。 它不会做任何事情,但应该让Ocelot启动。

{
    "ReRoutes": [],
    "GlobalConfiguration": {
        "BaseUrl": "https://api.mybusiness.com"
    }
}

这里要注意的最重要的是BaseUrlOcelot需要知道它正在运行的URL,以便进行Header查找和替换以及某些管理配置。 设置此URL时,它应该是客户端将看到运行Ocelot的外部URL,例如 如果您正在运行容器,Ocelot可能会在网址http://123.12.1.1:6543上运行,但在https://api.mybusiness.com上响应之前有类似nginx的内容。 在这种情况下,Ocelot基本网址应为https://api.mybusiness.com

如果您正在使用容器并要求Ocelothttp://123.12.1.1:6543响应客户端,那么您可以执行此操作,但是如果您要部署多个Ocelot,您可能希望在某种类型脚本的命令行上传递它。希望您使用的任何调度程序都可以通过IP。

Program
然后在您的Program.cs中,您将需要以下内容。 需要注意的主要事项是 AddOcelot()(添加ocelot服务),UseOcelot().Wait()(设置所有Ocelot中间件)。

public class Program
{
    public static void Main(string[] args)
    {
         new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config
                    .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                    .AddJsonFile("appsettings.json", true, true)
                    .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                    .AddJsonFile("ocelot.json")
                    .AddEnvironmentVariables();
            })
            .ConfigureServices(s => {
                s.AddOcelot();
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                //add your logging
            })
            .UseIISIntegration()
            .Configure(app =>
            {
                app.UseOcelot().Wait();
            })
            .Build()
            .Run();
    }
}

.NET Core 1.0

配置

{
    "ReRoutes": [],
    "GlobalConfiguration": {}
}

Program

public class Program
{
    public static void Main(string[] args)
    {
        IWebHostBuilder builder = new WebHostBuilder();

        builder.ConfigureServices(s => {
        });

        builder.UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>();

        var host = builder.Build();

        host.Run();
    }
}

Startup

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddJsonFile("ocelot.json")
            .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOcelot(Configuration);
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseOcelot().Wait();
    }
}

猜你喜欢

转载自blog.csdn.net/WuLex/article/details/88075014