spiral/grpc-client
Lightweight, extensible PHP gRPC client with a Guzzle-like API. Supports standalone use or Spiral integration, configurable via DTOs, includes interceptors (timeouts, retries), and rich exceptions for error handling. Requires the PHP gRPC extension.
Pros:
GrpcClient can integrate with Laravel via service providers or manual DI, avoiding framework lock-in.Cons:
guzzlehttp/guzzle.grpc PECL extension must be installed, adding devops friction (e.g., Dockerfiles, CI/CD)..proto files (generated via protoc), which may not align with existing REST/Laravel APIs.Cache::remember for gRPC calls).Http::post() for internal APIs with gRPC (e.g., calling a Java payment service).RetryInterceptor) before adopting streaming..proto files for gRPC endpoints..proto files and client updates.TlsConfig may conflict with Laravel’s HTTPS defaults.UNAVAILABLE) differ from Laravel’s HTTP exceptions, requiring custom exception mappers.grpc extension is not bundled with PHP core, increasing CI/CD and deployment complexity..proto files may face schema design and tooling challenges (e.g., protoc setup).auth:api)?Log::channel('single') vs. gRPC metadata)?RetryInterceptor) or will custom logic be needed?GrpcClient to Laravel’s container (e.g., app['grpc.client']).Grpc facade for fluent calls (e.g., Grpc::service(MailSender::class)->send()).Http client to fall back to gRPC for internal endpoints.OrderCreated) and dispatch Laravel events..proto files for internal services (e.g., order_service.proto).protoc --php_out=. --grpc_out=. order_service.proto.GrpcClient with ServiceConfig for each protobuf interface.RetryInterceptor with exponential backoff for transient failures (e.g., payment gateways).SetTimeoutInterceptor::createConfig(2_000)).Spiral\Grpc\Client\Interceptor\Helper).| Phase | Action | Laravel Integration | Risk Mitigation |
|---|---|---|---|
| Assessment | Audit top 5 REST endpoints for internal calls (e.g., payments, auth). | Benchmark gRPC vs. REST latency. | Start with non-critical endpoints. |
| Pilot | Replace 1 REST call with gRPC (e.g., inventory check). | Use GrpcClient standalone, log performance. |
Monitor error rates and latency. |
| Interceptors | Add retries/timeouts to pilot service. | Create a Laravel middleware to inject gRPC metadata (e.g., auth tokens). | Test with chaos engineering (e.g., kill servers). |
| Protobuf | Migrate 1 service to .proto + gRPC. |
Generate Laravel models from protobuf messages. | Use versioned .proto files. |
| Full Adoption | Roll out gRPC for all internal APIs. | Deprecate REST endpoints, update docs. | Feature flag gRPC endpoints. |
protoc + protoc-gen-php-grpc (not bundled with Laravel).spiral/grpc-tools or custom scripts to generate PHP classes.GrpcClient in AppServiceProvider:
$this->app->singleton(GrpcClient::class, function () {
return GrpcClient::create('order-service:9001')
->withInterceptors([SetTimeoutInterceptor::createConfig(2_000)]);
});
RpcException) to Laravel’s HttpException:
try {
$response = $grpcClient->service(OrderService::class)->getOrder($ctx, $request);
} catch (RpcException $e) {
throw new HttpException(500, $e->getMessage());
}
grpc_health_v1.Health) in PHPUnit.HandlerInterface mocks.1
How can I help you explore Laravel packages today?