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
相关文章
相关标签/搜索