DEV Community

Danilo O. Pinheiro, dopme.io
Danilo O. Pinheiro, dopme.io

Posted on

Integração do Kong com Aplicações .NET Modernas

Nos últimos anos, a arquitetura baseada em microserviços tornou-se um padrão para sistemas escaláveis e distribuídos. Com isso, a necessidade de um API Gateway se tornou essencial. O Kong, uma solução open-source robusta e extensível, tem se destacado como uma das melhores opções.

Neste artigo, vamos mostrar como utilizar o Kong API Gateway com uma aplicação .NET 8, fazendo roteamento, controle de acesso e análise de chamadas de API — com a interface web Konga, agora usando a imagem estável spotx/konga:version-1.1.1.


🔍 O Que é o Kong?

O Kong é um API Gateway moderno construído sobre o NGINX, com uma arquitetura altamente extensível. Ele oferece suporte a autenticação, rate limiting, logging, transformações de request/response, e muito mais — tudo através de plugins reutilizáveis e configuráveis via API ou interface visual.


📦 Tecnologias Utilizadas

  • .NET 8 (Web API)
  • Kong Gateway (v3.x)
  • Docker e Docker Compose
  • PostgreSQL
  • Konga (interface web via spotx/konga)
  • Postman ou Curl

🧱 Estrutura do Projeto

kong-dotnet-demo/
├── docker-compose.yml
├── Services/
│   ├── catalog-api/
│   └── user-api/
Enter fullscreen mode Exit fullscreen mode

🐳 docker-compose.yml com Kong + Konga Atualizado

version: '3.8'

services:

  kong-database:
    image: postgres:13
    container_name: kong-database
    environment:
      POSTGRES_USER: kong
      POSTGRES_DB: kong
      POSTGRES_PASSWORD: kong
    ports:
      - "5432:5432"

  kong:
    image: kong:3.6
    container_name: kong
    depends_on:
      - kong-database
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-database
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: kong
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
      KONG_PROXY_ERROR_LOG: /dev/stderr
      KONG_ADMIN_ERROR_LOG: /dev/stderr
      KONG_ADMIN_LISTEN: 0.0.0.0:8001
    ports:
      - "8000:8000"
      - "8001:8001"

  konga:
    image: spotx/konga:version-1.1.1
    container_name: konga
    restart: always
    environment:
      NODE_ENV: development
      DB_ADAPTER: postgres
      DB_HOST: kong-database
      DB_PORT: 5432
      DB_USER: kong
      DB_PASSWORD: kong
      DB_DATABASE: kong
    ports:
      - "1337:1337"
    depends_on:
      - kong-database
Enter fullscreen mode Exit fullscreen mode

🚀 Criando API .NET Simples

CatalogApi.csproj (Minimal API)

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/catalog", () => new[] {
    new { Id = 1, Nome = "Produto A" },
    new { Id = 2, Nome = "Produto B" }
});

app.Run();
Enter fullscreen mode Exit fullscreen mode

Execute com:

dotnet run --project Services/catalog-api
Enter fullscreen mode Exit fullscreen mode

🔌 Registrando a API no Kong (via Admin API)

curl -i -X POST http://localhost:8001/services \
  --data name=catalog-api \
  --data url=http://host.docker.internal:5100

curl -i -X POST http://localhost:8001/services/catalog-api/routes \
  --data paths[]=/catalog
Enter fullscreen mode Exit fullscreen mode

Teste:

http://localhost:8000/catalog
Enter fullscreen mode Exit fullscreen mode

🔐 Habilitando Plugins (Key Authentication)

curl -i -X POST http://localhost:8001/services/catalog-api/plugins \
  --data name=key-auth

curl -i -X POST http://localhost:8001/consumers \
  --data username=devsfree

curl -i -X POST http://localhost:8001/consumers/devsfree/key-auth
Enter fullscreen mode Exit fullscreen mode

Use a chave retornada:

GET /catalog HTTP/1.1
Host: localhost:8000
apikey: SUA_CHAVE_AQUI
Enter fullscreen mode Exit fullscreen mode

🧠 Benefícios do Kong com .NET

Recurso Benefício
Proxy reverso Roteamento flexível
Plugins (Auth, Rate) Controle centralizado
Admin API Infra como código
Interface Web (Konga) Configuração rápida
Escalabilidade horizontal Ideal para microsserviços

🧪 Interface Web (Konga)


🤝 Conecte-se Comigo

Estou sempre aberto a trocar ideias e aprender com a comunidade. Se você também está explorando o C# 13, .NET 8, Kong ou arquiteturas distribuídas, vamos nos conectar:

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.