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

Forcepoint releases 2017 cybersecurity predictions

[[179053]] Forcepoint , a global cybersecurity le...

...

Let's talk about IPv4 to IPv6 tunnel

[[273990]] When is IPv6 tunneling used? Connect t...

VULTR Launches Free VPS Plan

VULTR released information about the launch of a ...

Application research on intercepting web crawler traffic technology

1. Headers verification The essence of a web craw...

Why are unlimited data plans dying?

Regardless of whether it was a unified arrangemen...

10 hottest enterprise networking startups in 2019

Cloud computing, automation, and intent-based net...

The rewards and risks facing retailers in a 5G world

The rollout of 5G is expected to have a significa...

Where is the future in the post-5G era?

According to the latest news from the 3GPP offici...

Http code: What does 304 mean? How much do you know?

picture 1. http code 304 Not Modified The HTTP st...