Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basics of .net Core micro service gateway Ocelot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the basics of .net Core Micro Services Gateway Ocelot". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is the gateway?

Simply put, a gateway is a request portal exposed to the outside world. Just like the doorman, people outside have to pass through the doorman if they want to come in. Of course, gateways are not necessarily necessary, and back-end services can also provide services to clients through http. However, for complex and large-scale projects, the use of gateways has many unforgettable benefits, such as unified request aggregation to save traffic, reduce coupling, and give the project the ability to circuit breaker and limit current to improve availability and so on.

What is ocelot?

Ocelot is an open source api gateway project implemented by. Net core, open source address: https://github.com/ThreeMammals/Ocelot

In addition to being very suitable for .net developers, ocelot has powerful functions, including routing, authentication, request aggregation, current limit circuit breaker, service discovery, authentication, built-in load balancer, Consul integration, and so on.

Of course, the api gateway is not the only one, there are kong and so on the market, as long as you like.

Ocelot integration

First of all, it is clear that the gateway should exist as a separate process. So let's first create a new. Net core3.1 project, and then add the nuget package:

As for the version, you can choose the latest version that you can currently support.

After adding the nuget package, you need to modify the StartUp:

/ / This method gets called by the runtime. Use this method to add services to the container. Public void ConfigureServices (IServiceCollection services) {services.AddOcelot (); / / services.AddControllers ();} / / This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {app.UseOcelot (). Wait (); / / if (env.IsDevelopment ()) / / {/ / app.UseDeveloperExceptionPage (); / /} / / app.UseHttpsRedirection (); / / app.UseRouting (); / / app.UseAuthorization () / / app.UseEndpoints (endpoints = > / {/ / endpoints.MapControllers (); / /});}

Don't be surprised here, because if you take the gateway, you won't take the default pipeline again. UseOcelot (). Wait () means to set up all the middleware in ocelot, and ocelot also provides many libraries for integrating middleware, like these:

Now, for ocelot to run successfully, you need to add a new configuration file and a reference to the configuration file in Program:

Public static IHostBuilder CreateHostBuilder (string [] args) = > Host.CreateDefaultBuilder (args) .ConfigreAppConfiguration (config = > {config.AddJsonFile ("ocelotConfig.json", optional: false, reloadOnChange: true);}) .ConfigureWebHostDefaults (webBuilder = > {webBuilder.UseStartup ();})

Configuration file:

{"Routes": [{"DownstreamPathTemplate": "/ {url}", / / Service address-- url variable "DownstreamScheme": "http", "DownstreamHostAndPorts": [{"Host": "123.123.123.123", "Port": 5050 / / Service port}], "UpstreamPathTemplate": "/ MJ/ {url}" / / Gateway address-- url variable "UpstreamHttpMethod": ["Get", "Post"]}]}

This is a simple forwarding configuration. The configuration items at the beginning of Downstream and Upstream are downstream and upstream related items. To say here, in the micro-service architecture, the client-server side is usually understood as upstream-downstream, which is replaced by itself.

The above configuration file does one thing. When receiving upstream requests, the request containing [/ MJ/ all] in the request path is forwarded to IP [http://123.123.123.123:5050/ all] and the result is returned. The get and post methods of http are supported. In fact, this is the most basic routing.

To test it, start the project and write the request path:

As you can see, ocelot successfully forwards the local request to the remote server according to the routing rules and sends back the result. The most basic functions of a gateway are available.

The remote service requested in the screenshot is a project based on consul in my previous articles. If you are interested, you can take a look at it.

This is the end of the introduction to "what are the basics of .net Core Micro Services Gateway Ocelot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report