Back to projects

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

  1. Clone the repository:

    git clone https://github.com/haroonalbar/go-grpc-graphql-microservices
    cd go-grpc-graphql-microservices
  2. Start the services using Docker Compose:

    docker-compose up -d --build
  3. 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:

  1. Download and install protoc

  2. 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
  3. Create the pb folder in your service if doesn't exist.

     mkdir pb
  4. 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.