Hyperf Framework

repository·master·Indexed 27 days ago

https://github.com/hyperf/hyperf

A high-performance, flexible PHP CLI framework designed for microservices. It leverages coroutines (Swoole 5.0+ or Swow 1.4+) and AOP to handle massive traffic. Hyperf includes a wide array of coroutine-compatible components for RPC (gRPC, JSON RPC), storage (MySQL, Redis, Elasticsearch), service discovery (Consul, ETCD, Apollo), and messaging (RabbitMQ, NSQ, Nats). It also provides specialized tools like Nano, GoTask, Jet, and Box, as well as libraries for DAG task orchestration, Nacos SDK, RPC multiplexing, and Socket.io server integration.

Tokens
199.8K
Snippets
567
Records
1.3K
Agent score
87%

What's inside Hyperf

  1. Overview of Hyperf Framework

    master

    Hyperf is a high-performance, highly flexible progressive PHP coroutine framework. It features a built-in coroutine server and a wide array of common components. The framework is designed for high concurrency and extreme flexibility, with most components being replaceable and reusable due to its strong dependency injection design and adherence to PSR standards.

    Key capabilities include:

    • High Performance: Optimized for Swoole and Swow coroutines.
    • Microservices Ready: Supports WebSocket, JSON RPC, gRPC, and various service discovery/configuration tools (Consul, ETCD, Apollo, etc.).
    • Rich Component Library: Includes coroutine-compatible MySQL/Redis clients, Eloquent ORM, AMQP, Nats, RabbitMQ, and more.
    • Core Features: PSR-11 DI Container, AOP (Aspect Oriented Programming), PSR-15 Middleware, PSR-14 Event Manager, Crontab tasks, and Validation.
  2. Overview of Hyperf Dependency Injection

    master

    Hyperf uses hyperf/di as its default dependency injection management container. It is designed specifically for long-lived applications (like Swoole or Swow-based servers) and provides advanced features including:

    • Automatic Injection: Manages class dependencies automatically.
    • Annotation & Annotation Injection: Supports injecting dependencies via annotations.
    • AOP (Aspect-Oriented Programming): Provides powerful AOP capabilities to intercept and modify class behavior.
  3. Configure an external Configuration Center

    master
    Hyperf supports distributed configuration centers. By default, it provides support for Apollo (by Ctrip) via the hyperf/config-apollo component. Refer to the Configuration Center guide for specific implementation details.
  4. Hyperf Ecosystem Components

    master

    The Hyperf ecosystem includes several specialized libraries:

    • Nano: A zero-configuration, minimal Hyperf distribution that allows building an application with just a single PHP file.
    • GoTask: A library that starts a Go process as a Swoole main process sidecar, using IPC to handle tasks (similar to Swoole TaskWorker but using Go).
    • Jet: A unified model RPC client with built-in JSONRPC support, compatible with PHP-FPM, Swoole, and Hyperf.
    • Box: A tool to improve the programming experience, manage PHP environments/dependencies, package Hyperf apps as binaries, and provide reverse proxy services for deployment.
  5. Understand Hyperf Coroutine Concepts

    master

    Hyperf is built on Swoole 5 coroutines to provide high performance through an asynchronous non-blocking system.

    Key Concepts

    • Coroutine vs. PHP-FPM: Unlike the traditional PHP-FPM model where one worker process handles one request synchronously (blocking the process during I/O), Hyperf uses coroutines to allow a single worker process to handle multiple requests concurrently. When one coroutine encounters an I/O operation (like a MySQL query), the scheduler automatically switches to another coroutine instead of blocking the process.
    • Lightweight Threads: Coroutines are lightweight threads managed in user mode by a scheduler rather than the OS kernel.
    • Collaborative vs. Preemptive: Unlike standard threads which are preemptive (managed by the OS), Swoole coroutines are collaborative. Switching occurs implicitly during I/O operations or explicit code switches.
    • Single-threaded Execution: Within a single process, only one coroutine runs at any given time. This eliminates the need for complex synchronization locks typically required in multi-threaded programming.
  6. Explore the Hyperf Ecosystem

    master

    Hyperf provides several specialized tools to extend its capabilities:

    • Nano: A minimal, zero-config distribution for building applications with a single PHP file.
    • GoTask: A library to spawn Go processes as Swoole sidecars, using bi-directional IPC to offload heavy tasks to Go.
    • Jet: A unified RPC Client with built-in JSONRPC protocol that works in both PHP-FPM and Swoole/Hyperf environments.
    • Box: A tool for managing PHP environments, dependencies, packaging Hyperf applications as binary programs, and providing reverse proxy services.