一、Hyperf 的定位

如果说 EasySwoole 是「Swoole 的极简封装」,那 Hyperf 就是「PHP 版的 Spring Boot」。

它的核心理念:

  • 基于 Swoole 协程,常驻内存 + 协程 IO,性能压榨到位
  • 注解驱动:路由、依赖注入、AOP、限流、缓存全部走注解
  • DI 容器 + AOP:写业务时不用关心实例化和拦截
  • 微服务套件齐全:服务注册、配置中心、链路追踪、熔断限流、RPC,开箱即用

适合中大型团队做微服务架构 —— 学习曲线陡,但工程化收益大。


二、核心组件一览

组件对标用途
hyperf/diSpring DI依赖注入 + AOP
hyperf/databaseEloquentORM(基于 Laravel illuminate/database)
hyperf/grpcgRPC微服务间通信
hyperf/json-rpc-轻量 RPC
hyperf/config-nacosSpring Cloud Config配置中心
hyperf/service-governanceEureka/Nacos服务注册发现
hyperf/circuit-breakerHystrix熔断
hyperf/tracerZipkin/Jaeger链路追踪

三、快速开始

1
2
3
composer create-project hyperf/hyperf-skeleton my-app
cd my-app
php bin/hyperf.php start

控制器(注解驱动路由):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Contract\RequestInterface;

#[AutoController(prefix: "/user")]
class UserController
{
#[GetMapping(path: "info")]
public function info(RequestInterface $request): array
{
return ['id' => $request->input('id'), 'name' => 'Mike'];
}
}

访问 GET /user/info?id=1 即返回 JSON。


四、依赖注入 + AOP

构造器注入

1
2
3
4
5
6
7
class OrderService
{
public function __construct(
private UserService $userService,
private LoggerInterface $logger,
) {}
}

注解切面

1
2
3
4
5
6
7
8
9
10
11
12
13
#[Aspect]
class LogAspect extends AbstractAspect
{
public array $annotations = [Log::class];

public function process(ProceedingJoinPoint $point)
{
$start = microtime(true);
$result = $point->process();
// 记录方法执行时间
return $result;
}
}

业务方法只要打上 #[Log],自动被切面包裹 —— 比手动 try/finally 优雅得多。


五、协程客户端示例

1
2
3
4
5
6
7
8
9
10
11
use Hyperf\Utils\Parallel;
use Hyperf\Guzzle\ClientFactory;

$parallel = new Parallel();
foreach (['http://a', 'http://b', 'http://c'] as $url) {
$parallel->add(function () use ($url) {
return $this->container->get(ClientFactory::class)
->create()->get($url)->getBody()->getContents();
});
}
$results = $parallel->wait(); // 三个请求并发,总耗时取最慢

六、踩坑笔记

现象解法
注解扫描慢启动 5s+生产开启 scan_cacheable=true,预生成代理类
改代码不生效常驻内存hyperf/watcher 自动热重启
协程下用同步函数整个 Worker 阻塞Hyperf\Guzzle、协程版 Redis/MySQL,禁用 curl_*(除非配 hook)
DI 单例污染上个请求数据串到下个Context::set/get 存请求级数据
AOP 不生效切面没拦截到检查 proxy_class_dir 是否被打包;继承/接口方法可能不被代理
MySQL 连接耗尽“Too many connections”配置 pool.max_connections,业务用完归还

七、什么时候选 Hyperf

:团队 5+ 人、做微服务、有 Java 背景想要类似工程化体验、QPS 要求 1k+
别选:单人项目、CRUD 后台、对注解 + DI 不熟 —— 用 Webman 或 Laravel 更快


参考