At Microsoft Tech 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 on ASP.NET
Core Build scalable high-performance enterprise class API gateway , Open source API gateway Ocelot Based on their own business characteristics , Only 40 minute , There's a lot of content that hasn't been expanded , Let's talk about it in a small article Ocelot
Implementation principle of , You can also contribute together in the process of using . In general, this is a ASP.NET Core
Advanced programming content , Before, I have sent many articles written by friends in the official account. , It will be quoted in this article today , Let you further study .


<http://images2017.cnblogs.com/blog/510/201711/510-20171105131518826-977325890.png>

 
<http://images2017.cnblogs.com/blog/510/201711/510-20171105130725716-1267734898.png>

I am here github Address on https://github.com/geffzhang <https://github.com/geffzhang>
Welcome to each other ,Ocelot stay github Address of https://github.com/TomPallister/Ocelot
<https://github.com/TomPallister/Ocelot> , The awesome thing was done on the day of the class. .NET Core 2.0 Upgrade of , See the upgrade process
https://github.com/TomPallister/Ocelot/issues/114
<https://github.com/TomPallister/Ocelot/issues/114>
. It took me half an hour to get my other one POC project Nanofabric  https://github.com/geffzhang/NanoFabric
<https://github.com/geffzhang/NanoFabric> Upgrade to .NET Core 2.0,
this POC The project is also the prototype of my shared project , You can say that .NET Core 2.0 8 Officially released in , after 3
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 .


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130725716-1267734898.png>


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130726716-1916329001.png>


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 is linux and windows Mixed environment of , Our goal is to unify in the company Linux 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 .


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130727732-217071007.png>


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130728357-877588407.png>

Here's a list Ocelot Features currently supported :

* Routing
* Users can specify the mapping between upstream requests , And forward it to different downstream services URL.
* 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 use IdentityServer Host tags authenticate your users .
* Authorisation using Claims
* If using bearer tokens, have access to claims Tag specific endpoints It's 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
* Caching
* Logging
* Custom Middleware
Refer to the document for more details https://github.com/TomPallister/Ocelot/wiki
<https://github.com/TomPallister/Ocelot/wiki> 

It's about Ocelot Functional characteristics of , Let's move on to introduction Ocelot Analysis of the realization principle of , The core is ASP.NET Core Middleware as well as
ASP.NET Core DependencyInjection:


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130728982-392358323.png>

ASP.NET Core conventional ASP.NET Great improvement in architecture , More modularity , The figure below illustrates the difference between them ,Application and
Middleware It's equal , such as ASP.NET Core
MVC It's also a Middleware, adopt Middleware This structure makes it very easy for us to extend our applications .


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130729623-990667817.png>

Ocelot Is to use Middleware To complete all functions of the gateway , Every little function is one Middleware, See the code for details
https://github.com/TomPallister/Ocelot/blob/develop/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs

<https://github.com/TomPallister/Ocelot/blob/develop/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs>
,Ocelot How to put each Middleware String together to finish one API Function of gateway . asp.net core
Very clever design , hold Middleware Abstraction becomes a delegation RequestDelegate, ASP.NET Core Each of Request Every registered
Middleware,Response And one by one , Process each packet in a first in, second out way :


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130730248-231827402.png>

Specific content reference : ASP.NET Core HTTP Those things in the pipeline
<http://www.cnblogs.com/savorboard/p/aspnetcore-http-pipeline.html> and
How to build one from scratch in one second ASP.NET Core middleware
<http://www.cnblogs.com/savorboard/p/generator-aspnetcore-middleware.html>,
We are Middleware In the process of programming HttpContext And the registrants and builders of the pipeline ApplicationBuilder.

 

ASP.NET Core A lot of DI (Dependency Injection)
Design , Again, we are Ocelot A large number of DI Design , See the source code for details
https://github.com/TomPallister/Ocelot/blob/develop/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs

<https://github.com/TomPallister/Ocelot/blob/develop/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs>
 

register Service There are three ways :

* Transient  At each injection , All over again new A new entity .
* Scoped    each Request All over again new A new entity .
* Singleton After the program starts new One entity . That is to say, there will only be one entity during operation .
Here's a picture from https://blog.johnwu.cc/article/asp-net-core-dependency-injection.html
<https://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 all Ocelot Fundamentals of development , at present Ocelot from 17 individual Middleware
To complete , In each Middleware 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 extension API Interface verification


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130730873-568075011.png>

Ocelot Default support based on IdentityServer4 Certification of , Custom authentication required , You can refer to
https://github.com/TomPallister/Ocelot/pull/110
<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


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130731576-746237648.png>

Ocelot
Default support Http Communications for , There are many old services in our actual projects RPC call , It's private Relay Communication framework , stay API Protocol conversion is required on the gateway , Automatically Http Request to Relay Of tcp communication .

3, Custom management console


<http://images2017.cnblogs.com/blog/510/201711/510-20171105130732263-800617670.png>

ocelot Managed API, Can be based on Management API Make a custom management console ,github There's a
https://github.com/dbarkwell/Ocelot.ConfigEditor
<https://github.com/dbarkwell/Ocelot.ConfigEditor>, This project has been realized asp.net core mvc
Edit route Online .