kislayphp/gateway
Composer 安装命令:
pie install kislayphp/gateway
包简介
High-performance C++ PHP extension providing API gateway functionality with load balancing and routing for PHP microservices
关键字:
README 文档
README
Edge-only HTTP gateway for KislayPHP services. Route, apply lightweight edge policy, and forward requests without duplicating Core runtime behavior.
Installation
Via PIE (recommended):
pie install kislayphp/gateway:0.0.9
Add to php.ini:
extension=kislayphp_gateway.so
Role In The Stack
Gateway is the edge layer only.
- Gateway: route, optional edge auth, rate limit, circuit break, forward
- Core: request lifecycle, JWT state, tracing, async HTTP, business logic
- Discovery: resolve service name to healthy instance URL
Gateway does not reimplement Core's JWT state model or async HTTP engine.
Quick Start
<?php $gateway = new Kislay\Gateway\Gateway(); $gateway->addRoute('GET', '/health', 'http://127.0.0.1:9008'); $gateway->listen('0.0.0.0', 9009); while (true) { sleep(1); }
Discovery Integration
<?php $gateway = new Kislay\Gateway\Gateway(); $gateway->addServiceRoute('GET', '/api/users', 'user-service'); $gateway->registerService('user-service', [ 'http://127.0.0.1:9001', 'http://127.0.0.1:9002', ]); $gateway->listen('0.0.0.0', 9009); while (true) { sleep(1); }
Service route resolution order is:
- native C++ service registry via
registerService() - PHP
setResolver()callback if configured - Discovery RPC when
KISLAYPHP_RPC_ENABLED=1
For production, prefer registerService() so request threads stay on the native path.
Runtime Behavior
Request forwarding
Gateway forwards:
- method
- path and query string
- headers
- body
AuthorizationX-Request-IDtraceparenttracestate
Gateway generates X-Request-ID only when the incoming request does not provide one.
Gateway preserves upstream X-Forwarded-For chains, forwards the incoming host, and derives X-Forwarded-Proto from the actual client-facing scheme.
Auth alignment
Gateway can do optional edge validation:
- shared bearer token via
KISLAY_GATEWAY_AUTH_TOKEN - JWT signature/expiry validation via
KISLAY_GATEWAY_JWT_SECRET
In both cases, Gateway forwards the original Authorization header downstream and leaves jwt_valid / jwt_payload ownership to Core.
Resilience
Gateway keeps resilience lightweight:
- read timeout on upstream responses
- retry only for idempotent methods and only on pre-response upstream failures
- simple per-upstream circuit breaker with
CLOSED / OPEN / HALF_OPEN - thread-local upstream connection reuse for direct routes
Environment Variables
| Variable | Default | Description |
|---|---|---|
KISLAY_GATEWAY_THREADS |
1 |
CivetWeb worker threads |
KISLAY_GATEWAY_MAX_BODY |
0 |
Max request body bytes, 0 = unlimited |
KISLAY_GATEWAY_AUTH_REQUIRED |
0 |
Enable edge auth checks |
KISLAY_GATEWAY_AUTH_TOKEN |
empty | Expected bearer token in simple auth mode |
KISLAY_GATEWAY_JWT_SECRET |
empty | Enable lightweight HS256 JWT validation |
KISLAY_GATEWAY_AUTH_EXCLUDE |
/health,/ready,/metrics |
Auth-exempt path prefixes |
KISLAY_GATEWAY_READ_TIMEOUT_MS |
10000 |
Upstream response timeout |
KISLAY_GATEWAY_RETRY_IDEMPOTENT |
1 |
Retry count for idempotent methods |
KISLAY_GATEWAY_RATE_LIMIT_ENABLED |
0 |
Enable in-memory rate limiting |
KISLAY_GATEWAY_RATE_LIMIT_REQUESTS |
120 |
Requests per window |
KISLAY_GATEWAY_RATE_LIMIT_WINDOW |
60 |
Rate-limit window in seconds |
KISLAY_GATEWAY_CIRCUIT_BREAKER_ENABLED |
0 |
Enable circuit breaker |
KISLAY_GATEWAY_CB_FAILURE_THRESHOLD |
5 |
Failures before open |
KISLAY_GATEWAY_CB_OPEN_SECONDS |
30 |
Open duration |
Notes
listen()starts the server and returns; keep the process alive explicitly.- Retry is intentionally narrow. Gateway is not a replacement for Core's async execution layer.
registerService()is the recommended production service discovery path. It publishes a native registry snapshot and avoids PHP callbacks on the request path.- On ZTS builds, PHP resolvers are rejected at
listen()time. Use Discovery RPC or direct targets there. - Rate limiting currently uses in-memory storage.
License
Licensed under the Apache License 2.0.
kislayphp/gateway 适用场景与选型建议
kislayphp/gateway 是一款 基于 Shell 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「microservices」 「php-cors」 「php-middleware」 「php-extension」 「php-echo-system」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kislayphp/gateway 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kislayphp/gateway 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kislayphp/gateway 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP CORS (Cross-origin resource sharing) middleware.
The Listener component of the RabbitEvents library.
The Publisher component of the RabbitEvents package.
Laravel common
Laravel support for additional blueprint methods
Laravel comment system
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 7
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2026-02-15