美文网首页
Nginx Unit 1.2 + Go / Python 微服务

Nginx Unit 1.2 + Go / Python 微服务

作者: 半帅气 | 来源:发表于2018-06-13 09:48 被阅读0次

    1.简介

    Nginx Unit是一个面向微服务架构的、依托Nginx、支持多语言的动态Web应用服务器。
    在2018年6月7日发布了1.2版本。
    官网:https://www.nginx.com/products/nginx-unit/
    项目地址:https://github.com/nginx/unit

    2.功能

    • 可使用 RESTful JSON API 动态配置服务器
    • 可同时运行多语言及多版本的应用
    • 动态语言的进程管理功能(开发中)
    • TLS 支持(开发中)
    • TCP, HTTP, HTTPS, HTTP/2 路由和代理(开发中)

    3.支持语言

    • Python
    • PHP
    • Go
    • Perl
    • Ruby
    • JavaScript/Node.js (开发中)
    • Java (开发中)

    4.环境搭建

    OS:Ubuntu 18.04

    4.1 下载密钥并将其添加到apt的keyring

    $ wget https://nginx.org/keys/nginx_signing.key
    $ sudo apt-key add nginx_signing.key
    

    该密钥用于验证Nginx公司签发的仓库软件包,以消除安装过程中的告警。

    4.2 新增软件源

    创建文件:/etc/apt/sources.list.d/unit.list,并
    添加内容:

    deb https://packages.nginx.org/unit/ubuntu/ bionic unit
    deb-src https://packages.nginx.org/unit/ubuntu/ bionic unit
    

    4.3 安装Unit基础包

    $ sudo apt-get update
    $ sudo apt-get install unit
    

    4.4 安装模块包

    根据需要添加:

    $ sudo apt-get install unit-python3.6 unit-go1.9
    

    当前1.2版本支持模块有:

    unit-python2.7
    unit-python3.6
    unit-go1.9
    unit-go1.10
    unit-perl
    unit-php
    unit-ruby

    安装输出信息:

    The Go 1.9 module for NGINX Unit has been installed.
    
    To check out the sample app, run these commands:
    
     GOPATH=/usr/share/gocode /usr/lib/go-1.9/bin/go build -o /tmp/go1.9-app /usr/share/doc/unit-go1.9/examples/go-app/let-my-people.go
     sudo service unit restart
     sudo service unit loadconfig /usr/share/doc/unit-go1.9/examples/unit.config
     curl http://localhost:8500/
    
    Online documentation is available at https://unit.nginx.org
    
    ----------------------------------------------------------------------
    Unpacking unit-go1.9 (1.2-1~bionic) ...
    Selecting previously unselected package unit-python3.6.
    Preparing to unpack .../7-unit-python3.6_1.2-1~bionic_amd64.deb ...
    ----------------------------------------------------------------------
    
    The Python 3.6 module for NGINX Unit has been installed.
    
    To check out the sample app, run these commands:
    
     sudo service unit restart
     sudo service unit loadconfig /usr/share/doc/unit-python3.6/examples/unit.config
     curl http://localhost:8400/
    
    Online documentation is available at https://unit.nginx.org
    
    ----------------------------------------------------------------------
    Unpacking unit-python3.6 (1.2-1~bionic) ...
    Setting up golang-1.9-race-detector-runtime (0.0+svn285455-0ubuntu1) ...
    Setting up pkg-config (0.29.1-0ubuntu2) ...
    Setting up golang-1.9-src (1.9.4-1ubuntu1) ...
    Processing triggers for man-db (2.8.3-2) ...
    Setting up unit-python3.6 (1.2-1~bionic) ...
    Setting up golang-1.9-go (1.9.4-1ubuntu1) ...
    Setting up golang-1.9-doc (1.9.4-1ubuntu1) ...
    Setting up golang-1.9 (1.9.4-1ubuntu1) ...
    Setting up unit-go1.9 (1.2-1~bionic) ...
    

    5.开发步骤

    5.1 启动unit服务

    $ sudo unitd --control 127.0.0.1:8443
    

    为了便于使用RESTful JSON API动态配置服务器,配置--control指定IP和端口,默认采用:
    unix:/var/run/control.unit.sock

    查看进程是否成功启动:


    调用返回如下信息,说明启动成功:


    5.2 服务接口编写

    • Go

    文件名:unit.go

    package main
        
    import (
        "fmt"
        "net/http"
        "nginx/unit"
    )   
        
    func index(w http.ResponseWriter, r *http.Request) {
        w.Header().Add("Content-Type", "text/plain")
        fmt.Fprintf(w, "Go ngnix unit")
    }   
        
    func main() {
        http.HandleFunc("/", index)                                                                                                                                           
        unit.ListenAndServe("8000", nil)
    } 
    

    编译:

    $ GOPATH=/usr/share/gocode go build -o go-app unit.go
    
    • python

    unit.py

    import sys 
                                                                                                                                                                              
    def application(environ, start_response):
        body = sys.version.encode("utf-8")
        status = "200 OK"
        headers = [("Content-type", "text/plain")]
        start_response(status, headers)
        return [body]
    

    5.3 动态全量添加配置

    $ cat go.json 
    {
        "applications": {
            "example_go": {
                "type": "go",
                "user": "nobody",
                "executable": "/home/jasonruan/Software/Ngnix/Unit/example/go/go-app"
            }
        },
    
        "listeners": {
            "*:8500": {
                "application": "unit_go"
            }
        }
    }
    
    • 全量配置动态加载:
    $ curl -X PUT -d@go.json 127.0.0.1:8443
    {
        "success": "Reconfiguration done."
    }
    
    • 查看配置,已加载成功:
    • 测试接口,返回预期信息:

    5.4 动态调整配置

    • 编写python服务接口的配置,进行增量追加
      配置:
    $ cat unit_python.json 
    {
        "type": "python 3.6",
        "user": "nobody",
        "processes": 2,
        "path": "/home/jasonruan/Software/Ngnix/Unit/example/python",
        "module": "unit"
    }
    
    • 追加配置:
    $ curl -X PUT -d@unit_python.json '127.0.0.1:8443/applications/unit_python'
    {
        "success": "Reconfiguration done."
    }
    
    • 查看配置情况:
    $ http 127.0.0.1:8443
    HTTP/1.1 200 OK
    Connection: close
    Content-Length: 421
    Content-Type: application/json
    Date: Wed, 13 Jun 2018 01:22:42 GMT
    Server: Unit/1.2
    
    {
        "applications": {
            "unit_go": {
                "executable": "/home/jasonruan/Software/Ngnix/Unit/example/go/go-app",
                "type": "go",
                "user": "nobody"
            },
            "unit_python": {
                "module": "unit",
                "path": "/home/jasonruan/Software/Ngnix/Unit/example/python",
                "processes": 2,
                "type": "python 3.6",
                "user": "nobody"
            }
        },
        "listeners": {
            "*:8500": {
                "application": "unit_go"
            }
        }
    }
    
    • 将listeners动态改为unit_python:
    $ curl -X PUT -d '"unit_python"' '127.0.0.1:8443/listeners/*:8500/application'
    {
        "success": "Reconfiguration done."
    }
    
    • 测试:

      符合预期,返回内容是python api提供服务内容。

    • 追加Go的listener配置:

    $ curl -X PUT -d@unit_go.json '127.0.0.1:8443/listeners/*:8600'
    
    • 测试:

      Go和Python服务均能正常访问,符合预期。

    5.5 动态删除配置

    $ curl -X DELETE '127.0.0.1:8443/listeners/*:8600'
    {
        "success": "Reconfiguration done."
    }
    $ curl -X DELETE '127.0.0.1:8443/applications/unit_go'
    {
        "success": "Reconfiguration done."
    }
    

    注:需要删除listeners后方能删除applications配置。


    6.后记

    本文只是一篇初浅的入门文章,重点介绍了环境搭建以及动态配置服务器,与Nginx整合及安全性等方面后续进行补充。

    7.参考

    http://unit.nginx.org/?_ga=2.114330055.1787926827.1528855190-288714655.1525766596

    相关文章

      网友评论

          本文标题:Nginx Unit 1.2 + Go / Python 微服务

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