PHP微服务实践 -- 手把手教你搭建PHP微服务

2020-11-22T11:30:00

简介

由于公司项目的发展,现在有SaaS架构的B2B、B2C项目,且项目提供的功能多而杂;并且有大部分功能是重复的。虽然,并发不大,但是从维护和开发的角度讲,开发和维护一套的成本要小很多;所以自己再业余时间探索PHP的微服务架构。

基础知识普及:

1、hyerf:https://hyperf.wiki/2.0/#/
2、consul:https://www.consul.io/
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 php*
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -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-pear
yum install php72w-devel
pecl 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

# 启动Docker
sudo systemctl start docker

安装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-compose

sudo chmod +x /usr/local/bin/docker-compose

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

测试是否安装成功:

docker-compose --version
输出:docker-compose version 1.27.4, build unknown
6.启动Docker

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

docker-compose up

输出如下内容表示部署成功:

Starting consul-server-leader ... done
Starting microservice-1       ... done
Starting microservice-2       ... done
Starting app                  ... done
Attaching to consul-server-leader, microservice-1, microservice-2, app
consul-server-leader    | ==> Found address '172.18.0.2' for interface 'eth0', setting bind option...
consul-server-leader    | ==> Starting Consul agent...
consul-server-leader    |            Version: '1.8.5'
consul-server-leader    |            Node ID: 'e8e8c49a-1ba9-9103-ae28-84fc4860455f'
consul-server-leader    |          Node name: 'consul-server-leader'
consul-server-leader    |         Datacenter: 'dc1' (Segment: '<all>')
consul-server-leader    |             Server: true (Bootstrap: true)
consul-server-leader    |        Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, gRPC: -1, DNS: 8600)
consul-server-leader    |       Cluster Addr: 172.18.0.2 (LAN: 8301, WAN: 8302)
consul-server-leader    |            Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
consul-server-leader    |
consul-server-leader    | ==> Log data will now stream in as it occurs:
consul-server-leader    |
consul-server-leader    |     2020-11-20T09:33:51.554Z [WARN]  agent: bootstrap = true: do not enable unless necessary
consul-server-leader    |     2020-11-20T09:33:51.568Z [WARN]  agent.auto_config: bootstrap = true: do not enable unless necessary
consul-server-leader    |     2020-11-20T09:33:51.574Z [INFO]  agent.server.raft: initial configuration: index=1 servers="[{Suffrage:Voter ID:e8e8c49a-1ba9-9103-ae28-84fc4860455f Address:172.18.0.2:8300}]"
consul-server-leader    |     2020-11-20T09:33:51.575Z [INFO]  agent.server.serf.wan: serf: EventMemberJoin: consul-server-leader.dc1 172.18.0.2
consul-server-leader    |     2020-11-20T09:33:51.576Z [INFO]  agent.server.serf.lan: serf: EventMemberJoin: consul-server-leader 172.18.0.2
consul-server-leader    |     2020-11-20T09:33:51.576Z [INFO]  agent.router: Initializing LAN area manager
consul-server-leader    |     2020-11-20T09:33:51.578Z [INFO]  agent: Started DNS server: address=0.0.0.0:8600 network=udp
consul-server-leader    |     2020-11-20T09:33:51.578Z [INFO]  agent.server.raft: entering follower state: follower="Node at 172.18.0.2:8300 [Follower]" leader=
consul-server-leader    |     2020-11-20T09:33:51.579Z [WARN]  agent.server.serf.wan: serf: Failed to re-join any previously known node
consul-server-leader    |     2020-11-20T09:33:51.579Z [WARN]  agent.server.serf.lan: serf: Failed to re-join any previously known node
consul-server-leader    |     2020-11-20T09:33:51.579Z [INFO]  agent.server: Adding LAN server: server="consul-server-leader (Addr: tcp/172.18.0.2:8300) (DC: dc1)"
consul-server-leader    |     2020-11-20T09:33:51.579Z [INFO]  agent.server: Handled event for server in area: event=member-join server=consul-server-leader.dc1 area=wan
consul-server-leader    |     2020-11-20T09:33:51.579Z [INFO]  agent: Started DNS server: address=0.0.0.0:8600 network=tcp
consul-server-leader    |     2020-11-20T09:33:51.579Z [INFO]  agent: Started HTTP server: address=[::]:8500 network=tcp
consul-server-leader    |     2020-11-20T09:33:51.579Z [INFO]  agent: started state syncer
consul-server-leader    | ==> Consul agent running!
consul-server-leader    |     2020-11-20T09:33:52.670Z [WARN]  agent: Check is now critical: check=service:AdditionService-2
consul-server-leader    |     2020-11-20T09:33:52.670Z [WARN]  agent: Check is now critical: check=service:MultiplicationService-1
consul-server-leader    |     2020-11-20T09:33:52.670Z [WARN]  agent: Check is now critical: check=service:AdditionService-1
microservice-1          | Scanning ...
microservice-2          | Scanning ...
consul-server-leader    |     2020-11-20T09:33:53.039Z [WARN]  agent: Check is now critical: check=service:AdditionService-0
consul-server-leader    |     2020-11-20T09:33:53.039Z [WARN]  agent: Check is now critical: check=service:MultiplicationService-0
app                     | Scanning ...
consul-server-leader    |     2020-11-20T09:33:53.673Z [WARN]  agent: Check is now critical: check=service:MultiplicationService-1
consul-server-leader    |     2020-11-20T09:33:53.673Z [WARN]  agent: Check is now critical: check=service:AdditionService-2
consul-server-leader    |     2020-11-20T09:33:53.673Z [WARN]  agent: Check is now critical: check=service:AdditionService-1
consul-server-leader    |     2020-11-20T09:33:54.044Z [WARN]  agent: Check is now critical: check=service:AdditionService-0
consul-server-leader    |     2020-11-20T09:33:54.044Z [WARN]  agent: Check is now critical: check=service:MultiplicationService-0
microservice-1          | Scan completed.
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Di\Listener\BootApplicationListener listener.
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\JsonRpc\Listener\RegisterProtocolListener listener.
microservice-1          | [DEBUG] Event Hyperf\RpcServer\Event\AfterPathRegister handled by Hyperf\JsonRpc\Listener\RegisterServiceListener listener.
microservice-1          | [DEBUG] Event Hyperf\RpcServer\Event\AfterPathRegister handled by Hyperf\JsonRpc\Listener\RegisterServiceListener listener.
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
microservice-2          | Scan completed.
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Di\Listener\BootApplicationListener listener.
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\JsonRpc\Listener\RegisterProtocolListener listener.
microservice-1          | [2020-11-20 17:33:54 @1.0]    WARNING    swoole_file_put_contents(:711): open(/var/www/runtime/hyperf.pid) failed, Error: No such file or directory[2]
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
microservice-2          | [DEBUG] Event Hyperf\RpcServer\Event\AfterPathRegister handled by Hyperf\JsonRpc\Listener\RegisterServiceListener listener.
microservice-2          | [DEBUG] Event Hyperf\RpcServer\Event\AfterPathRegister handled by Hyperf\JsonRpc\Listener\RegisterServiceListener listener.
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
microservice-2          | [2020-11-20 17:33:54 @1.0]    WARNING    swoole_file_put_contents(:711): open(/var/www/runtime/hyperf.pid) failed, Error: No such file or directory[2]
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
microservice-1          | [DEBUG] [service-governance] Service AdditionService[/addition/add] is registering to the consul.
microservice-1          | [INFO] [service-governance] Service AdditionService[/addition/add] has been already registered to the consul.
microservice-1          | [DEBUG] [service-governance] Service MultiplicationService[/multiplication/multiply] is registering to the consul.
microservice-1          | [INFO] [service-governance] Service MultiplicationService[/multiplication/multiply] has been already registered to the consul.
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\MainWorkerStart handled by Hyperf\ServiceGovernance\Listener\RegisterServiceListener listener.
microservice-1          | [INFO] Worker#0 started.
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
microservice-1          | [INFO] HTTP Server listening at 0.0.0.0:9502
microservice-1          | [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
microservice-2          | [DEBUG] [service-governance] Service AdditionService[/addition/add] is registering to the consul.
microservice-2          | [INFO] [service-governance] Service AdditionService[/addition/add] has been already registered to the consul.
microservice-2          | [DEBUG] [service-governance] Service MultiplicationService[/multiplication/multiply] is registering to the consul.
microservice-2          | [INFO] [service-governance] Service MultiplicationService[/multiplication/multiply] has been already registered to the consul.
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\MainWorkerStart handled by Hyperf\ServiceGovernance\Listener\RegisterServiceListener listener.
microservice-2          | [INFO] Worker#0 started.
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
microservice-2          | [INFO] HTTP Server listening at 0.0.0.0:9502
microservice-2          | [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
app                     | Scan completed.
app                     | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Di\Listener\BootApplicationListener listener.
app                     | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
app                     | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\RpcClient\Listener\AddConsumerDefinitionListener listener.
app                     | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
app                     | [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\JsonRpc\Listener\RegisterProtocolListener listener.
app                     | [DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
app                     | [2020-11-20 17:33:54 @1.0]    WARNING    swoole_file_put_contents(:711): open(/var/www/runtime/hyperf.pid) failed, Error: No such file or directory[2]
app                     | [DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
app                     | [DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
app                     | [DEBUG] Event Hyperf\Framework\Event\MainWorkerStart handled by Hyperf\ServiceGovernance\Listener\RegisterServiceListener listener.
app                     | [INFO] Worker#0 started.
app                     | [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
app                     | [INFO] HTTP Server listening at 0.0.0.0:9501
app                     | [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
consul-server-leader    |     2020-11-20T09:33:58.808Z [ERROR] agent.anti_entropy: failed to sync remote state: error="No cluster leader"
consul-server-leader    |     2020-11-20T09:33:59.312Z [WARN]  agent.server.raft: heartbeat timeout reached, starting election: last-leader=
consul-server-leader    |     2020-11-20T09:33:59.312Z [INFO]  agent.server.raft: entering candidate state: node="Node at 172.18.0.2:8300 [Candidate]" term=3
consul-server-leader    |     2020-11-20T09:33:59.316Z [INFO]  agent.server.raft: election won: tally=1
consul-server-leader    |     2020-11-20T09:33:59.316Z [INFO]  agent.server.raft: entering leader state: leader="Node at 172.18.0.2:8300 [Leader]"
consul-server-leader    |     2020-11-20T09:33:59.317Z [INFO]  agent.server: cluster leadership acquired
consul-server-leader    |     2020-11-20T09:33:59.317Z [INFO]  agent.server: New leader elected: payload=consul-server-leader
consul-server-leader    |     2020-11-20T09:33:59.323Z [INFO]  agent.leader: started routine: routine="federation state anti-entropy"
consul-server-leader    |     2020-11-20T09:33:59.323Z [INFO]  agent.leader: started routine: routine="federation state pruning"
consul-server-leader    |     2020-11-20T09:33:59.323Z [INFO]  agent.leader: started routine: routine="intermediate cert renew watch"
consul-server-leader    |     2020-11-20T09:33:59.323Z [INFO]  agent.leader: started routine: routine="CA root pruning"
consul-server-leader    |     2020-11-20T09:34:00.130Z [INFO]  agent: Synced node info
consul-server-leader    |     2020-11-20T09:34:00.135Z [INFO]  agent: Synced check: check=service:MultiplicationService-0
consul-server-leader    |     2020-11-20T09:34:00.138Z [INFO]  agent: Synced check: check=service:AdditionService-0

或者输入:

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

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »