Ocelot gateway,

Source: Internet
Author: User

Ocelot gateway,

Ocelot is. the open-source project of the gateway under the net core framework is the basic implementation diagram provided by the official team, that is, to unify multiple backend services to the gateway, front-end applications: desktop, web, only the Access Gateway can be used on the app side.

 

The implementation principle of Ocelot is to set the client's Request to the gateway according to configuration. the json ing configuration is forwarded to the corresponding backend http service, and then the Response is obtained from the backend http service, and then returned to the client. Of course, with the gateway, we can perform unified verification on the gateway layer, or perform unified monitoring on the gateway.

Next, let's make a Demo.

Create three new asp.net core web aip projects:

OcelotGateway Gateway project, Port: 5000

DemoAAPI Project A, port 5001

DemoBAPI Project B, port 5002

(Note: The port can be in the launchSettings under the Properties of each project. modified In json. The published port can be in Program. use UseUrls ("http: // *: 5000") in cs to modify it)

For OcelotGateway:

REFERENCE The Ocelot Nuget package:

View-> other Windows-> Package Management Console: Install-Package Ocelot

Or right-click the project and choose "manage Nuget packages" from the shortcut menu to find Ocelot for installation.

In the OcelotGateway project, add a configuration. json file to "Copy to output directory" and set it to "always copy". The content is as follows:

{  "ReRoutes": [    {      "DownstreamPathTemplate": "/demoaapi/values",      "DownstreamScheme": "http",      "DownstreamPort": 5001,      "DownstreamHost": "localhost",      "UpstreamPathTemplate": "/demoaapi/values",      "UpstreamHttpMethod": [ "Get" ],      "QoSOptions": {        "ExceptionsAllowedBeforeBreaking": 3,        "DurationOfBreak": 10,        "TimeoutValue": 5000      },      "HttpHandlerOptions": {        "AllowAutoRedirect": false,        "UseCookieContainer": false      },      "AuthenticationOptions": {        "AuthenticationProviderKey": "",        "AllowedScopes": []      }    },    {      "DownstreamPathTemplate": "/demobapi/values",      "DownstreamScheme": "http",      "DownstreamPort": 5002,      "DownstreamHost": "localhost",      "UpstreamPathTemplate": "/demobapi/values",      "UpstreamHttpMethod": [ "Get" ],      "QoSOptions": {        "ExceptionsAllowedBeforeBreaking": 3,        "DurationOfBreak": 10,        "TimeoutValue": 5000      },      "HttpHandlerOptions": {        "AllowAutoRedirect": false,        "UseCookieContainer": false      },      "AuthenticationOptions": {        "AuthenticationProviderKey": "",        "AllowedScopes": []      }    }  ]}

Next we will rebuild the Program. cs of OcelotGateway.

1 using Microsoft. aspNetCore. hosting; 2 3 using Microsoft. extensions. configuration; 4 5 using Microsoft. extensions. dependencyInjection; 6 7 8 9 namespace OcelotGateway10 11 {12 13 public class Program14 15 {16 17 public static void Main (string [] args) 18 19 {20 21 BuildWebHost (args ). run (); 22 23} 24 25 public static IWebHost BuildWebHost (string [] args) 26 27 {28 29 IWebHostBuilder builder = new WebHostBuilder (); 30 31 // inject WebHostBuilder32 33 return builder. configureServices (service => 34 35 {36 37 service. addSingleton (builder); 38 39}) 40 41 // load configuration 42. configureAppConfiguration (conbuilder => 44 45 {46 47 conbuilder. addJsonFile ("configuration. json "); 48 49}) 50 51. useKestrel () 52 53. useUrls ("http: // *: 5000") 54 55. useStartup <Startup> () 56 57. build (); 58 59} 60 61} 62 63}View Code

Modify Startup. cs

1 using Microsoft. aspNetCore. builder; 2 3 using Microsoft. aspNetCore. hosting; 4 5 using Microsoft. extensions. configuration; 6 7 using Microsoft. extensions. dependencyInjection; 8 9 using Ocelot. dependencyInjection; 10 11 using Ocelot. middleware; 12 13 14 15 namespace OcelotGateway16 17 {18 19 public class Startup20 21 {22 23 public Startup (IConfiguration configuration) 24 25 {26 27 Configuration = configuration; 28 29} 30 31 public IConfiguration Configuration {get;} 32 33 public void ConfigureServices (IServiceCollection services) 34 35 {36 37 // inject the Configuration file. AddOcelot requires the parameter to be of the IConfigurationRoot type, so we need to make a conversion of 38 39 services. addOcelot (Configuration as ConfigurationRoot); 40 41} 42 43 public void Configure (IApplicationBuilder app, IHostingEnvironment env) 44 45 {46 47 // Add middleware 48 49 app. useOcelot (). wait (); 50 51} 52 53} 54 55}View Code

To make the test data look good, let's modify the ValuesController of the DemoAAPI project and DemoBAPI project:

1 [Route ("demoaapi/[controller]")] 2 3 public class ValuesController: Controller 4 5 {6 7 [HttpGet] 8 9 public IEnumerable <string> Get () 10 11 {12 13 return new string [] {"DemoA service", "request"}; 14 15} 16 17 //...... 18 19} 20 21 22 23 [Route ("demobapi/[controller]")] 24 25 public class ValuesController: controller26 27 {28 29 [HttpGet] 30 31 public IEnumerable <string> Get () 32 33 {34 35 return new string [] {"DemoB service", "request "}; 36 37} 38 39 //...... 40 41}View Code

Finally, in Solution Properties> multiple startup projects, set DemoAAPI, DemoBAPI, and OcelotGateway to start and start the solution. The effect is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.