This repository contains a tutorial on setting up a simple microservice architecture using Rust, gRPC, and Actix Web. The project demonstrates how to build a task manager application with a gRPC service for handling tasks and an HTTP API gateway for client interaction.
rust-microservice-tutorial/
├── api-gateway/
│ └── ...
├── grpc-service/
│ └── ...
└── task-service/
└── ...
This project contains the HTTP API gateway built with Actix Web. It serves as the entry point for client requests and interacts with the task service via gRPC.
This project contains the .proto files that define the gRPC service interfaces. It is packaged as a library and can be accessed from GitHub by the other services.
This project contains the task service implemented using Rust and gRPC with Tonic. It manages a PostgreSQL database using Sqlx and provides gRPC functions for CRUD operations on tasks.
The task manager application is designed with the following flow:
- A client sends an HTTP request to the API gateway.
- The API gateway, built with Actix Web, receives the request and translates it into a gRPC call.
- The gRPC call is sent to the task service, which interacts with a PostgreSQL database to perform the necessary operations (Create, Read, Update, Delete).
- The task service responds with the result of the operation via gRPC.
- The API gateway receives the gRPC response and sends an HTTP response back to the client.
- Rust (latest stable version)
- Cargo (Rust package manager)
- PostgreSQL (for the task service database)
-
Clone the repository:
git clone https://github.com/Bolu1/rust-gRPC-microservice-tutorial.git cd rust-gRPC-microservice-tutorial -
Setup PostgreSQL: Ensure you have PostgreSQL installed and running. Create a new database for the task service.
-
Setup gRPC Service: Navigate to the
grpc-servicedirectory and publish the package to your local or remote repository if needed. -
Setup Task Service: Navigate to the
task-servicedirectory:cd task-serviceAdd the necessary dependencies:
cargo add tonic prost cargo add tonic-build --build
Update the
Cargo.tomlto include the gRPC service package from GitHub.Configure the database connection settings in the
.envfile. -
Setup API Gateway: Navigate to the
api-gatewaydirectory:cd api-gatewayAdd the necessary dependencies for Actix Web and gRPC:
cargo add actix-web tonic
Update the
Cargo.tomlto include the gRPC service package from GitHub.
-
Run the Task Service:
cd task-service cargo run -
Run the API Gateway:
cd api-gateway cargo run
- Send HTTP requests to the API gateway to interact with the task service.
- The API gateway will handle the gRPC communication with the task service and return the appropriate responses.
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or improvements.
This project is licensed under the MIT License. See the LICENSE file for details.
By following this tutorial, you will learn how to set up and run a microservice architecture in Rust, utilizing gRPC for efficient service communication and Actix Web for an HTTP API gateway.
