The PHP microservice framework is "Micro Service Framework For PHP". It is a modern PHP coroutine service framework independently developed by the Camera360 community server team based on Swoole. It is referred to as msf or php-msf. It is Swoole's engineering-level enterprise application framework and has withstood the Camera360 The test of high concurrency and large traffic for hundreds of millions of users. php-msf is led by the Camera360 server team and will be continuously updated and maintained. We also hope that more outstanding Swoole application developers will join. The core design idea of php-msf is to use innovative technical means of coroutine, asynchronous, and parallelism to improve the system's single-machine throughput capability and reduce overall server costs.
Framework Manual (Gitbook): PHP-MSF Development Manual
API Document(Rawgit): class document
Sample DEMO project: PHP-MSF DEMO
Help improve the documentation: https://github.com/pinguo/php-msf-docs, please submit a PR.
PHP-MSF#1 group (QQ): 614054288
$ > php -r " copy('https://raw.githubusercontent.com/pinguo/php-msf-docker/master/installer.php', 'installer.php');include('installer.php'); " && source ~ /.bashrc
installer.php
will check the running environment, automatically create a project template based on your custom configuration, install dependencies with composer, and start the service. If cdn.rawgit.com
is inaccessible, you can directly clone or download php-msf-docker, extract installer.php
, and then run php installer.php
directly
If everything goes well, at the end of the run you will see the following output:
[2017-09-06 16:08:34] Run composer install success
[2017-09-06 16:08:34] Congratulations, all are installed successfully !
[2017-09-06 16:08:34] You can, visit http://127.0.0.1:8990/Welcome for test
_______ ____
________ / /_ ____ ____ ___ _____/ __/
___/ __ / __ / __ _ _____/ __ ` __ / ___/ /_
__/ /_/ / / / / /_/ /_____/ / / / / (__ ) __/
_/ .___/_/ /_/ .___/ /_/ /_/ /_/____/_/
/_/ /_/ Camera360 Open Source TM
[2017-09-06 16:08:34] Swoole Version: 1.9.18
[2017-09-06 16:08:34] PHP Version: 7.1.8
[2017-09-06 16:08:34] Application ENV: docker
[2017-09-06 16:08:34] Listen Addr: 0.0.0.0
[2017-09-06 16:08:34] Listen Port: 8990
Access test:
$ > curl http://127.0.0.1:8990/Welcome
hello world !
Pay attention to the port. If it is not 8990, you need to modify it and then access the test.
├── app // PHP业务代码
│ ├── AppServer.php // 应用server类,可根据需求自定义
│ ├── Controllers // 控制器类目录
│ ├── Lib // 特殊逻辑处理类目录
│ ├── Models // Model类目录
│ ├── Route // 特殊路由规则类目录
│ ├── Tasks // Task类目录
│ └── Views // 视图文件目录
├── build.sh // 构建脚本(拉取docker镜像,启动容器)
├── checkstyle.sh // 代码检查脚本
├── composer.json // composer包依赖配置文件
├── config // 配置目录
├── server.php // server启动脚本
├── console.php // 命令行脚本
├── test // 单元测试目录
The above is a standard application structure based on php-msf. The one-click installation program installer.php will automatically generate a directory. Users can create some custom directories according to their needs. As long as they meet the psr4 standard, they can be automatically loaded.
debug mode
$ > ./server.php start
Daemon mode
$ > ./server.php start -d
Stop service
$ > ./server.php stop
Restart service
$ > ./server.php restart
We have created a Docker image to facilitate Docker users to quickly install the environment and run the PHP-MSF DEMO project. In addition, if you want to preview the effect of modifying the code in the development environment in real time, it is recommended to use Docker for Mac/Windows desktop version.
If you upgrade Docker, it will automatically migrate the original images and containers. Please wait patiently and be sure not to kill the Docker process midway, otherwise it will be difficult to migrate again.
Docker Registry (Alibaba Cloud):
docker pull registry.cn-hangzhou.aliyuncs.com/pinguo-ops/php-msf-docker:latest
docker pull registry-internal.cn-hangzhou.aliyuncs.com/pinguo-ops/php-msf-docker:latest
docker pull registry-vpc.cn-hangzhou.aliyuncs.com/pinguo-ops/php-msf-docker:latest
docker pull pinguoops/php-msf-docker
We focus on building a stable and high-performance purely asynchronous microservice framework based on HTTP, as an alternative technology stack to nginx+php-fpm to realize the microservices of the architecture; and Tcp/WebSocket Server will be supported as a plug-in, or as other independent open source project.
For small teams or business systems, we recommend using the traditional nginx+php-fpm technology stack. There is no bottleneck in terms of cost and performance, and there is no need to introduce a new technology stack.
For large and medium-sized teams or business systems that are at an important stage of service governance or service-oriented evolution, php-msf is one of the options.
For large PHP application clusters, if you want to significantly save server costs and improve service performance, php-msf is one of the options.
For aggregation services, such as large website homepages, if you want to integrate data through server-side aggregation content, php-msf is one of the options.
The recommended installation method is to add the dependency pinguo/php-msf
by editing the project composer.json
{
"require" : {
"pinguo/php-msf" : " >=3.0.0 "
},
"minimum-stability" : " dev "
}
"minimum-stability": "dev"
configuration option must be added because the log component depends on "monolog/monolog": "2.0.x-dev"
, and monolog/monolog
does not have a 2.0 release package, but we have already done so in the production environment Verify its stability.
php-msf has withstood the high traffic and high concurrency of Camera360 community service, and its stability has been fully verified. Stability is the most important issue that we spend a lot of time and energy to solve, and it is the most important principle of the three principles.
The single-machine processing capacity of IO-intensive business is increased by 5-10 times. This is real data obtained in the production environment. For example, an aggregation service in the Camera360 community requires 40 servers to withstand the traffic during peak traffic. After reconstruction with php-msf Only 4 servers with the same configuration are needed to withstand all traffic.
Due to Swoole's complex process model, which is divided into synchronous blocking and asynchronous non-blocking, when running the same code logic, the calling method and passing parameters may be inconsistent, which directly increases the learning cost. In order to shield the low-level differences, we , a lot of work has been done, and the only difference between the implementation and the traditional MVC framework is the addition of the "yield" keyword. We refer to some code practices of the Yii2 framework, and we expect to seamlessly switch from Yii2 development.
The above three principles are the basis for voting or merging code when we implement new features and functions. Any PR that affects these principles will also be rejected.
At present, there are several PHP open source projects in the community that support coroutines. Most of them use Generator+Yield to implement them. However, subtle differences in implementation will lead to very different performance. We should realize that coroutines can run asynchronous logic in a synchronous code writing method. , so coroutine The performance of the scheduler must be high enough. The coroutine scheduling performance of php-msf is 80% of the native asynchronous callback method. That is to say, a certain API using the native asynchronous callback writing method has a QPS of 10,000. Through the php-msf coroutine scheduler The scheduling QPS is 8000.
At present, php-msf is still in its infancy. We have spent a lot of time and energy solving stability, high performance, and memory problems, because we believe that the "cornerstone" is the most basic guarantee for a "high-rise building." Only if the foundation is solid, Only in this way can the "building" be built "higher". Version 3.0 is the initial version of our open source and an important step for us. Next, our focus will be on polishing the distributed microservices framework.
In addition, because it is based on the PHP resident process and directly parses HTTP or TCP requests, this is the most important support for service. Based on this, we can do many ideas that we did not dare to realize before. In short, there is a lot of room for imagination.
php-msf was originally developed based on SwooleDistributed-1.7.x, and in this open source version, the connection pool mainly uses SD implementation. Since the positioning of our frameworks, the business scenarios they solve, stability requirements, and coding styles are all very different, we decided to independently develop microservice frameworks. Each framework has its own characteristics and advantages. Choose the one that suits your company and business scenarios. The framework is the most important, and I would like to thank White Cat for this. In addition, during the development of the php-msf framework and production environment applications, I encountered many underlying problems, but they were all solved one by one. The most important thing that can solve these problems is the Swoole open source project I would like to express my deep gratitude to the founder Han Tianfeng-Rango for his strong support.
GNU General Public License, version 2 see https://www.gnu.org/licenses/gpl-2.0.html