Ocelot is an open-source API gateway implemented in .NET Core, which is powerful and includes routing, request aggregation, service discovery, authentication, authentication, current limiting circuit breaker, and built-in load balancer integration with Service Fabric and Butterfly Tracing. These functions only need to be simple to complete, and we will explain the configuration of these functions one by one.
Of course, Java projects also have gateways, Spring Cloud Gateway is Spring Cloud's new gateway framework, previously Netflix Zuul.
First, create an empty project ASP.NET Core 3.1 with the name: dotnet-gateway-demo
nuget installs ocelot with the following command:
Create two new asp.net Core 3.1 API projects, named web-api-demo1 and web-api-demo2, respectively. The solution structure is as follows:
Modify the Get method of the WeatherForecastController controller of the "web-api-demo1" and "web-api-demo2" projects with the following code:
demo1 link: http://localhost:7853/weatherforecast Demo2 link: http://localhost:7863/weatherforecast
In the root directory of the dotnet-gateway-demo project, create a new configuration file "ocelot.json", as follows:
where the BaseUrl address is filled in with the launch URL of the current project,Fill in according to your actual situation。
- DownstreamPathTemplate: The relative path of the downstream service
- DownstreamScheme: The downstream service http schema
- DownstreamHostAndPorts: The address of the downstream service, if you use LoadBalancer, you can fill in multiple fields here
- UpstreamPathTemplate: 上游也就是用户输入的请求Url模板
- UpstreamHttpMethod: 上游请求http方法,可使用数组
Join in Program.cs's CreateHostBuilder
Edit the Startup.cs file with the following code:
Its main codes are:
Set up a startup project and start 3 web projects at the same time, as shown in the following figure:
Once the setup is complete, launch and, according to our configuration rules, try to access:The hyperlink login is visible.andThe hyperlink login is visible.As shown below:
We add the following configuration to the ocelot.json file, try to reverse proxy this site, the configuration is as follows:
Try to visit:The hyperlink login is visible.
Source code download:
Tourists, if you want to see the hidden content of this post, please Reply
Reference Documentation:The hyperlink login is visible.
(End)
|