Skip to content

goish135/apisix-dotnet-docker

 
 

Repository files navigation

Manage .NET Microservices APIs with Apache APISIX API Gateway

This is an example project focus on the usage of Apache APISIX API Gateway for applications developed in ASP.NET Core (Assume that you have an API that manage products) and provide an easy example of how to deploy a multiple images using docker compose.

👉 To execute and customize the example project per your need shown in this post, here are the minimum requirements you need to install in your system:

Build a multi-container APISIX via Docker CLI

You can start the application by running docker compose command from the root folder of the project:

after modify the source code ProductApi/Controllers/ProductsController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ProductApi.Services;
using Microsoft.AspNetCore.Mvc;

namespace ProductApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductsController : ControllerBase
    {
        private IProductsService _productsService;
        public ProductsController(IProductsService productsService)
        {
            _productsService = productsService;
        }

        [HttpGet]
        public IActionResult GetAll()
        {
            throw new Exception(); // add this line !!!
            return Ok(_productsService.GetAll());
        }
    }
}
docker-compose -p docker-apisix up -d

re-produce api-breaker validate

step1 : set up route @ Terminal
curl "http://127.0.0.1:9080/apisix/admin/upstreams/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
{
 "type": "roundrobin",
 "nodes": {
 "productapi:80": 1
 }
}'
step2 : set up plugin @ Terminal
curl "http://127.0.0.1:9080/apisix/admin/routes/1" -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "name": "Route for a circuit breaker",
    "methods": ["GET"],
    "uri": "/api/products",
    "upstream_id": "1",
    "plugins": {
        "api-breaker": {
            "break_response_code": 502,
			      "max_breaker_sec": 30,
            "unhealthy": {
                "http_statuses": [500, 503],
                "failures": 3
            },
            "healthy": {
                "http_statuses": [200],
                "successes": 1
            }
        }
    }
}'
step3 : test plugin-api-breaker @ Terminal
curl http://127.0.0.1:9080/api/products -i
curl http://127.0.0.1:9080/api/products -i
curl http://127.0.0.1:9080/api/products -i
curl http://127.0.0.1:9080/api/products -i

re-produce api-breaker validate RESULT image

Sample output:

[+] Running 7/7
 - Network apisix-dotnet-docker_apisix                Created                                                              0.0s
 - Container apisix-dotnet-docker-apisix-dashboard-1  Started                                                              1.2s
 - Container apisix-dotnet-docker-prometheus-1        Started                                                              0.7s
 - Container apisix-dotnet-docker-etcd-1              Started                                                              0.9s
 - Container apisix-dotnet-docker-grafana-1           Started                                                              1.2s
 - Container apisix-dotnet-docker-productapi-1        Started                                                              0.7s
 - Container apisix-dotnet-docker-apisix-1            Started                                                              2.0s

The running container list you can see by running docker compose ps CLI command or using docker desktop:

NAME                                      COMMAND                  SERVICE             STATUS              PORTS
apisix-dotnet-docker-apisix-1             "sh -c '/usr/bin/api…"   apisix              running             0.0.0.0:9080->9080/tcp, 0.0.0.0:9091-9092->9091-9092/tcp, 0.0.0.0:9443->9443/tcp
apisix-dotnet-docker-apisix-dashboard-1   "/usr/local/apisix-d…"   apisix-dashboard    running             0.0.0.0:9000->9000/tcp
apisix-dotnet-docker-etcd-1               "/opt/bitnami/script…"   etcd                running             0.0.0.0:12379->2379/tcp
apisix-dotnet-docker-grafana-1            "/run.sh"                grafana             running             0.0.0.0:3000->3000/tcp
apisix-dotnet-docker-productapi-1         "dotnet ProductApi.d…"   productapi          running             0.0.0.0:5555->80/tcp
apisix-dotnet-docker-prometheus-1         "/bin/prometheus --c…"   prometheus          running             0.0.0.0:9090->9090/tcp

Once the containers are running, navigate to http://localhost:5555/api/products in your web browser and you will see the following output:

Product list .NET API response

You can read the blog post for more information on how to configure APISIX API Gateway for the ASP.NET API.

Releases

No releases published

Packages

No packages published

Languages

  • Shell 57.0%
  • C# 39.4%
  • Dockerfile 3.6%