Ocelot API Analysis of the implementation of gateway
At MicrosoftTech Summit 2017 I shared a course with you at the conference《.NET Core The practice of enterprise application development in Tencent TenPay》, The focus is onASP.NET
Core Build scalable high-performance enterprise classAPI gateway, Open sourceAPI gatewayOcelot Based on their own business characteristics, Only40 Minute, There's a lot of content that hasn't been expanded, Let's talk about it in a small articleOcelot
Implementation principle of, You can also contribute together in the process of using. In general, this is aASP.NET Core
Advanced programming content, Many articles have been written by friends before in the public address. It will be quoted in this article today, Let you further study.
I am heregithub Address onhttps://github.com/geffzhang <https://github.com/geffzhang>
Welcome to each other.Ocelot staygithub Addresshttps://github.com/TomPallister/Ocelot
<https://github.com/TomPallister/Ocelot> , It was very hard to finish on the day of the class..NET Core 2.0 Upgrading, See the upgrade process
. It took me half an hour to get my other onePOC projectNanofabric https://github.com/geffzhang/NanoFabric
<https://github.com/geffzhang/NanoFabric> Upgrade to.NET Core 2.0,
thisPOC The project is also the prototype of my shared project, You can say that.NET Core 2.0 8 Officially released in, after3
Months of development, Community ecology is ready, New projects can be developed using.NET Core 2,Ocelot Is a representative of many excellent open source projects in an integrated community.
Rapid development of business, A lot of external service interfaces are generated, Distributed in all parts of the organization requires unified management, And our environment islinux andwindows Mixed environment of, Our goal is to unify in the companyLinux Environmental Science,.NET
Core about.NET It's a great technology for the technical team, And.NET Core Its architecture is very good, Better performance.
Here's a listOcelot Features currently supported:
* Users can specify the mapping between upstream requests, And forward it to different downstream servicesURL.
* Service Discovery
* Ocelot Can view your service discovery, And find the service that it should forward the downstream request. It can load balance these services..
* Authentication using IdentityServer
* You can mark an endpoint as authenticated, And useIdentityServer Host tags authenticate your users.
* Authorisation using Claims
* If used bearer tokens, have access to claims Tag specific endpoints Authorized.
* Claims Transformation
* Ocelot Provides a syntax for converting to downstream requests, And add the declaration data to the title,URL parameter, Other statements, etc
* Quality of service
* Retries, circuit breaker, timeouts etc.
* Request / Correlation Ids
* Custom Middleware
Refer to the document for more details https://github.com/TomPallister/Ocelot/wiki
It's aboutOcelot Functional characteristics of, Let's move on to introductionOcelot Analysis of the realization principle of, The core isASP.NET Core Middleware as well as
ASP.NET Core DependencyInjection：
ASP.NET Core ConventionalASP.NET Great improvement in architecture, More modularity, The figure below illustrates the difference between them,Application and
Middleware It is equal. such asASP.NET Core
MVC Also aMiddleware, adoptMiddleware This structure makes it very easy for us to extend our applications.
Ocelot Just useMiddleware To complete all functions of the gateway, Every little function is oneMiddleware, See the code for details
,Ocelot How to put eachMiddleware String together to finish oneAPI Function of gateway. asp.net core
Very clever design, holdMiddleware Abstraction becomes a delegationRequestDelegate, ASP.NET Core Each Request Every registered
Middleware,Response And one by one, Process each packet in a first in, second out way：
Specific content reference： ASP.NET Core HTTP Those things in the pipeline
How to build one from scratch in one second ASP.NET Core middleware
We areMiddleware In the process of programmingHttpContext And the registrants and builders of the pipeline ApplicationBuilder.
ASP.NET Core A lot of DI (Dependency Injection)
Design, Again, we areOcelot A large number ofDI Design, See the source code for details
register Service There are three ways：
* Transient At each injection, All re new A new entity.
* Scoped each Request All re new A new entity.
* Singleton After the program starts new An entity. That is to say, there will only be one entity during operation.
Here's a picture fromhttps://blog.johnwu.cc/article/asp-net-core-dependency-injection.html
, Vividly demonstrate the object life cycle.
* A by Singleton
* B by Scoped
* C by Transient
That's allOcelot Fundamentals of development, at presentOcelot from17 individualMiddleware
To finish, In eachMiddleware There is also a lot of business knowledge involved in the internal implementation of, This article will not be expanded first, Detailed analysis of subsequent specific articles. Let's talk about how to customize the extension, In our project, we have expanded in three aspects：
1, Custom extensionAPI Interface verification
Ocelot Default support based onIdentityServer4 Authentication, Custom authentication required, Can refer to
<https://github.com/TomPallister/Ocelot/pull/110>, Add custom validation, however.net core 2.0
The authentication part is basically rewritten.
2, Custom extended downstream communication protocol
Default supportHttp Communication, There are many old services in our actual projectsRPC call, It's privateRelay Communication framework, stayAPI Protocol conversion is required on the gateway, Automatic willHttp Request toRelay Oftcp communication.
3, Custom management console
ocelot ManagedAPI, Can be based on ManagementAPI Make a custom management console,github There is
<https://github.com/dbarkwell/Ocelot.ConfigEditor>, This project has been realizedasp.net core mvc
Edit route Online.