date: 2018-8-01 14:22:17
title: swoft| 源码解读系列一: 好难! swoft demo 都跑不起来怎么破? docker 了解一下呗~
description: 阅读 sowft 框架源码, swoft 第一步, 搞定环境php
小伙伴刚接触 swoft 的时候会感受 压力有点大, 更直观的说法是 难. 开发组是不同意 难 这个说法的, swoft 的代码都是 php 实现的, 而 php 又是 世界上最好的语言, swoft 的代码阅读起来是很轻松的.html
开发组会用 源码解读系列 博客, 深刻解读 swoft. 咱们相信, 这会成为一段轻松之旅.mysql
swoft 源码解读系列一: 好难! swoft demo 都跑不起来怎么破? docker 了解一下呗~linux
本篇内容速读:nginx
这个好难真心不是 swoft 的锅, 这是环境的锅. swoft 官方文档已经提供了至关详细的 环境搭建 说明, 若是一直没法成功:git
学习和使用 swoft 须要预备这些 基础知识.github
那么, 怎么快速配好开发环境呢? 答案是 docker !web
docker 要上手比想象中的要容易, 开发组提供了 官方镜像 swoft/swoft, 镜像详情在 swoft项目dockerfile 中, 摘录其中配置 swoole 的部分:redis
# Swoole extension RUN wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \ && mkdir -p swoole \ && tar -xf swoole.tar.gz -C swoole --strip-components=1 \ && rm swoole.tar.gz \ && ( \ cd swoole \ && phpize \ && ./configure --enable-async-redis --enable-mysqlnd --enable-openssl --enable-http2 \ && make -j$(nproc) \ && make install \ ) \ && rm -r swoole \ && docker-php-ext-enable swoole
若是你尚未掌握 swoole 运行所需环境的配置, 能够参考这个 dockerfile 文件的源码.sql
固然, 为了开发方便, 咱们可能须要构建不一样的环境, 好比指定不一样的 php 版本, 使用不一样的 swoole 版本, 设置中文镜像加速等, 也能够参考 gitee.com/daydaygo/docker 下的 dockerfile
FROM php:7.2.5-cli-alpine3.7 # FROM php:7.1.13-cli-alpine3.4 LABEL maintainer="1252409767@qq.com" RUN echo -e "http://mirrors.ustc.edu.cn/alpine/v3.7/main\nhttp://mirrors.ustc.edu.cn/alpine/v3.7/community" > /etc/apk/repositories && \ apk update RUN apk add tzdata && \ cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone RUN apk add $PHPIZE_DEPS # docker-php-ext RUN docker-php-ext-install bcmath mysqli pcntl pdo_mysql # pecl # http://pecl.php.net/package/mongodb RUN curl -O http://pecl.php.net/get/redis-4.0.2.tgz && \ pecl install redis-4.0.2.tgz && \ docker-php-ext-enable redis RUN curl -O http://pecl.php.net/get/mongodb-1.4.3.tgz && \ apk add openssl-dev && \ pecl install mongodb-1.4.3.tgz && \ docker-php-ext-enable mongodb # swoole RUN curl -O https://gitee.com/swoole/swoole/repository/archive/v4.0.3.zip && unzip v4.0.3.zip && \ apk add linux-headers openssl-dev nghttp2-dev hiredis-dev && \ cd swoole && \ phpize && \ ./configure --enable-coroutine --enable-openssl --enable-async-redis --enable-http2 && make && make install && \ docker-php-ext-enable swoole && \ rm -rf v4.0.3.zip swoole
ps: 这里的 dockerfile 使用 alpine linux 作基础镜像, 大小不到 10m, 很是简单纯粹的一个 linux 发行版, 推荐尝试. 使用了国内源和gitee进行加速.
到这里咱们已经有了 swoft 的运行环境了, 根据 文档-服务启动 章节中的说明, 执行 php bin/swoft start
就能够将 swoft demo 运行起来了.
若是咱们还须要更多服务, mysql呀, redis呀, 甚至前置 nginx, docker-compose 能够帮助到咱们, docker-compose 用来把咱们每个 docker 服务编排(管理)起来, 举个例子 gitee.com/daydaygo/docker:
nginx: build: context: nginx dockerfile: Dockerfile volumes: - ../:/var/www - ./logs/nginx/:/var/log/nginx links: - swoft ports: - "80:80" - "443:443" swoft: # container_name: swoft image: swoft/swoft volumes: - ../:/var/www links: - mysql - redis ports: - "8001:8001" - "9501:9501" working_dir: /var/www/swoole/swoft stdin_open: true command: php -a tty: true - redis mysql: build: context: mysql dockerfile: Dockerfile volumes: - ./data/mysql:/var/lib/mysql ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: root redis: build: context: redis dockerfile: Dockerfile volumes: - ./data/redis:/data - ./logs/redis:/var/log/redis ports: - "6379:6379"
这里咱们启动了 nginx / swoft / mysql / redis 4个服务:
links
: 服务之间的关系, 好比 swoft 会使用到 mysql 和 redis, 那么 swoft 中就可使用 mysql
做为 host
访问到 myql 服务ports
: 端口映射volumes
: 文件挂载了解这些标签都有什么做用, 就能理解和使用 docker-compose 了
使用 git clone
下载了 swoft 的源码后, 还须要使用 composer install
安装 swoft 框架. 在此以前, swoft 进行了 组件化拆分, 让框架进一步的解耦和易用. 普通用户不多会修改 composer install
安装的 swoft 框架, 可是开发组会频繁更新 swoft 框架, 而后在 swoft demo 项目中验证, 那是如何实现的呢?
答案是使用 composer 提供的 repositories
功能, 直接引入本带代码库:
{ "name": "swoft/swoft", "type": "project", "keywords": [ "php", "swoole", "swoft" ], "description": "Modern High performance AOP and Coroutine PHP Framework, base on Swoole 2", "license": "Apache-2.0", "require": { "php": ">=7.0", "ext-swoole": ">=2.1", "swoft/framework": "^1.0", "swoft/rpc": "^1.0", "swoft/rpc-server": "^1.0", "swoft/rpc-client": "^1.0", "swoft/http-server": "^1.0", "swoft/http-client": "^1.0", "swoft/websocket-server": "^1.0", "swoft/task": "^1.0", "swoft/http-message": "^1.0", "swoft/view": "^1.0", "swoft/db": "^1.1", "swoft/cache": "^1.0", "swoft/redis": "^1.0", "swoft/console": "^1.0", "swoft/devtool": "^1.0", "swoft/session": "^1.0", "swoft/i18n": "^1.0", "swoft/process": "^1.0", "swoft/memory": "^1.0", "swoft/service-governance": "^1.0" }, "autoload": { "psr-4": { "App\\": "app/" }, "files": [ "app/Swoft.php" ] }, "autoload-dev": { "psr-4": { "Swoft\\Test\\": "test/" } }, "scripts": { "post-root-package-install": [ "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], "test": "./vendor/bin/phpunit -c phpunit.xml", "cs-fix": "./vendor/bin/php-cs-fixer fix $1" }, "repositories": [ { "type": "path", // 修改在此 "url": "../swoft-component" } ] }
我习惯删掉 require-dev
下配置的包, 而选择本地全局安装, 这点就全凭我的喜爱啦
但愿你们看到 swoft demo 首页的 swoft
时, 能和咱们同样开心