Nginx Unit 1.2 + Go / Python 微服務開發(fā)起步

1.簡介

Nginx Unit是一個面向微服務架構的、依托Nginx、支持多語言的動態(tài)Web應用服務器。
在2018年6月7日發(fā)布了1.2版本。
官網(wǎng):https://www.nginx.com/products/nginx-unit/
項目地址:https://github.com/nginx/unit

2.功能

  • 可使用 RESTful JSON API 動態(tài)配置服務器
  • 可同時運行多語言及多版本的應用
  • 動態(tài)語言的進程管理功能(開發(fā)中)
  • TLS 支持(開發(fā)中)
  • TCP, HTTP, HTTPS, HTTP/2 路由和代理(開發(fā)中)

3.支持語言

  • Python
  • PHP
  • Go
  • Perl
  • Ruby
  • JavaScript/Node.js (開發(fā)中)
  • Java (開發(fā)中)

4.環(huán)境搭建

OS:Ubuntu 18.04

4.1 下載密鑰并將其添加到apt的keyring

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

該密鑰用于驗證Nginx公司簽發(fā)的倉庫軟件包,以消除安裝過程中的告警。

4.2 新增軟件源

創(chuàng)建文件:/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 安裝模塊包

根據(jù)需要添加:

$ 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.開發(fā)步驟

5.1 啟動unit服務

$ sudo unitd --control 127.0.0.1:8443

為了便于使用RESTful JSON API動態(tài)配置服務器,配置--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 動態(tài)全量添加配置

$ 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"
        }
    }
}
  • 全量配置動態(tài)加載:
$ curl -X PUT -d@go.json 127.0.0.1:8443
{
    "success": "Reconfiguration done."
}
  • 查看配置,已加載成功:
  • 測試接口,返回預期信息:

5.4 動態(tài)調整配置

  • 編寫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動態(tài)改為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 動態(tài)刪除配置

$ 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.后記

本文只是一篇初淺的入門文章,重點介紹了環(huán)境搭建以及動態(tài)配置服務器,與Nginx整合及安全性等方面后續(xù)進行補充。

7.參考

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,520評論 19 139
  • 第一章 Nginx簡介 Nginx是什么 沒有聽過Nginx?那么一定聽過它的“同行”Apache吧!Ngi...
    JokerW閱讀 33,012評論 24 1,002
  • 從 LAMP、LEMP 到 LEMUR 從 20 世紀 90 年代開始,LAMP 架構成為了部署 Web 應用程序...
    51reboot閱讀 1,650評論 1 2
  • 原諒我,來信我沒有一一回復,我無法解決所有故事里的困境,因為我很平凡也會軟弱,和所有人一樣,在自己的生活里重復著每...
    小北愛吃肉r閱讀 418評論 0 0
  • 一個像樣的App,首先要有一個像樣的門面 ---app啟動頁,現(xiàn)在我就給大家分享下我做過的各種各樣的啟動頁! 新手...
    輕斟淺醉17閱讀 2,789評論 1 5

友情鏈接更多精彩內容