Go Microservices
Haroon Albar / November 13, 2024
Checkout in GitHub š
Microservices in Go with gRPC, GraphQL & Elasticsearch
Overview
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.
Technologies Used
- 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.
Project Structure
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
Getting Started
-
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
.
GraphQL API Usage
The GraphQL API provides a unified interface to interact with all the microservices.
Example Queries and Mutations
Query Accounts
query {
accounts {
id
name
}
}
Create an Account
mutation {
createAccount(account: { name: "New Account" }) {
id
name
}
}
Query Products
query {
products {
id
name
price
}
}
Create a Product
mutation {
createProduct(
product: { name: "New Product", description: "A new product", price: 19.99 }
) {
id
name
price
}
}
Create an Order
mutation {
createOrder(
order: {
accountId: "account_id"
products: [{ id: "product_id", quantity: 2 }]
}
) {
id
totalPrice
products {
name
quantity
}
}
}
Advanced Queries
Pagination and Filtering
query {
products(pagination: { skip: 0, take: 5 }, query: "search_term") {
id
name
description
price
}
}
Calculate Total Spent by an Account
query {
accounts(id: "account_id") {
name
orders {
totalPrice
}
}
}
gRPC File Generation
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
Acknowledgments
Special thanks to Akhil Sharma for the valuable insights and resources that contributed to the development of this project.
Conclusion
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.