码迷,mamicode.com
首页 > 其他好文 > 详细

Ocelot(七)- 入门

时间:2019-06-13 22:18:36      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:重要   sha   you   添加   baseurl   iis   容器   中间   应该   

入门

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

.NET 

安装NuGet包

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

Install-Package Ocelot

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

配置

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

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

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

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

程序

然后在您的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();
       }
   }

**Note:** When using ASP.NET Core 2.2 and you want to use In-Process hosting, replace **.UseIISIntegration()** with **.UseIIS()**, otherwise you‘ll get startup errors.

.NET 

安装NuGet包

使用nuget安装Ocelot及其依赖项。您需要创建一个netcoreapp1.0 + projct并将包带入其中。然后按照下面的“启动”和“ 配置”部分启动并运行。请注意,您需要从NuGet Feed中选择一个Ocelot包。

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

配置

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

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

程序

然后在您的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();
    }
}

启动

使用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(七)- 入门

标签:重要   sha   you   添加   baseurl   iis   容器   中间   应该   

原文地址:https://www.cnblogs.com/letyouknowdotnet/p/11019705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!