1. brief introduction
Ocelot It was originally designed to be associated with .NET Core Used together , It's a .NET API gateway , As usage oriented .NET Run microservices / Service oriented architecture requires a unified system entry point , That is, when the client （Web Site , mobile phone APP） Wait for a visit Web API When ,Ocelot As a unified entry point, it will be distributed to the corresponding API Go to the website （ Addressing ）. and Ocelot It also integrates a lot of functions , Example routing , authentication , to grant authorization , Speed limit and other function points ,Ocelot The official website also suggests that authentication should be followed by authentication （IdentityServer4） Use it together , Easy integration of bearer token . You can go to the official website for details （https://ocelot.readthedocs.io/en/latest/introduction/bigpicture.html） Get to know .
And look at Ocelot Source code , We'll see Ocelot It's a bunch of Middleware in a specific order （Middleware） The pipes that make up .
Ocelot take HttpRequest Object to the state specified by its configuration , Until it reaches the request builder middleware , In middleware, it creates a HttpRequestMessage object , This object is used to make requests to downstream Services . The middleware that makes the request is Ocelot The last thing in the pipeline . It doesn't call the next middleware . Responses from downstream services are stored in the repository for each request scope , And ask to return Ocelot When the pipeline is retrieved . There is a piece of middleware that will HttpResponseMessage Mapping to HttpResponse object , And then it's returned to the client .
2.Ocelot To configure
According to the official website ,Ocelot There are five configurations ：
2.1 Basic integration （Basic Implementation）
When a client visits a downstream service site , Will be unified through Ocelot gateway ,Ocelot gateway Host The host will first read configuration.json Configuration information , According to the configuration file to find the corresponding downstream service site and return the processing results to the client . This process can be called routing addressing .
2.2 Integrate IdentityServer（With IdentityServer）
When the service site involves authentication and Authorization , It can be done by Ocelot Gateway integration IdentityServer, When a client visits a downstream service site , Will pass first IdentityServer After authentication and authorization, they are distributed to downstream service sites .
2.3 Multiple gateway instance clusters （Multiple Instances）
Single Ocelot Gateway is more dangerous , If this gateway goes down , All downstream service sites will be inaccessible , It is impossible to achieve high availability in this way . To solve this problem , You can deploy multiple Ocelot Gateway cluster , and Ocelot Also integrated with load balancer .
2.4 Integrate Consul Service discovery （With Consul）
Check the official website document load balancing column , We know Ocelot Already supports simple load functions , When there are multiple service nodes in the downstream site ,Ocelot Able to take on the role of load balancing . But it doesn't provide health checks , The registration of the service can only be done by manually adding it to the configuration file . It's not flexible enough and, to a certain extent, risky . At this time, we can use Consul To do service discovery , It can be associated with Ocelot Perfect combination .
2.5 Integrate Service Fabric（With Service Fabric）
If you are in Service Fabric Central government has a service , The naming service is usually used to access them .
Ocelot Gateway is the only access to the outside of the system , It's like the company's doorman is responsible for addressing 、 Access restrictions 、 security check 、 Location guidance and other functions . It also provides routing , Authentication 、 monitor 、 Load balancing 、 cache 、 Request fragmentation and management 、 Static response processing and other functions .Ocelot The core point of the gateway is , All clients and consumers access microservices through a unified gateway , All non business functions are handled at the gateway layer . Usually the gateway also provides REST/HTTP The interview of API, The server registers and manages services through the gateway . After that chapter , I will continue to base on GitHub Contributors to open source projects Ocelot Demo This paper introduces its functions with examples .Ocelot Demo Address https://github.com/catcherwong-archive/APIGatewayDemo.
Ocelot Official website