Ocelot Chinese Documentation - Getting Started

Ocelot is only available for .NET Core and is currently built for netcoreapp2.0, this documentation may help you understand if Ocelot is right for you.

.NET Core 2.0

Install NuGet packages

Install Ocelot and its dependencies using nuget. You need to create a netcoreapp2.0 project and install the Ocelot package. Then follow the configuration section below to get up and running.

Install-Package Ocelot

All versions can be found here .

configure

Below is a very simple ocelot.json. It doesn't do anything, but it can be started on Ocelot.

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

The most important thing here is BaseUrl. Ocelot needs to know the URL it's running at for head find and replace and some admin configuration. When setting this URL, it should be the external URL that Ocelot will see clients running. For example, let's say you run Ocelot in a container with the url http://123.12.1.1:6543 , but there is a proxy like nginx in front of it responding to https://api.mybusiness.com . In this case the BaseUrl of Ocelot should be https://api.mybusiness.com .

If for some reason you are using containers and you want Ocelot to respond to clients on http://123.12.1.1:6543 then you can do this, but if you are deploying multiple Ocelots you may want to kind of script to pass it on the command line. Hopefully whatever scheduler you use can pass this IP.

program

Then in your Program.cs you will want the following. The main ones are AddOcelot() (add ocelot service), UseOcelot().Wait() (set all Ocelot 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();
    }
}

.NET Core 1.0

original

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856944&siteId=291194637