Ocelot中文文檔入門

入門

Ocelot僅適用於.NET Core,目前是根據netstandard2.0構建的,若是Ocelot適合您,這個文檔可能會有用。nginx

.NET Core 2.1

安裝NuGet包json

使用nuget安裝Ocelot及其依賴項。 您須要建立一個netstandard2.0項目並將其打包到其中。 而後按照下面的「啓動」和「配置」部分啓動並運行。api

Install-Package Ocelotapp

全部版本均可以在這裏找到ui

配置spa

如下是一個很是基本的ocelot.json。 它不會作任何事情,但應該讓Ocelot開始。命令行

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

這裏要注意的最重要的是BaseUrl。 Ocelot須要知道它正在運行的URL,以便執行Header查找和替換以及某些管理配置。 設置此URL時,它應該是客戶端將看到運行Ocelot的外部URL,例如 若是你正在運行容器,Ocelot可能會在網址http://123.12.1.1:6543上運行,但在https://api.mybusiness.com上響應它以前有相似nginx的東西。 在這種狀況下,Ocelot基本網址應爲https://api.mybusiness.com。 code

若是因爲某種緣由你正在使用容器而且但願Ocelot在http://123.12.1.1:6543上響應客戶端,那麼你能夠這樣作可是若是要部署多個Ocelot,你可能但願在命令行中傳遞它 某種腳本。 但願您使用的任何調度程序均可以傳遞IP。
 
Program.cs文件
而後在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中間件

安裝NuGet包blog

使用nuget安裝Ocelot及其依賴。 您須要建立一個netcoreapp1.0 + projct並將包帶入其中。 而後按照下面的「啓動」和「配置」部分啓動並運行。 請注意,您須要從NuGet Feed中選擇一個Ocelot包。

全部版本均可以在 這裏找到
 
配置
 
如下是一個很是基本的ocelot.json。 它不會作任何事情,但應該讓Ocelot開始。
 
{
    { "ReRoutes": [],
    "GlobalConfiguration": {}
}

 

Program.cs文件
 
而後在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.cs文件
 
使用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();
    }
}
以上這些就是Ocelot基本入門內容。喜歡就收藏此文。版權全部,禁止未經受權的複製轉載。詳細的中文文檔查閱 http://nopapp.com/Blog/Article/Ocelot-Basic
相關文章
相關標籤/搜索