网关Kong Gateway

作者: 勤_ | 来源:发表于2021-08-13 17:29 被阅读0次

    概述

    上文我们了解了JAVA技术栈的网关Shenyu,今天再来认识下基于nginx的Kong。Kong是一个在Nginx中运行的Lua应用程序,并且可以通过lua-nginx模块实现。Kong不是用这个模块编译Nginx,而是与OpenResty一起分发,OpenResty已经包含了lua-nginx-module。OpenResty不是Nginx的分支,而是一组扩展其功能的模块。

    这为可插拔架构奠定了基础,可以在运行时启用和执行Lua脚本(称为“插件”)。 因此,我们认为Kong是微服务架构的典范:它的核心是实现数据库抽象,路由和插件管理。 插件可以存在于单独的代码库中,并且可以在几行代码中注入到请求生命周期的任何位置。

    introduction.png

    依赖的框架以及版本

    工具 版本
    kong 2.5.0
    centos 7

    实现步骤

    1,下载并安装

    下载地址健参考

    root# sudo yum install epel-release
    root# mkdir -p /data/work/kong
    root# cd /data/work/kong/
    root# yum install kong-2.5.0.el7.amd64.rpm
    root# kong config init
    root# touch kong.conf
    root# cat kong.conf
    database = off
    declarative_config = /data/work/kong/kong.yml
    root# kong start kong start -c /data/work/kong/kong.conf
    

    2,配置service、routes以及验证:

    • 配置service以及routes

       .............
        services:
        - name: example-service
          url: http://192.168.3.115:18091/
          routes:
          - name: example-route
            paths:
            - /example  
        ...............
      
    • 使用postman请求验证,可以通过kong 网关访问到后台服务。如下图所示:

      image-20210813115057856.png image-20210813115133422.png

    3,启用插件以及验证

    • 启用Key Authentication,kong.yml配置如下:

      ............
      services:
      - name: example-service
        url: http://192.168.3.115:18091/
        routes:
        - name: example-route
          paths:
          - /example
        plugins:
        - name: key-auth
        
      consumers:
      - username: example-consumer-user
        keyauth_credentials:
        - key: userkey  
      .............
        
      
    • 进行验证

      image-20210813140901062.png image-20210813140906695.png

    异常报错

    1,ulimit is currently set to "1024". For better performance set it to at least "4096" using "ulimit -n"

    解决办法:临时调整:ulimit -n 65535

    2,Error: [PostgreSQL error] failed to retrieve PostgreSQL server_version_num: connection refused

    解决办法:添加kong.conf文件,编辑文件添加以下内容,选择不依赖数据库:

    database = off
    declarative_config = /data/work/kong/kong.yml
    

    3,发送 curl -i -X POST http://localhost:8001/services/
    -d 'name=example'
    -d 'url=http://example.com' 请求 报错:"message":"cannot create 'services' entities when not using a database","name":"operation unsupported","code":12

    解决办法:由于没有配置数据库,所以不能使用api创建服务,只能通过配置文件去设置。

    4,kong pack 报错:Error: Failed packing /data/work/kong/plugin/kong-plugin-template-master/kong-plugin-myplugin-0.1.0-1.all.rock

    解决办法:这是没有安装zip组件,执行命令:yum install -y zip

    5,luarocks install 报错:
    Warning: Failed searching manifest: Failed extracting manifest file: 'unzip -n' program not found. Make sure unzip is installed and is available in your PATH (or youmay want to edit the 'variables.UNZIP' value in file '/usr/local/etc/luarocks/config-5.1.lua')

    解决办法:这是没有安装unzip组件,执行命令:yum install -y unzip

    参考

    下载地址
    kong文档
    Service和Route配置参考

    相关文章

      网友评论

        本文标题:网关Kong Gateway

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