美文网首页kong
API网关企业应用实战 - Kong最佳实践

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

作者: DreamsonMa | 来源:发表于2019-11-01 09:26 被阅读0次

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

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

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

Then,get straight to the point!

湿定制控制台

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

API GATEWAY

网关基本使用

1、定义SERVICE

这里定义了三个SERVICE。分别介绍一下:

  • backend-mall:基于后端的应用的请求转发
  • saas-frontend-mall:基于前端应用的请求转发
  • orderAdd:基于前端应用某个接口的请求转发
定义SERVICE

下面先看下backend-mall的配置(其他类似):
接收到上流的请求,转发到 http://172.16.6.131:8080/backend-mall 上面。

backend-mall配置

2、定义ROUTE

这里针对每个SERVICE,分别定义对应的ROUTE。

  • 路由backend-mall对应服务backend-mall
  • 路由saas-frontend-mall对应服务saas-frontend-mall
  • 路由orderAdd对应服务orderAdd
ROUTE列表

这里介绍一下 backend-mall 对应的ROUTE:请求路径匹配到 /backend-mall,就会转到对应SERVICE配置的转发地址。

backend-route

3、定义UPSTREAM

UPSTREAM主要是用来负载均衡(分流),需要定义对应的TARGET,最后在SERVICE的host上配置上UPSTREAM即可。

定义UPSTREAM

我们这里只定义另一个目标。请求经过这个UPSTREAM就会根据权重轮训到对应的目标地址。

定义TARGET

网关高级用法

1、API限流

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

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

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

限流配置

2、使用Prometheus+Grafana监控

Konga提供的控制台监控没有一个持续性,可以把监控数据吐到Prometheus,然后用Grafana展示处理。

为saas-frontend-mall添加一个prometheus监控插件。

Kong监控

检查一下监控数据是否正常。

吐出监控数据

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.

监控面板

相关文章

网友评论

    本文标题:API网关企业应用实战 - Kong最佳实践

    本文链接:https://www.haomeiwen.com/subject/vxcnectx.html