A Preliminary Study on ASP.NET Core Api Gateway Ocelot

A Preliminary Study on ASP.NET Core Api Gateway Ocelot

[[387094]]

This article is reprinted from the WeChat public account "UP Technology Control", the author is conan5566. Please contact the UP Technology Control public account for reprinting this article.

Overview

Ocelot is aimed at people running microservices/service-oriented architectures with .NET that need to have a unified entry point into their systems. In particular I wanted to easily integrate with IdentityServer references and bearer tokens. Ocelot is a bunch of middleware in a specific order. Ocelot manipulates an HttpRequest object into a state specified by its configuration until it reaches the request builder middleware where it creates an HttpRequestMessage object that is used to make a request to a downstream service. The middleware that makes the request is the last thing in the Ocelot pipeline. It does not call the next middleware. There is a piece of middleware that maps an HttpResponseMessage to an HttpResponse object and returns it to the client. Basically, it has a bunch of other functionality.

Code Implementation

1. Create a new API client 1

2. Create a new API gateway test

3. Install Ocelot using nuget

4. Add ConfigureAppConfiguration to the Program file

  1. public class Program
  2. {
  3. public   static void Main(string[] args)
  4. {
  5. CreateHostBuilder(args).Build().Run();
  6. }
  7.  
  8. public   static IHostBuilder CreateHostBuilder(string[] args) =>
  9. Host.CreateDefaultBuilder(args)
  10. .ConfigureAppConfiguration(conf =>
  11. {
  12. conf.AddJsonFile( "ocelot.json" , false , true );
  13. })
  14. .ConfigureWebHostDefaults(webBuilder =>
  15. {
  16. webBuilder.UseStartup<Startup>();
  17. });
  18. }

5. Startup file configuration

  1. services.AddOcelot(Configuration);
  2.  
  3. app.UseOcelot().Wait();

6. Add the file ocelot.json to the gateway project

  1. {
  2. "ReRoutes" : [
  3. {
  4. "DownstreamPathTemplate" : "/api/WeatherForecast/GetList" ,
  5. "DownstreamScheme" : "http" ,
  6. "DownstreamHostAndPorts" : [
  7. {
  8. "Host" : "localhost" ,
  9. "Port" : 5000
  10. }
  11. ],
  12. "UpstreamPathTemplate" : "/GetList" ,
  13. "UpstreamHttpMethod" : [ "Get" ]
  14. },
  15.  
  16. {
  17. "DownstreamPathTemplate" : "/{everything}" ,
  18. "DownstreamScheme" : "http" ,
  19. "DownstreamHostAndPorts" : [
  20. {
  21. "Host" : "localhost" ,
  22. "Port" : 5000
  23. }
  24. ],
  25. "UpstreamPathTemplate" : "/{everything}" ,
  26. "UpstreamHttpMethod" : [ "Post" ]
  27. },
  28. {
  29. "DownstreamPathTemplate" : "/api/WeatherForecast/GetModel?id={s1}" ,
  30. "DownstreamScheme" : "http" ,
  31. "DownstreamHostAndPorts" : [
  32. {
  33. "Host" : "localhost" ,
  34. "Port" : 5000
  35. }
  36. ],
  37. "UpstreamPathTemplate" : "/GetModel?id={s1}" ,
  38. "UpstreamHttpMethod" : [ "Get" ]
  39. }
  40. ]
  41. }

7. Run and test 2 projects

Code address

https://gitee.com/conanOpenSource_admin/Example/commit/b3b5a6b15a060b46c5ecd2ea31f0d36791cda18c

<<:  This year's 5G mobile phones must have these features!

>>:  US operators confirm that only premium users can enjoy C-band 5G signals

Recommend

What exactly is the “computing power network”?

What is a “computing network”? Let’s get straight...

How to Develop an Effective Data Center Management Services Plan

Any organization that uses UPS power supplies hop...

The Role of Machine Learning and AIOps in Network Performance Management

Before the pandemic brought a shift in business o...

Analysis: Which businesses need a dedicated wireless network?

Over the past few years, private wireless network...

Interesting explanation of bearer: PTN and IPRAN in one article

The "old-fashioned" old boss—SDH The tr...

T-Mobile and Sprint to merge

Early morning news on February 11, 2020, accordin...

ENOs and Private LTE: Intelligent Connectivity for Smart Factories

Manufacturing processes and operations are underg...

The future is here: Will 5G users reach 2.6 billion by 2025?

This article is reproduced from the public accoun...

IDC: Edge management services market expected to explode

As enterprises seek greater process efficiency an...