In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.