This repository demonstrates how to build microservices using gRPC for communication between a Go backend service and a Node.js/Express frontend service.
┌─────────────────┐
│ │
│ Client/User │
│ │
└────────┬────────┘
│
│ HTTP
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ ┌────────────────────────────┐ ┌────────────────────────────┐ │
│ │ │ │ │ │
│ │ Express.js Service │ gRPC │ Go Service │ │
│ │ (Node.js, port 3000) ◄──────────► (Golang, port 50051) │ │
│ │ │ │ │ │
│ └────────────────────────────┘ └────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
Docker Compose Network
- Client Request: External clients send HTTP requests to the Express.js service on port 3000
- gRPC Client Call: The Express service acts as a gRPC client, serializing the request using Protocol Buffers
- Service Communication: The Express service communicates with the Go service via gRPC on port 50051
- Processing: The Go service processes the request and returns a gRPC response
- HTTP Response: The Express service deserializes the gRPC response and returns an HTTP response to the client
- proto/: Contains Protocol Buffer definitions (.proto files) that define the service interface
- go-service/: Go implementation of the gRPC server
- grpcgoexpress/: Auto-generated gRPC code for Go
- api/: API handlers and business logic
- models/: Data models
- express-service/: Node.js/Express implementation of the gRPC client
- src/generated/: Auto-generated gRPC code for Node.js
- src/controllers/: Express controllers
- src/routes/: Express route definitions
- docker-compose.yml: Docker Compose configuration for running both services
- Docker and Docker Compose
- Go 1.18+ (for local development)
- Node.js 16+ (for local development)
- Protocol Buffers compiler (protoc)
-
Build and start the services:
docker-compose up -d
-
Check if the services are running:
docker-compose ps
-
Test the API:
curl "http://localhost:3000/data?query=hello"
-
Install dependencies:
cd go-service go mod tidy
-
Run the service:
go run main.go
-
Install dependencies:
cd express-service npm install
-
Run the service:
npm start
protoc --go_out=. --go-grpc_out=. proto/service.proto
npx grpc_tools_node_protoc \
--js_out=import_style=commonjs,binary:./express-service/src/generated \
--grpc_out=grpc_js:./express-service/src/generated \
--proto_path=./proto \
./proto/service.proto
GET /data?query=<your_query>
- Returns data from the Go service via gRPC
- Example:
curl "http://localhost:3000/data?query=hello"
The service is defined in proto/service.proto
:
service GreetingService {
rpc GetData (RequestMessage) returns (ResponseMessage);
}
message RequestMessage {
string query = 1;
}
message ResponseMessage {
string data = 1;
}
- gRPC: Used for efficient, typed communication between microservices
- Protocol Buffers: Service contract definition and efficient serialization
- Docker & Docker Compose: Containerization and orchestration
- Go: High-performance backend service implementation
- Express.js: Flexible frontend service exposing HTTP endpoints
- Strong Typing: Compile-time type checking for service contracts
- Performance: Efficient binary serialization and HTTP/2
- Language Agnostic: Services implemented in different languages
- Scalability: Services can be scaled independently
- Developer Experience: Auto-generated client/server code
- Authentication: Add JWT or other authentication mechanisms
- Service Discovery: Integrate with Consul or etcd
- Streaming: Implement streaming APIs for real-time data
- Monitoring: Add Prometheus metrics and Grafana dashboards
- CI/CD: Set up automated testing and deployment
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
This project is licensed under the MIT License - see LICENSE for details.