关于hyperf:hyperf-实战之安装连载中

44次阅读

共计 7636 个字符,预计需要花费 20 分钟才能阅读完成。

转发自白狼栈:查看原文

首先,我本地的环境是 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 框架曾经装置结束。

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

装置上如果有任何问题,能够在文章上面或者在 http://funqas.com/ 上发问留言。

正文完
 0