hyperf 实战之安装(连载中)

1,645 阅读6分钟

转发自白狼栈:查看原文

首先,我本地的环境是 Mac + PHP 7.4.21 + Swoole Version => 4.6.7 + Composer version 2.4.4,作为学习,建议大家统一选择官方的 docker 镜像。

我们先在本地创建一个目录,这个目录会直接映射到 hyperf 容器的 /data/project 目录。

~ » cd /data/work
/data/work » mkdir questions_learn

执行下面的命令安装 hyperf 镜像,可能需要等待几分钟等待时间(如果本地没有这个镜像的话)。

docker run --name hyperf-php-8.0 \
-v /data/work/questions_learn:/data/project \
-p 9501:9501 -it \
--privileged -u root \
--entrypoint /bin/sh \
hyperf/hyperf:8.0-alpine-v3.15-swoole

在这一步中,大家记得把 -v 后面的 "/data/work/questions_learn" 改成你本地的目录,其他的保持不变。

上面的安装命令执行成功后,我们就已经在 hyperf 容器内了,来一起看下 hyperf 容器内的环境。

/ # php -v
PHP 8.0.18 (cli) (built: Apr 22 2022 22:53:40) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.18, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.18, Copyright (c), by Zend Technologies

/ # php --ri swoole
swoole
Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 5.0.0
Built => Aug  2 2022 03:09:01
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.1.1q  5 Jul 2022
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
pcre => enabled
zlib => 1.2.12
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
async_redis => enabled
Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

/ # composer -V
Composer version 2.2.1 2021-12-22 22:21:31

/ # composer config -gl
[repositories.packagist.org.type] composer
[repositories.packagist.org.url] https://repo.packagist.org
......(省略了其他代码)

将 Composer 镜像设置为阿里云镜像,加速国内下载速度
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer

可以看到,官方镜像默认支持了很多扩展,如果你选择自己本地的环境,请至少确保你的运行环境达到了以下要求:

  • PHP >= 8.0 and <= 8.1
  • Swoole PHP 扩展 >= 4.5,并关闭了 Short Name
  • JSON PHP 扩展
  • Pcntl PHP 扩展
  • enabled swoole coroutine、openssl、curl-native、async_redis、rwlock、pcre、zlib、brotl

另外,由于 Hyperf 基于 Swoole 协程实现,而 Swoole 4 带来的协程功能是 PHP 前所未有的,所以与不少扩展都仍存在兼容性的问题。

以下扩展(包括但不限于)都会造成一定的兼容性问题,不能与之共用或共存:

  • xhprof
  • xdebug
  • blackfire
  • trace
  • uopz

这些我们基本也用不上,推荐大家按照文中的操作,选择官方镜像。

下面我们继续在容器内操作。

创建一个项目,并命名 questions

/ # cd /data/project/
/data/project # composer create-project hyperf/hyperf-skeleton questions

执行 composer create-project 命令的时候,开始会一直提示我们安装一大堆东西,直接全部回车,后面需要我们再手动进行安装。

安装好之后,安装的 hyperf 还是 2.2 版本,通过 composer info 命令可以查看已安装的 hyperf 相关的版本信息。

/data/project/questions # composer info | grep hyperf
hyperf/cache                       v2.2.33 A cache component for hyperf.
hyperf/command                     v2.2.35 Command for hyperf
hyperf/config                      v2.2.33 An independent component that provides configuration container.
hyperf/context                     v2.2.33 A coroutine context library.
hyperf/contract                    v2.2.33 The contracts of Hyperf.
hyperf/database                    v2.2.33 A flexible database library.
hyperf/db-connection               v2.2.33 A hyperf db connection handler for hyperf/database.
hyperf/devtool                     v2.2.33 A Devtool for Hyperf.
hyperf/di                          v2.2.33 A DI for Hyperf.
hyperf/dispatcher                  v2.2.33 A HTTP Server for Hyperf.
hyperf/engine                      v1.3.4
hyperf/event                       v2.2.33 an event manager that implements PSR-14.
hyperf/exception-handler           v2.2.33 Exception handler for hyperf
hyperf/framework                   v2.2.33 A coroutine framework that focuses on hyperspeed and flexible, specifically use for build microservices and middlewares.
hyperf/guzzle                      v2.2.33 Swoole coroutine handler for guzzle
hyperf/http-message                v2.2.33 microservice framework base on swoole
hyperf/http-server                 v2.2.33 A HTTP Server for Hyperf.
hyperf/ide-helper                  v2.2.33 IDE help files for Hyperf.
hyperf/logger                      v2.2.33 A logger component for hyperf.
hyperf/macroable                   v2.2.33 Hyperf Macroable package which come from illuminate/macroable
hyperf/memory                      v2.2.33 An independent component that use to operate and manage memory.
hyperf/model-listener              v2.2.33 A model listener for Hyperf.
hyperf/pool                        v2.2.33 An independent universal connection pool component.
hyperf/process                     v2.2.33 A process component for hyperf.
hyperf/redis                       v2.2.33 A redis component for hyperf.
hyperf/server                      v2.2.33 A base server library for Hyperf.
hyperf/testing                     v2.2.33 Testing for hyperf
hyperf/utils                       v2.2.34 A tools package that could help developer solved the problem quickly.

这是因为 3.0 正式还没有发布,如果你安装的时候发现已经是3.0的正式包了,下面的升级步骤可以直接跳过。

接下来大家可以参考官方的升级指南,也可以跟着当前教程继续操作。

composer require hyperf/code-generator
php bin/hyperf.php code:generate -D app

直接将 composer.json 中的 hyperf/* 统一修改为 3.0.* ,然后执行 composer update -o,现在我们重新看下 hyperf 相关的版本信息。

/data/project/questions # composer info | grep hyperf
hyperf/cache                       v3.0.0-rc.5   A cache component for hyperf.
hyperf/code-generator              v0.3.3        Code Generator for Hyperf
hyperf/command                     v3.0.0-rc.7   Command for hyperf
hyperf/config                      v3.0.0-rc.8   An independent component that provides configuration container.
hyperf/context                     v3.0.0-rc.1   A coroutine context library.
hyperf/contract                    v3.0.0-rc.10  The contracts of Hyperf.
hyperf/coordinator                 v3.0.0-beta.1 Hyperf Coordinator
hyperf/database                    v3.0.0-rc.10  A flexible database library.
hyperf/db-connection               v3.0.0-rc.4   A hyperf db connection handler for hyperf/database.
hyperf/devtool                     v3.0.0-rc.12  A Devtool for Hyperf.
hyperf/di                          v3.0.0-rc.12  A DI for Hyperf.
hyperf/dispatcher                  v3.0.0-rc.3   A HTTP Server for Hyperf.
hyperf/engine                      v1.3.4
hyperf/event                       v3.0.0-rc.12  an event manager that implements PSR-14.
hyperf/exception-handler           v3.0.0-rc.9   Exception handler for hyperf
hyperf/framework                   v3.0.0-rc.8   A coroutine framework that focuses on hyperspeed and flexible, specifically use for build microservices and middlewares.
hyperf/guzzle                      v3.0.0-rc.3   Swoole coroutine handler for guzzle
hyperf/http-message                v3.0.0-rc.8   microservice framework base on swoole
hyperf/http-server                 v3.0.0-rc.10  A HTTP Server for Hyperf.
hyperf/ide-helper                  v3.0.0-rc.1   IDE help files for Hyperf.
hyperf/logger                      v3.0.0-rc.3   A logger component for hyperf.
hyperf/macroable                   v3.0.0-rc.1   Hyperf Macroable package which come from illuminate/macroable
hyperf/memory                      v3.0.0-rc.1   An independent component that use to operate and manage memory.
hyperf/model-listener              v3.0.0-rc.1   A model listener for Hyperf.
hyperf/pool                        v3.0.0-rc.3   An independent universal connection pool component.
hyperf/process                     v3.0.0-rc.3   A process component for hyperf.
hyperf/redis                       v3.0.0-rc.4   A redis component for hyperf.
hyperf/server                      v3.0.0-rc.1   A base server library for Hyperf.
hyperf/testing                     v3.0.0-rc.1   Testing for hyperf
hyperf/utils                       v3.0.0-rc.10  A tools package that could help developer solved the problem quickly.

部分组件的 rc 版本可能跟文中的不太一致,注意是3.0.*的版本即可。

启动项目测试下是否安装成功

/data/project/questions # php bin/hyperf.php start
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#1 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.

这里注意,更改过框架内的任何代码,都需要手动执行 php bin/hyperf.php start 命令,已经启动的服务可以在终端按住 Ctrl + C 键停止服务。

本地我们先按照这个模式走,后期上线会全自动操作,这点无需担心。

窗口卡着不动即表示安装成功。

以上,我们都是在容器内执行的命令操作。(将 composer.json 中的 hyperf/* 统一修改为 3.0.* ,这个大家可以在宿主机用 ide 进行编辑修改,编辑的是本地的 /data/work/questions_learn/questions/composer.json 文件)

回到宿主机内,对本地的 9501 端口请求测试。

/data/work » curl 127.0.0.1:9501
{"method":"GET","message":"Hello Hyperf."}%

至此,hyperf 框架已经安装完毕。

教程中演示的所有代码,都会上传在 github.com/bailangzhan…

安装上如果有任何问题,可以在文章下面或者在 funqas.com/ 上提问留言。