Ocelot中文文档-Getting Started

Getting Started

Ocelot是只在.NET Core运行,目前基于netstandard2.0构建的。如果Ocelot适合你们的话,那么这个文档会有所帮助。

.NET Core 2.0

安装Nuget包

安装Ocelot以及依赖包。你将新疆一个.netstandard 2.0项目,并引入包。然后根据下面的Startup和Configuration节点启动运行。

安装Ocelot Nugget命令行:Install-Package Ocelot

配置项

下面是最基本的ocelot.json配置。它不会做任何事,但是能够正常运行Ocelot

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

大多要注意的是 BaseUrl。Ocelot需要知道在那个URL下运行的,以便查找头文件和替换一些配置。这个URL是外部的地址,它在客户端上能看到Ocelot在运行在那个URL。如果你正在 http://123.12.1.1:6543 运行Ocelot容器,但是就好像有nginx代理一样,在https://api.mybusiness.com上响应。在这种情况下,Ocelot的BaseUrl应该是https://api.mybusiness.com

如果因为一些原因你要使用Ocelot容器并且想在 http://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

Install NuGet package

安装Ocelot及其依赖包。新建一个.net core 1.0项目并引入包。然后按照下面的Startup和Configuration节点设置并运行。请注意安装对应版本的nuget包。

Configuration

下面是最基本的配置内容。不会做任何事但是会正常运行项目

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

Program

接着在Program.cs输入如下代码

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

使用json文件配置启动实例的例子如下所示

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();
    }
}

你需要做的就是这么多。

猜你喜欢

转载自www.cnblogs.com/ms27946/p/Ocelot-ChineseDoc-GettingStarted.html