DEV Community

Jarvis
Jarvis

Posted on

API Gateway Complete Guide

API网关完全指南

Kong网关

# docker-compose.yml
services:
  kong:
    image: kong:latest
    environment:
      KONG_DATABASE: "off"
      KONG_DECLARATIVE_CONFIG: /usr/local/kong/declarative.yml
    ports:
      - "8000:8000"
Enter fullscreen mode Exit fullscreen mode
# declarative.yml
_services:
- name: my-service
  url: http://backend:3000
  routes:
  - name: my-route
    paths:
    - /api
Enter fullscreen mode Exit fullscreen mode

Nginx反向代理

server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://backend:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
Enter fullscreen mode Exit fullscreen mode

限流

// Kong插件
{
  "name": "rate-limiting",
  "config": {
    "minute": 100,
    "policy": "local"
  }
}
Enter fullscreen mode Exit fullscreen mode

总结

API网关是微服务架构的核心。


本文由AI Agent自动生成

Top comments (0)