• nginx-unit docker 运行以及php &&golang 简单使用


    备注:
       nginx  unit  nginx 开源的新的nginx 开发平台,但是说白了,个人感觉一般,而且官方文档也不是很好,
    api 接口目前暂时文档比较。。。。,以前写过虚拟机&&物理机安装部署的方式,今天写一个使用docker
    进行安装部署的方式,同时添加golang语言的使用,算是比较全了,毕竟大部分公司再用容器。
     
    1. 参考项目
    https://github.com/rongfengliang/nginx-unit-docker-demo
    2. 项目说明
    a. 项目结构
    
    ├── Dockerfile   // docker 构建
    ├── Jenkinsfile  // jenkins 构建
    ├── README.md
    ├── app.go   // golang 语言支持
    ├── docker-compose.yml  // docker-compose 构建
    ├── run.sh  // 应用配置,通过rest 接口
    ├── start.json  // 应用配置  php && golang 
    └── website
        └── index.php  // php 语言
    
    
    b. 代码说明(使用docker多阶段构建)
    
    Dockerfile docker 构建
    
    FROM golang:stretch AS build-env
    RUN apt-get update
    # RUN apt-get install -y build-essential
    RUN git clone https://github.com/nginx/unit.git /go/src/github.com/nginx/unit
    
    ## 注意分之的版本 必须和你的unit 一致,不然报错,这个觉得不好,可能和目前生产可用性有关系吧
    
    ## 这个主要是golang sdk 的构建
    RUN cd /go/src/github.com/nginx/unit && git checkout tags/1.1 && ./configure && ./configure go && make go-install
    
    ADD . /go/src/github.com/rongfengliang/nginx-unit-go
    RUN cd /go/src/github.com/rongfengliang/nginx-unit-go && go build -o /appdemo app.go
    
    FROM nginx/unit:1.1-full
    WORKDIR /appdemo
    ADD start.json  /appdemo/start.json 
    COPY website /appdemo/website/
    COPY --from=build-env /appdemo /appdemo/website/appdemo
    EXPOSE 8080 8000 8081
    CMD ["/usr/sbin/unitd", "--no-daemon","--control", "0.0.0.0:8000"]
    
    Jenkinsfile  jenkinspipeline  构建
    
    pipeline {
      agent {
        node {
          label 'docker-65'
        }
      }
      stages {
        stage('docker-compose build') {
          steps {
            sh 'docker-compose build'
          }
        }
        stage('docker-compose run ') {
          steps {
            sh 'docker-compose up -d'
          }
        }
        stage('deploy service') {
          steps {
            sh './run.sh'
          }
        }
      }
    }
    
    app.go  golang 支持
    
    package main
    
    import (
    	"math"
    	"net/http"
    	"nginx/unit" ## 注意unit 版本,感觉做的有点不好,因为版本因为无法发布服务
    )
    
    func term(ch chan float64, k float64) {
    	ch <- 4 * math.Pow(-1, k) / (2*k + 1)
    }
    
    func handler(w http.ResponseWriter, r *http.Request) {
    	w.Write([]byte("appdemorong"))
    }
    
    func main() {
    	http.HandleFunc("/", handler)
    	//http.ListenAndServe(":8080", nil)
    	unit.ListenAndServe(":8089", nil)
    }
    
    
    docker-compose.yml  比较简单,不用说了
    
    version: "3"
    services: 
      nginx-unit:
        build:
          context: .
        image: myunit
        ports: 
         - "9080:8080"
         - "9000:8000" 
         - "9001:8081"
    
    run.sh   服务注册的shell
    #!/bin/sh
    curl -X PUT -d @$PWD/start.json http://localhost:9000
    
    start.json  服务配置的说明
    
    {
        "listeners":{
            "*:8080":{
                "application":"phpindex"
            },
            "*:8081":{
                "application":"golang"
            }
        },
        "applications":{
            "phpindex":{
               "type":"php",
               "processes":30,
               "root":"/appdemo/website",
               "index":"index.php"
            },
            "golang":{
                "type":"go",
                "processes":30,
                "working_directory": "/appdemo/website",
                "executable": "appdemo"
             }
        }
    }
    
    website/index.php  php  语言支持
    
    <?php
     phpinfo()
    ?>
    3. 构建&&启动
    docker-compose build
    docker-compose up -d
    4. 发布服务
    ./run.sh
    5. 效果
     
    a. 注册的服务列表
    b. golang 服务访问
     
    c.  php 服务访问
     
    6. 扩展
    多版本切换以及多版本也比较简单,就是服务注册配置文件的处理,总的来说,挺好的功能,但是在微服务,以及service mesh 的大趋势之下,就显得没有那么好的,
    也只是个人的观点
    7. 参考资料
    http://unit.nginx.org/
    https://github.com/rongfengliang/nginx-unit-docker-demo
  • 相关阅读:
    关于宏定义与内联函数
    vsv
    nginx与php之间的通信
    php高级
    PHP基础知识
    php结合redis实现高并发下的抢购、秒杀功能
    MySQL索引优化
    PHP基础(谈一谈Session&Cookie)
    Yii2框架查询指定字段和获取添加数据的id
    linux常用命令
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/9025222.html
Copyright © 2020-2023  润新知