1、安装环境、以下是 dockerfile 文件,有环境请忽略
FROM php:7.3.17-cli-alpine3.11
ARG swoole_ver
ENV SWOOLE_VER=${swoole_ver:-"v4.4.15"}
RUN set -ex \
&& cd /tmp \
&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk update \
&& apk add vim git autoconf openssl-dev build-base zlib-dev re2c libpng-dev oniguruma-dev
RUN cd /tmp \
&& wget https://mirrors.aliyun.com/composer/composer.phar \
&& chmod u+x composer.phar \
&& mv composer.phar /usr/local/bin/composer \
&& composer config -g repo.packagist composer https://mirrors.aliyun.com/composer \
&& echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
RUN php -m \
&& docker-php-ext-install gd pdo_mysql mysqli sockets pcntl \
&& php -m
RUN cd /tmp \
# from mirrors
&& git clone https://gitee.com/swoole/swoole swoole \
&& cd swoole \
&& git checkout ${SWOOLE_VER} \
&& phpize \
&& ./configure --enable-openssl --enable-sockets --enable-http2 --enable-mysqlnd \
&& make \
&& make install \
&& docker-php-ext-enable swoole \
&& php -m \
&& php --ri swoole
RUN cd /tmp \
# from mirrors
&& git clone https://gitee.com/mirrors/phpredis phpredis \
&& cd phpredis \
&& phpize \
&& ./configure \
&& make \
&& make install \
&& docker-php-ext-enable redis \
&& php -m \
&& php --ri redis
RUN cd /tmp \
&& php -v \
&& php -m \
&& echo -e "Build Completed!"
EXPOSE 9501
WORKDIR /home/www
2、构建 dockerfile、因为我的dockerfile文件是放在桌面的
docker build -f C:\Users\lenovo\Desktop\dockerfile -t swoole/php-swoole-hyperf:1.0 .
3、构建完成得到镜像
4、运行镜像
docker run -di -p 6379:6379 -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 -v E:/project:/home/www --name swoole swoole/php-swoole-hyperf:1.0
5、查看镜像并且进入镜像
docker ps --查看镜像
docker run exec -it aac744f38a32 sh --进入镜像内部
6、确认当前目录是否是自己映射的目录,安装hyperf
composer create-project hyperf/hyperf-skeleton --安装hyperf框架
cd hyperf-skeleton --进入hyperf框架
php bin/hyperf.php start --运行框架
配置Hyperf热更新
composer require hyperf/watcher --dev
生成配置文件
php bin/hyperf.php vendor:publish hyperf/watcher (文件位置:config/autoload/watcher)
热更新启动框架
php bin/hyperf.php server:watch
配置文件说明
return [
'driver' => ScanFileDriver::class,
'bin' => 'php',
'watch' => [
'dir' => ['app', 'config'],
'file' => ['.env'],
'scan_interval' => 2000,
],
];
配置 | 默认值 | 备注 |
---|---|---|
driver | ScanFileDriver | 默认定时扫描文件驱动 |
bin | php | 用于启动服务的脚本 |
watch.dir | app, config | 监听目录 |
watch.file | .env | 监听文件 |
watch.interval | 2000 | 扫描间隔(毫秒) |
如果启动框架报这个错
ERROR Swoole short name have to disable before start server, please set swoole.use_shortname = off into your php.ini.
那么可以在php.ini配置文件中增加以下(1)这个配置项或者是用(2)这个方式启动
1、swoole.use_shortname = 'Off'
2、php -d swoole.use_shortname=Off bin/hyperf.php start
7、安装rabbit,前面有写,这里就不重复了
8、撸代码
1、首先在项目中安装AMQP扩展
composer require hyperf/amqp
2、添加amqp配置文件
php bin/hyperf.php vendor:publish hyperf/amqp
3、链接rabbitMQ
连接rabbitMQ的坑
不要使用localhost或者是127.0.0.1
docker inspect --format='{{.NetworkSettings.IPAddress}}' rabbit --查看docker内web的ip地址
使用docker ip 内部的地址
我的.env文件
APP_NAME=skeleton
APP_DEBUG=true
DB_DRIVER=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=hyperf
DB_USERNAME=root
DB_PASSWORD=123456
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci
DB_PREFIX=
REDIS_HOST=172.17.0.4
REDIS_AUTH=(null)
REDIS_PORT=6379
REDIS_DB=0
RABBIT_HOST=172.17.0.2
RABBIT_PORT=5672
RABBIT_USER=guest
RABBIT_PASSWORD=guest
config/amqp.php 配置
amqp.php 文件
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
return [
'default' => [
'host' => env('RABBIT_HOST'),
'port' => (int) env('RABBIT_PORT'),
'user' => env('RABBIT_USER'),
'password' => env('RABBIT_PASSWORD'),
'vhost' => '/',
'concurrent' => [
'limit' => 1,
],
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
],
'params' => [
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'connection_timeout' => 3.0,
'read_write_timeout' => 6.0,
'context' => null,
'keepalive' => false,
'heartbeat' => 3,
'close_on_destruct' => true,
],
],
];
4、安装amqplib
composer require php-amqplib/php-amqplib ---composer安装amqplib
5、生产者
php bin/hyperf.php gen:amqp-producer DemoProducer
6、消费者
php bin/hyperf.php gen:amqp-consumer DemoConsumer
完整的目录
7、测试
DemoProducer.php
<?php
declare(strict_types=1);
namespace App\Amqp\Producer;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
/**
* DemoProducer
* @Producer(exchange="hyperf", routingKey="hyperf")
*/
class DemoProducer extends ProducerMessage
{
public function __construct($data)
{
$this->payload = $data;
}
}
DemoConsumer.php
<?php
declare(strict_types=1);
namespace App\Amqp\Consumer;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Result;
/**
* @Consumer(exchange="hyperf", routingKey="hyperf", queue="hyperf", nums=1)
*/
class DemoConsumer extends ConsumerMessage
{
/**
* rabbitMQ消费者代码
* @param $data
* @return string
*/
public function consume($data): string
{
// print_r($data);
var_dump('投放至队列的数据:' . $data);
return Result::ACK;
}
}
在 App\Controller\IndexController 下编写测试代码
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use App\Amqp\Producer\DemoProducer;
use Hyperf\Amqp\Producer;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Utils\ApplicationContext;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Swoole\Server;
use TheSeer\Tokenizer\Exception;
/**
* @AutoController()
* Class IndexController
* @package App\Controller
*/
class IndexController extends AbstractController
{
/**
* @var Producer
*/
private $product;
public function __construct(Producer $product)
{
$this->product = $product;
}
/**
* 测试rabbit代码
* @throws \Swoole\Exception
*/
public function rabbit()
{
$data = [
'code' => 200,
'data' => [
'rabbit_msg' => 'From rabbit message',
'time' => date('Y-m-d H:i:s', time())
]
];
$data = \GuzzleHttp\json_encode($data);
// 将消息推送给生产者
$message = new DemoProducer($data);
// 传递消息
try {
$this->product->produce($message);
} catch (\Exception $exception) {
throw new \Swoole\Exception($exception->getMessage());
}
return [
'msg' => 'rabbit测试方法',
'time' => date('Y-m-d H:i:s', time()),
];
}
}
8、启动hyperf框架
php bin/hyperf.php start
9、调用接口
服务端消息
初次使用,记录一下简单的Demo