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

The significance of SDN deployment in developing countries

If you haven't been to Brazil, you should go ...

HTTP, TCP, IP, and Ethernet in one article

This article is reprinted from the WeChat public ...

GSMA: Global 5G connections will reach 1.8 billion by 2025

According to a new study from GSMA, global 5G con...

Juniper Networks MIST AI network solution gives network engineers "superpowers"!

[51CTO.com original article] Under the night, the...

Revolutionizing Networking with Edge Computing

Compared with cloud computing, edge computing foc...