This project demonstrates a microservices architecture utilizing gRPC for inter-service communication and GraphQL as the API gateway. It includes services for account management, product catalog, and order processing. The architecture is designed to be modular and scalable, making it suitable for various applications.
- Go: The primary programming language for building the microservices.
- gRPC: A high-performance RPC framework for communication between services.
- GraphQL: A query language for APIs, providing a more efficient and flexible alternative to REST.
- Elasticsearch: A distributed search and analytics engine used for the catalog service.
- PostgreSQL: A relational database used for account and order services.
- Docker: For containerization and orchestration of services.
The project consists of the following main components:
- Account Service: Manages user accounts and authentication.
- Catalog Service: Handles product listings and search functionalities.
- Order Service: Manages order processing and transactions.
- GraphQL API Gateway: Acts as the entry point for client requests, routing them to the appropriate services.
Each service has its own database:
- Account and Order services use PostgreSQL.
- Catalog service uses Elasticsearch.
All services file structure are similar.
.
├── account.proto
├── app.dockerfile
├── client.go
├── cmd
│ └── account
│ └── main.go
├── db.dockerfile
├── pb
│ ├── account.pb.go
│ └── account_grpc.pb.go
├── repository.go
├── server.go
├── service.go
└── up.sql
-
Clone the repository:
git clone https://github.com/haroonalbar/go-grpc-graphql-microservices cd go-grpc-graphql-microservices
-
Start the services using Docker Compose:
docker-compose up -d --build
-
Access the GraphQL playground at
http://localhost:8080/playground
.
Or access the demo htmx frontend at
http://localhost:8080
.
The GraphQL API provides a unified interface to interact with all the microservices.
query {
accounts {
id
name
}
}
mutation {
createAccount(account: {name: "New Account"}) {
id
name
}
}
query {
products {
id
name
price
}
}
mutation {
createProduct(product: {name: "New Product", description: "A new product", price: 19.99}) {
id
name
price
}
}
mutation {
createOrder(order: {accountId: "account_id", products: [{id: "product_id", quantity: 2}]}) {
id
totalPrice
products {
name
quantity
}
}
}
query {
products(pagination: {skip: 0, take: 5}, query: "search_term") {
id
name
description
price
}
}
query {
accounts(id: "account_id") {
name
orders {
totalPrice
}
}
}
To generate gRPC files, follow these steps:
-
Download and install protoc
-
Install the necessary Go plugins:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
-
Create the
pb
folder in your service if doesn't exist.mkdir pb
-
Finally, run the command to generate the files:
# The Go generate directive is defined in the on graph.go on graphql service # and on service.go of all the other services go generate
Special thanks to @AkhilSharma90 for the valuable insights and resources that contributed to the development of this project.
This project serves as a comprehensive example of building a microservices architecture using Go, gRPC, GraphQL, and Elasticsearch. It provides a solid foundation for further development and scaling of microservices-based applications.