In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to install and configure .NET Core ocelot". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to install and configure .NET Core ocelot".
Ocelot is used as the Api gateway in this paper.
Environmental requirements:
Vs2019
.NetCore3.1
Ocelot16.0.1
Create a product service Api site (AAStore.ProductCatalog.Api)
Add a ProductController
[Route ("api/ [controller]")]
[ApiController]
Public class ProductController: ControllerBase
{
[HttpGet (template: "Get")]
Public string GetProductById ()
{
Return "Product service"
}
}
Run browsing
Then create an order service Api site (AAStore.Orde.Api)
Add an OrderController
[Route ("api/ [controller]")]
[ApiController]
Public class OrderController: ControllerBase
{
[HttpGet (template: "Get")]
Public string GetOrder ()
{
Return "Order Service"
}
}
Run browsing
Two services are ready, and finally a gateway site (AAStore.WebApiGateway) is created.
Install Ocelot
Create a json profile (ocelot.json)
{
"Routes": [
{
"DownstreamPathTemplate": "/ api/Product/get"
"DownstreamScheme": "http"
"DownstreamHostAndPorts": [
{
"Host": "localhost"
"Port": 8081
}
]
"UpstreamPathTemplate": "/ api/Product/ {everything}"
"UpstreamHttpMethod": ["Get"]
}
{
"DownstreamPathTemplate": "/ api/Order/get"
"DownstreamScheme": "http"
"DownstreamHostAndPorts": [
{
"Host": "localhost"
"Port": 8082
}
]
"UpstreamPathTemplate": "/ api/Order/get"
"UpstreamHttpMethod": ["Get"]
}
]
}
The main function of the ocelot api gateway is to receive incoming HTTP requests and forward them to downstream services, currently as a HTTP request. Ocelot describes the route from one request to another as Routes.
DownstreamPathTemplate, Scheme, and DownstreamHostAndPorts constitute the internal microservice URL to forward this request to.
The port is the internal port used by the service. When using a container, specify the port in its dockerfile. Host is a service name, depending on the service name resolution used. When using docker-compose, the service name is provided by the Docker host, which uses the service name provided in the docker-compose file. If you use a business process coordinator such as Kubernetes or Service Fabric, the name should be resolved through the DNS or name resolution provided by each business process coordinator.
DownstreamHostAndPorts is an array of hosts and ports of any downstream service to which the request is forwarded. Usually this contains only one entry, but sometimes you may want to load the balancing request to the downstream service, and you can add multiple entries through Ocelot, and then select the load balancer. But if you use Azure and any business process coordinator, it may be better to load balance through the cloud and business process coordinator infrastructure.
A UpstreamPathTemplate is a DownstreamPathTemplate that an URL,Ocelot uses to identify a given request in the client. Finally, UpstreamHttpMethod is used, so Ocelot can distinguish between different requests for the same URL (GET, POST, PUT).
Note: the configuration node after the ocelot16.x version is written as Routes, not ReRoutes, otherwise it will Failed to mat ch Route configuration for upstream path.
Provide ocelot.json files, add Ocelot services (AddOcelot) and add ocelot middleware (UseOcelot) to the generator through the AddJsonFile method in Program.cs. ConfigureWebHostDefaults (webBuilder = >
{
WebBuilder.ConfigureAppConfiguration ((hostingContext, config) = >
{
Config
.SetBasePath (hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile ("ocelot.json")
.AddenvironmentVariables ()
})
.ConfigureServices (services = >
{
Services.AddOcelot ()
Services.AddHttpContextAccessor ()
})
.configure (app = >
{
App.UseOcelot () .Wait ()
});
});
Then run the gateway to access the products and order micro-services through the gateway:
If you are lucky, you can also run successfully by doing it step by step. Of course, ocelot also has many functions such as: routing, request aggregation, service discovery, WebSockets, authentication, authorization, LB, K8S, current limit, circuit breaker and so on.
Thank you for reading, the above is the content of "how to install and configure .NET Core ocelot". After the study of this article, I believe you have a deeper understanding of how to install and configure .NET Core ocelot, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.