Ocelot中文文档-入门

原文: Ocelot中文文档-入门

Ocelot只能用于.NET Core,目前是为netcoreapp2.0构建的,这个文档可能会帮你了解Ocelot是否适合你。html

.NET Core 2.0

安装NuGet包nginx

使用nuget安装Ocelot和它的依赖。你须要建立一个netcoreapp2.0的项目并安装Ocelot包。而后按照下面的配置部分启动并运行。json

Install-Package Ocelotapi

全部版本能够在这里找到。app

配置ui

下面是个很简单的ocelot.json。它不作任何事情,可是能够上Ocelot启动了。url

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

这里最重要的就是BaseUrl了。Ocelot须要知道它正在运行的URL,以便头部查找和替换以及一些管理配置。设置此URL时,它应该是客户端将看到的Ocelot运行的外部URL。例如,假如你在容器中运行Ocelot的url是http://123.12.1.1:6543,可是在它前面有像nginx同样的代理在响应https://api.mybusiness.com。在这种状况下Ocelot的BaseUrl应该是https://api.mybusiness.com命令行

若是因为某种缘由你使用的是容器,而且但愿Ocelot在http://123.12.1.1:6543上响应客户端,那么你能够这样作,但若是你正在部署多个Ocelot,你可能会但愿经过某种脚本在命令行上传递它。 但愿您使用的任何调度程序均可以传递这个IP。代理

程序code

而后在你的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

原文

相关文章
相关标签/搜索