API网关企业应用实战 - Kong最佳实践

讲了这么久的Kong,到底在企业中这个API网关是怎么使用的呢?今天我们就来揭开这个神秘的面纱。

首先,得部署一个or多个应用。这里留下100个省略号...

然后,部署一个Kong环境,和控制台。还不了解的,可以先瞄瞄之前的系列文章: 开源API网关系统(Kong教程)入门到精通 。继续留下100个省略号...

Then,get straight to the point!

湿定制控制台

基于Kong的API,我们会开发出一套Kong的管理平台。以此,简化Kong的操作。就是傻瓜也会用的那种。我们以Konga为标准,二开了一套。

网关高级用法

1、API限流

我们思考一个问题。上一节为什么要单独对一个接口进行配置呢?例如:做秒杀的时候,会出现一个流量峰值,此时为了保护原有系统,会独立出一个系统,做一些分流、限流的处理。这个时候,可以通过Kong对某个接口配置一个单独的API实现该功能,而不用修改原系统的通用配置。

SO,下面我们做一个限流配置。

找到orderAdd路由,添加一个rate-limiting插件。这个插件的作用:速率限制在给定的几秒、几分钟、几小时、几天、几个月或几年时间内可以发出多少HTTP请求。

AND,当然是要搭建一个Prometheus+Grafana环境啦。非主体跳过...

导入Grafana控制面板。参考: https://grafana.com/grafana/dashboards/7424
最后配上一个Prometheus数据采集:

[root@mvxl4019 prometheus-2.5.0.linux-amd64]# cat prometheus.yml
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - 172.16.6.156:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "uat_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  # nodes    
  - job_name: 'kong-172.16.6.130'
    metrics_path: /metrics
    static_configs:
    - targets: ['172.16.6.130:8001']
      labels: 
         instance: 172.16.6.130
         env: kong

The end ! let's to get the results.