What does ocelot do? If it is an interview, the interviewer must ask, please say your understanding of ocelot, or simply talk about ocelot;
Ocelot Download: https://gitee.com/mirrors/ocelot-gateway
1. Ocelot is a NET Core and open source API gateway, which is powerful and powerful... Haha, anyway, if it's an interview, just blow it, but don't exaggerate, ocelot
It includes: routing function, request aggregation, service discovery, authentication, authentication, current limiting fuse, and built-in load balancer integrated with Service Fabric and Butterfly Tracing. These functions only need simple configuration;
2. In short, Ocelot is ASP Net core middleware. After receiving the request, it will use a request builder to construct an HttpRequestMessage and send it to the downstream server. After the downstream service returns the response, a middleware will map the returned HttpResponseMessage to the HttpResponse, which is equivalent to reverse proxy;
If you have many APIs, you can use this gateway as the only entry. One domain name and multiple URL s are enough. Of course, if there is a large amount of traffic, you must use a cluster, because this is the only entry. All requests pass through here. If it is a large project, a separate cluster for different microservices is made, which is also different from nginx. Multiple clusters are used for service management;
His comparison with Nginx:
[common ground of Ocelot and Nginx]
Reverse proxy, address forwarding
load balancing
[differences]
Nginx consists of a cluster. The main advantages are high performance, high throughput and safety
Ocelot consists of multiple clusters. The essence is to do routing (multi group address forwarding) and realize service governance
Let's start to implement a small cluster.
First create three netcore web sites or three APIs. Change the content. This is app1/2/3
After creation, start. Go to the following directory cmd of the three services respectively, and then start with the command:
dotnet run --urls="http://*:5051" /port=5051 dotnet run --urls="http://*:5052" /port=5052 dotnet run --urls="http://*:5053" /port=5053
Start creating gateway:
Create an api project
Then kill all the pipes in Startup, and Nuget adds the Ocelot reference
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Ocelot.DependencyInjection; using Ocelot.Middleware; using Ocelot.Provider.Consul; using Ocelot.Provider.Polly; namespace KH.Fz.GetWay { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOcelot(); //.AddConsul().AddPolly(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseOcelot().Wait(); } } }
Then add the profile
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, builder) => { builder.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath).AddJsonFile("Ocelot.json"); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
Configuration of configuration file: the high version and low version of Routes are written differently. If the old version is added with Re, it will not be displayed. This is a pit:
{ "Routes": [ { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5053 } ], "UpstreamPathTemplate": "/v3/{url}", "UpstreamHttpMethod": [ "Get", "Post" ], "LoadBalancerOptions": { "Type": "RoundRobin" //Polling and idle allocation https://www.freesion.com/article/53741280560/ } }, { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5052 } ], "UpstreamPathTemplate": "/v2/{url}", "UpstreamHttpMethod": [ "Get", "Post" ], "LoadBalancerOptions": { "Type": "RoundRobin" //Polling and idle allocation https://www.freesion.com/article/53741280560/ } }, { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5051 } ], "UpstreamPathTemplate": "/v1/{url}", "UpstreamHttpMethod": [ "Get", "Post" ], "LoadBalancerOptions": { "Type": "RoundRobin" //Polling and idle allocation } } ], // Global configuration. The configuration of this node will override Routes. You can set some general configurations here "GlobalConfiguration": { "ReRouteIsCaseSensitive": false // Is the route case sensitive } }
{ "Routes": [ { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5053 }, { "Host": "localhost", "Port": 5052 }, { "Host": "localhost", "Port": 5051 }, ], "UpstreamPathTemplate": "/v3/{url}", "UpstreamHttpMethod": [ "Get", "Post" ], "LoadBalancerOptions": { "Type": "RoundRobin" //Polling and idle allocation https://www.freesion.com/article/53741280560/ } } }
The above are two configuration methods. Please pay attention. If the service is the same and the load is the same, then use the second one.
After this, you can run