Ocelot (seven) - Getting Started

getting Started

Ocelot is only available for .NET Core, currently is netstandard2.0 built. If Ocelot is right for you, then this document may be useful.

.NET 

NuGet installation package

Use nuget installation Ocelot and its dependencies. You need to create a netstandard2.0 project and package them into it. Then follow the "start" and "  Configuration " section up and running.

Install-Package Ocelot

All versions can be here found .

Configuration

The following is a very basic ocelot.json. It does not do anything, but let Ocelot should begin.

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

The most important thing to note here is BaseUrl. Ocelot need to know the URL it is running, for Header find and replace, and certain management configuration. When you set this URL, it should be the client will see running Ocelot external URL, for example, if you are running container, Ocelot might run on the URL http://123.12.1.1:6543 but in front of a similar nginx response in the https response : //api.mybusiness.com . In this case, Ocelot base URL should https://api.mybusiness.com .

If you are using container and requires Ocelot in http://123.12.1.1:6543 response to a client,  then you can do this, but if you want to deploy multiple Ocelot, you may want to pass on some type of command line it script. You want to use any scheduling program are available through IP.

program

Then in your Program.cs, you will need the following. The main points to note are AddOcelot () (ocelot added services), UseOcelot (). Wait () (Ocelot set all the middleware).

   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 installation package

Use nuget installation Ocelot and its dependencies. You need to create a netcoreapp1.0 + projct and pack into them. Then follow the "start" and "  Configuration" section up and running. Please note that you need to select a package from NuGet Feed the Ocelot.

All versions can be here found .

Configuration

The following is a very basic ocelot.json. It does not do anything, but let Ocelot should begin.

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

program

Then in your Program.cs, you will need the following.

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

start up

Json configuration example uses a start file is shown below.

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

It is almost all you need to get started.

Guess you like

Origin www.cnblogs.com/letyouknowdotnet/p/11019705.html