There are two articles related to that repo:
- How to expose Protocol Buffers over REST. Detailed description can be found here: Exposing Microservices over REST Protocol Buffers
- How to create gRPC service with Spring Boot and integrate gRPC client with Spring Cloud discovery. Detailed description can be found here: Introduction to gRPC with Spring Boot. This version has been replaced in repository with a new Spring Boot GRPC starter provided by Spring team. To check it visit https://github.com/piomin/sample-microservices-protobuf/releases/tag/v1.1.
- Spring gRPC with Spring Boot. Detailed description can be found here: Spring gRPC in Spring Boot.
This project demonstrates a microservices architecture using Spring Boot with two distinct inter-service communication approaches: REST with Protocol Buffers and gRPC. The same business logic (Account and Customer management) is implemented in parallel for hands-on comparison.
The repository contains 5 Spring Boot applications and one service discovery component:
- discovery-service
- Port: 8761
- Role: Netflix Eureka server for service registration and discovery
- Dashboard URL: http://localhost:8761
-
account-service
- Port: 2222 (configurable via
PORTenv var) - Communication: HTTP + Protobuf
- Proto file:
account-service/src/main/proto/account.proto
- Port: 2222 (configurable via
-
customer-service
- Port: 3333 (configurable via
PORTenv var) - Communication: HTTP + Protobuf
- Proto file:
customer-service/src/main/proto/customer.proto
- Port: 3333 (configurable via
-
account-service-grpc
- HTTP Port: 8081
- gRPC Port: 9091
- Library:
grpc-server-spring-boot-starter - Services:
FindByNumber,FindByCustomer,FindAll,AddAccount
-
customer-service-grpc
- HTTP Port: 8081
- gRPC Port: 9091
- Services:
FindByPesel,FindById,FindAll,AddCustomer - Discovery: Registers with Eureka
- Java 21
- Spring Boot 3.4.5
- Spring Cloud 2024.0.1 (Eureka, LoadBalancer, OpenFeign)
- Protocol Buffers 4.31.1
- gRPC 1.63.0
- Maven 3.8+
- Docker & Docker Compose (optional)
Client → API Gateway → Service Discovery → Customer Service (HTTP:3333, Protobuf) → Account Service (HTTP:2222, Protobuf)
Client → gRPC Client → Service Discovery → Customer Service gRPC (9091) → Account Service gRPC (9091)
- Start discovery-service (8761).
- Each microservice registers with Eureka.
- Clients and services discover each other by querying Eureka.
- Spring Cloud LoadBalancer handles service-side load balancing.
- Health checks via Spring Boot Actuator endpoints.
- Java 21+ (
java --version) - Maven 3.8+ (
mvn --version) - Git 2+ (
git --version) - Optional: Docker & Docker Compose
- Clone the repo:
git clone https://github.com/piomin/sample-microservices-protobuf.git cd sample-microservices-protobuf - Compile (includes Protobuf & gRPC codegen):
mvn clean compile
- Package JARs (skip tests for speed):
mvn clean package -DskipTests
Important: Always start in this sequence to ensure proper registration.
- Discovery Service
cd discovery-service mvn spring-boot:run - REST + Protobuf Approach
OR
# In parallel terminals: cd account-service && mvn spring-boot:run cd customer-service && mvn spring-boot:run
gRPC Approachcd account-service-grpc && mvn spring-boot:run cd customer-service-grpc && mvn spring-boot:run
# Discovery
java -jar discovery-service/target/discovery-service-*.jar
# Choose one approach per microservice:
java -jar account-service/target/*.jar
java -jar customer-service/target/*.jar
# OR for gRPC:
java -jar account-service-grpc/target/*.jar
java -jar customer-service-grpc/target/*.jar- Eureka Dashboard: http://localhost:8761
- Health Checks:
curl http://localhost:8761/actuator/health curl http://localhost:2222/actuator/health curl http://localhost:3333/actuator/health curl http://localhost:9091/actuator/health
- REST API Example:
curl -H "Accept: application/json" http://localhost:3333/customers - gRPC Example (using grpcurl):
grpcurl -plaintext -d '{}' localhost:9091 model.CustomersService/FindAll
| Service | HTTP Port | gRPC Port | Env Var | Protocol |
|---|---|---|---|---|
| discovery-service | 8761 | – | – | HTTP |
| account-service | 2222 | – | PORT | HTTP + Protobuf |
| customer-service | 3333 | – | PORT | HTTP + Protobuf |
| account-service-grpc | 8081 | 9091 | – | gRPC |
| customer-service-grpc | 8081 | 9091 | – | gRPC |
- Port Conflicts:
lsof -ti:8761 | xargs kill -9
- Protobuf Errors:
mvn clean compile -X
- Increase JVM Memory:
export MAVEN_OPTS="-Xmx2G -Xms1G" mvn spring-boot:run
- Hot Reloading: Add DevTools dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
- gRPC Testing: Use
grpcurlor BloomRPC for introspection.