根本知识遍及:

1、hyerf:https://hyperf.wiki/2.0/#/

2、consul:https://www.consul.io/

php开发服务PHP微办事实践手把手教你搭建PHP微办事 Bootstrap

3、swoole:https://wiki.swoole.com/#/

4、docker:https://www.runoob.com/docker/docker-tutorial.html

5、docker-compose:https://www.runoob.com/docker/docker-compose.html

本文基于文章 《如何搭建PHP 微做事集群》 支配

支配环境:

"centos": ">=7.2""php": ">=7.2""hyperf": "=2.0""docker": "=19.03.13""docker-compose": "=1.27.4""swoole": "=4.2""Consul": "=1.8.6"安装所需环境:

1、安装PHP7.2w

yum -y remove phprpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmrpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpmyum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml

2、pear安装swoole

yum install php72w-pearyum install php72w-develpecl install swoole

修正 php.ini 配置文件

extension=swoole.so

3、 安装composer

# 下载composer.phar curl -sS https://getcomposer.org/installer | php# 把composer.phar移动到环境下让其变成可实行 mv composer.phar /usr/local/bin/composer# 测试composer -V # 输出:Composer version 2.0.7 2020-11-13 17:31:06

4、安装php-microservice-demo

git clone https://github.com/xugj-gits/php-microservice-demo.git

分别进入microservice-1、microservice-2、app目录实行:

composer install

5、安装Docker、Docker Compose

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun# 启动Dockersudo systemctl start docker

6、安装Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composesudo chmod +x /usr/local/bin/docker-composesudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

测试是否安装成功:

docker-compose --version输出:docker-compose version 1.27.4, build unknown

7、启动Docker

在目录/root/php-microservice-demo/下实行:

docker-compose up

输出如下内容表示支配成功:

Starting consul-server-leader ... doneStarting microservice-1 ... doneStarting microservice-2 ... doneStarting app ... done

或者输入:

docker ps

涌现4个容器信息表示支配成功

成功后访问 http://127.0.0.1:8500/ui/

1、访问add接口:

curl http://127.0.0.1:9501/add?a=6\&b=7返回:{"a":6,"b":7,"add":13}

2、访问add接口:

curl http://127.0.0.1:9501/multiply?a=6\&b=7返回:{"a":6,"b":7,"multiply":42}项目源码地址:https://github.com/xugj-gits/php-microservice-demo