https://github.com/rxcod9/laravel-grpc-microservice
A minimalistic setup for building Laravel-based gRPC microservices.
https://github.com/rxcod9/laravel-grpc-microservice
grpc laravel microservice
Last synced: 2 months ago
JSON representation
A minimalistic setup for building Laravel-based gRPC microservices.
- Host: GitHub
- URL: https://github.com/rxcod9/laravel-grpc-microservice
- Owner: rxcod9
- Created: 2025-07-13T06:12:39.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2026-01-08T07:56:17.000Z (3 months ago)
- Last Synced: 2026-01-13T06:15:35.590Z (2 months ago)
- Topics: grpc, laravel, microservice
- Language: PHP
- Homepage:
- Size: 31.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel gRPC Microservice with RoadRunner
A **minimal, production-ready Dockerized Laravel microservice** using **gRPC** and **RoadRunner**. This project enables you to **publish and receive messages** over gRPC with minimal dependenciesβno full Laravel framework required. Logging is directed to Docker stdout for easy container monitoring.
---
## π Features
- PHP 8.4 CLI (alpine or debian)
- gRPC via [`spiral/roadrunner-grpc`](https://github.com/spiral/roadrunner-grpc)
- Logging with `illuminate/log` and `monolog`
- Minimal Laravel components: `support`, `log`, `config`
- `.proto`-driven code generation (Dockerized `protoc`)
- RoadRunner gRPC server & PHP worker
- Docker Compose for local development
---
## π¦ Prerequisites
- Docker & Docker Compose
- `protoc` and PHP plugins (optional if using Docker for codegen)
- `grpcurl` (optional, for testing)
- PHP 8.4 extensions: `grpc`, `protobuf`, `sockets` (included in Dockerfile)
---
## π Project Structure
```
grpc-service/
βββ Dockerfile
βββ docker-compose.yml
βββ .rr.yaml
βββ protos/
β βββ messages.proto
βββ src/
β βββ MessageService.php
β βββ MessageServiceInterface.php
βββ app/
β βββ Grpc/ (auto-generated from .proto)
βββ bootstrap.php
βββ composer.json
βββ composer.lock
βββ README.md
```
---
## π Getting Started
### 1. Clone the Repository
```bash
git clone https://github.com/rxcod9/laravel-grpc-microservice.git
cd laravel-grpc-microservice
```
---
### 2. Generate gRPC PHP Stubs
Use Dockerized `namely/protoc-all`:
```bash
docker run --rm \
-v "$PWD:/workspace" \
-w /workspace namely/protoc-all \
-f protos/messages.proto \
-l php \
-o app/Grpc
```
This generates:
- `MessageServiceInterface.php`
- `MessageServiceClient.php`
- Message request/response classes
---
### 3. Build and Start the Service
```bash
docker-compose up --build
```
This will:
- Install PHP dependencies
- Compile and install gRPC extensions
- Start the RoadRunner worker and gRPC server
---
## π§ͺ Testing the gRPC Server
Example using `grpcurl`:
```bash
grpcurl -plaintext \
-proto protos/messages.proto \
-d '{"topic":"notifications","payload":"{\"event\":\"test\"}"}' \
localhost:50051 messages.MessageService/SendMessage
```
Expected response:
```json
{
"success": true,
"message": "Message processed"
}
```
---
## π Example `.proto` Definition
```proto
syntax = "proto3";
package messages;
service MessageService {
rpc SendMessage (MessageRequest) returns (MessageResponse);
}
message MessageRequest {
string topic = 1;
string payload = 2;
}
message MessageResponse {
bool success = 1;
string message = 2;
}
```
---
## βοΈ RoadRunner Configuration (`.rr.yaml`)
```yaml
rpc:
listen: tcp://0.0.0.0:6001
grpc:
listen: tcp://0.0.0.0:50051
proto:
- protos/messages.proto
workers:
command: "php bootstrap.php"
pool:
numWorkers: 2
```
---
## π Build Notes
- Install `grpc.so` and `protobuf.so` in Docker with `pecl install grpc protobuf`, or copy pre-built `.so` files for faster builds.
- Download the RoadRunner binary in Dockerfile:
```Dockerfile
RUN curl -Ls https://github.com/roadrunner-server/roadrunner/releases/download/v2025.1.2/roadrunner-2025.1.2-linux-amd64.tar.gz \
| tar -xz -C /usr/local/bin rr
```
---
## π¦ Example `composer.json`
```json
{
"require": {
"spiral/roadrunner-grpc": "^2.6",
"illuminate/log": "^12.0",
"illuminate/support": "^12.0",
"illuminate/config": "^12.0",
"illuminate/container": "^12.0",
"illuminate/console": "^12.0",
"monolog/monolog": "^3.0"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Messages\\": "app/Grpc/Messages/",
"GPBMetadata\\": "app/Grpc/GPBMetadata/"
}
}
}
```
---
## π§Ό Clean Shutdown
```bash
docker-compose down
```
---
## π License
MIT
---
## π Support
For issues, bugs, or feature requests, open an issue on GitHub or contact the maintainer.