美文网首页
Kong原理

Kong原理

作者: 诺之林 | 来源:发表于2019-03-07 11:44 被阅读1次

    目录

    Nginx

    Nginx

    NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more

    • Web Server A web server stores and delivers the content for a website – such as text, images, video, and application data – to clients that request it

    • Reverse Proxy A reverse proxy accepts a request from a client, forwards it to a server that can fulfill it, and returns the server’s response to the client

    • Load Balancing A load balancer distributes incoming client requests among a group of servers, in each case returning the response from the selected server to the appropriate client

    • Content Caching When caching is enabled, nginx saves responses in a disk cache and uses them to respond to clients without having to proxy requests for the same content every time

    关于Nginx的更多讨论可以参考8分钟带你深入浅出搞懂Nginx反向代理为何叫反向代理?

    Nginx Plus

    NGINX Plus is a software load balancer, web server, and content cache built on top of open source NGINX. NGINX Plus has exclusive enterprise‑grade features beyond what's available in the open source offering, including session persistence, configuration via API, and active health checks

    关于NGINX Plus和Nginx的更多对比可以参考Compare Versions

    Nginx Module

    高度模块化的设计是Nginx的架构基础 Nginx服务器被分解为多个模块 每个模块就是一个 功能模块 只负责自身的功能 模块之间严格遵循"高内聚 低耦合"的原则

    Handlers: 直接处理请求 并进行输出内容和修改headers信息等操作
    
    Filters: 主要对其他处理器模块输出的内容进行修改操作 最后由Nginx输出
    
    Proxies: Nginx的HTTP Upstream之类的模块 这些模块主要与后端一些服务比如FastCGI等进行交互 实现服务代理和负载均衡等功能
    
    核心模块: HTTP模块、Event模块和Mail模块
    
    基础模块: HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块
    
    第三方模块: HTTP Upstream Request Hash模块、Notice模块和HTTP Access Key模块
    

    关于Nginx的模块更多可以参考Modules referenceNGINX 3rd Party Modules

    OpenResty

    Lua

    Lua 是一种轻量小巧的脚本语言 用标准C语言编写并以源代码形式开放 其设计目的是为了嵌入应用程序中 从而为应用程序提供灵活的扩展和定制功能

    • Lua is portable 它用标准C语言编写并以源代码形式开放 编译后仅仅一百余K 可以很方便的嵌入别的程序里

    • Lua is fast Lua has a deserved reputation for performance. To claim to be "as fast as Lua" is an aspiration of other scripting languages

    • 自动内存管理 只提供了一种通用类型的表(table) 用它可以实现数组、哈希表、集合、对象

    # 基于Ubuntu 1604
    sudo apt install -y lua5.2
    
    lua -v
    # Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
    
    echo 'print("Hello World")' > test.lua
    
    lua test.lua
    # Hello World
    
    lua
    > print("Hello World")
    Hello World
    

    关于Lua更多介绍可以参考Lua 教程LUA简明教程

    lua-nginx-module

    Embed the Power of Lua into NGINX HTTP servers. This module is a core component of OpenResty. If you are using this module, then you are essentially using OpenResty :)

    OpenResty

    OpenResty是一个基于Nginx与Lua的高性能Web 平台 其内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项 用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关

    • Kong安装时会包含OpenResty 即Kong ⊃ OpenResty ⊃ lua-nginx-module

    • 章亦春是OpenResty软件包的主要作者之一

    关于作者的更多介绍可以参考https://openresty.org/cn/yichun-zhang.html锤子手机发布会提到的 OpenResty 是什么?

    mkdir ~/work && cd ~/work && mkdir logs/ conf/
    
    vim conf/nginx.conf
    
    worker_processes  1;
    error_log logs/error.log;
    events {
        worker_connections 1024;
    }
    http {
        server {
            listen 8080;
            location / {
                default_type text/html;
                content_by_lua '
                    s = "<p>hello, world!</p>"
                    ngx.say(s)
                ';
            }
        }
    }
    
    PATH=/usr/local/openresty/nginx/sbin:$PATH
    
    export PATH
    
    which nginx
    # /usr/local/openresty/nginx/sbin/nginx
    
    nginx -p `pwd`/ -c conf/nginx.conf
    
    curl http://localhost:8080/
    # <p>hello, world!</p>
    

    Kong

    • Kong is a management layer around OpenResty

    • It provides a nice REST based API using which one can dynamically add and remove API and also functionality to Nginx/OpenResty using a plugin based architecture

    • Kong converts OpenResty into an API gateway and solves many of the operational challenges involved in managing a web based load balancer

    kong-architecture.png
    sudo kong start
    
    ps -ax | grep kong
    # 12272 ?        Ss     0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -p /usr/local/kong -c nginx.conf
    

    更多Kong的介绍可以参考Frequently Asked Questions

    参考

    相关文章

      网友评论

          本文标题:Kong原理

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