Quickstart: Create your first application with LoadShedding
In this guide, you will use C# and the .NET CLI to create a WebApi that will have LoadShedding with the default configurations.
By the end of the guide, you will know how to use LoadShedding on your application.
Prerequisites
- One of the following .NET versions:
- .NET Core 2.0 or above.
Overview
You will create a WebApi using LoadShedding.
Steps
1. Create a folder for your applications
Create a new folder with the name LoadSheddingQuickstart.
2. Create WebApi Project
Run the following command to create a WebApi Project named LoadSheddingQuickstart:
dotnet new webapi -controllers -n LoadSheddingQuickstart
3. Install LoadShedding package
Inside the LoadSheddingQuickstart project directory, run the following command to install the required package:
dotnet add package Farfetch.LoadShedding.AspNetCore
4 Add Metrics
Inside the LoadSheddingQuickstart project directory, run the following command to install Prometheus:
dotnet add package prometheus-net.AspNetCore
dotnet add package Farfetch.LoadShedding.Prometheus
This step is optional. With this you will be able to confirm that LoadShedding is configured on the Run! step.
5. Add LoadShedding on the WebApi
Add the LoadShedding services by calling the AddLoadShedding extension:
services.AddLoadShedding();
Optionally you can configure SubscribeEvents()
and you will be able to confirm that LoadShedding is configured on the Run! step:
services.AddLoadShedding((provider, options) =>
{
options.SubscribeEvents(events =>
{
events.ItemEnqueued.Subscribe(args => Console.WriteLine($"QueueLimit: {args.QueueLimit}, QueueCount: {args.QueueCount}"));
events.ItemDequeued.Subscribe(args => Console.WriteLine($"QueueLimit: {args.QueueLimit}, QueueCount: {args.QueueCount}"));
events.ItemProcessing.Subscribe(args => Console.WriteLine($"ConcurrencyLimit: {args.ConcurrencyLimit}, ConcurrencyItems: {args.ConcurrencyCount}"));
events.ItemProcessed.Subscribe(args => Console.WriteLine($"ConcurrencyLimit: {args.ConcurrencyLimit}, ConcurrencyItems: {args.ConcurrencyCount}"));
events.Rejected.Subscribe(args => Console.Error.WriteLine($"Item rejected with Priority: {args.Priority}"));
});
});
For more information about de default configurations and possible customizations see here.
6. Use LoadShedding on the WebApi
Use the UseLoadShedding
extension method by extending the IApplicationBuilder
interface:
app.UseLoadShedding();
7. Run!
From the LoadSheddingQuickstart
directory run the project:
dotnet run --project LoadSheddingQuickstart/LoadSheddingQuickstart.csproj
And you should be able to call the available endpoint with Swagger Web Interface: https://localhost:7231/swagger/index.html
Now, you can confirm that LoadShedding is correctly configured on your WebApi by calling the following endpoint: https://localhost:7231/metrics
You should see the LoadShedding metrics referenced here.
Additionally, you can also confirm if LoadShedding is correctly configured by checking the following console output:
ConcurrencyLimit: 5, ConcurrencyItems: 1
ConcurrencyLimit: 5, ConcurrencyItems: 0