Skip to content

PHP Symfony microservice using gRPC, Hexagonal Architecture, and DDD. Includes RoadRunner setup, clean domain design, and practical examples

Notifications You must be signed in to change notification settings

albertcolom/symfony-grpc

Repository files navigation

PHP Symfony Microservice with gRPC

A modern Symfony 8 microservice with gRPC support powered by RoadRunner, implementing Domain-Driven Design (DDD).

Features

  • Symfony 8.0 - Latest Symfony framework
  • RoadRunner - High-performance PHP application server
  • gRPC - Modern RPC framework for microservices
  • DDD Architecture - Domain-Driven Design structure
  • PHPUnit - Comprehensive unit testing
  • PHPStan - Static analysis for code quality
  • Docker - Containerized development environment

Requirements

  • PHP 8.4+
  • Composer 2.x
  • Docker & Docker Compose
  • grpcurl (for testing gRPC endpoints)

Installation

  1. Clone the repository:
git clone git@github.com:albertcolom/symfony-grpc.git
cd symfony-grpc
  1. Install dependencies:
make install
  1. Build Docker containers:
make build
  1. Start the application:
make up

The gRPC server will be available at localhost:9001.

Project Structure

symfony-grpc/
├── bin/              # Executables (git-ignored, installed via Composer)
├── config/           # Symfony configuration
├── generated/        # Auto-generated gRPC PHP classes
├── proto/            # Protocol Buffer definitions
├── public/           # Public entry points (grpc-worker.php)
├── resources/        # Docker resources
│   └── docker/
│       ├── php-roadrunner/  # PHP + RoadRunner Dockerfile
│       └── protobuf/        # Protocol Buffer generator
├── src/              # Application source code
│   ├── Kernel.php
│   └── User/         # User domain module
│       ├── Application/      # Use cases (Commands, Queries, Handlers)
│       ├── Domain/          # Domain entities and value objects
│       └── Infrastructure/  # Infrastructure layer (GRPC, Persistence)
├── tests/            # PHPUnit tests
│   └── Unit/
└── vendor/           # Composer dependencies (git-ignored)

Available Commands

Run make help to see all available commands:

make help              # Show available commands
make install           # Install Composer dependencies
make build             # Build Docker containers
make up                # Start Docker containers
make down              # Stop Docker containers
make restart           # Restart Docker containers
make logs              # Show container logs (follow mode)
make shell             # Access PHP container shell
make test              # Run PHPUnit tests
make phpstan           # Run PHPStan static analysis
make grpc-test         # Test gRPC service example
make clean             # Clean cache and logs

gRPC Service

User Service

The application includes a User service with the following RPC method:

service UserService {
    rpc GetUser(GetUserRequest) returns (GetUserResponse);
}

Testing gRPC Endpoints

Using Make command:

make grpc-test

Using grpcurl directly:

grpcurl -plaintext \
  -proto proto/user.proto \
  -d '{"id":"f5fb962d-60f6-4096-8bd6-f717249d46fa"}' \
  localhost:9001 \
  user.v1.UserService/GetUser

Expected response:

{
  "id": "f5fb962d-60f6-4096-8bd6-f717249d46fa",
  "email": "john.doe@example.com"
}

Development

Running Tests

Run all unit tests with coverage:

make test

Or directly with PHPUnit:

docker compose exec php-rr vendor/bin/phpunit

Static Analysis

Run PHPStan for code quality checks:

make phpstan

Accessing Container Shell

To access the PHP container:

make shell

Viewing Logs

To follow container logs in real-time:

make logs

Architecture

Domain-Driven Design (DDD)

The project follows DDD principles with a clear separation of concerns:

  • Domain Layer: Core business logic, entities, and value objects
  • Application Layer: Use cases, commands, queries, and handlers
  • Infrastructure Layer: External concerns (gRPC, persistence, etc.)

Protocol Buffers

Protocol Buffer definitions are located in proto/ directory. Generated PHP classes are automatically created in generated/GRPC/ when containers start.

To manually regenerate:

docker compose up proto-gen

Docker Services

  • php-rr: PHP 8.4 with RoadRunner
    • Port 8080: HTTP server
    • Port 6001: RoadRunner metrics
    • Port 9001: gRPC server
  • proto-gen: Protocol Buffer code generator

Configuration

RoadRunner Configuration

RoadRunner is configured via .rr.yaml in the project root.

Symfony Configuration

Application configuration is in config/ directory:

  • services.yaml: Service container configuration
  • packages/: Bundle-specific configurations

Notes

  • The bin/ directory is git-ignored as executables (like bin/console) are installed via Composer
  • PHPUnit is available at vendor/bin/phpunit
  • The application runs in production mode by default in Docker

About

PHP Symfony microservice using gRPC, Hexagonal Architecture, and DDD. Includes RoadRunner setup, clean domain design, and practical examples

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published