Skip to content

mnitta220/microservices-demo-rust

 
 

Repository files navigation

Online Boutique

microservices-demo-rust

English  |  Japanese

This project is a rewrite of parts of the Online Boutique (microservices-demo) demo application provided by Google in Rust.
It is a web-based e-commerce application that allows users to browse products, add them to a cart, and purchase them. It runs on a Kubernetes cluster and the microservices communicate with each other using gRPC. The development languages used are Go, C#, JavaScript, Java, and Python.
It consists of 11 microservices. I have rewritten 4 of these services in Rust.
In the following table, services marked in the Rewrote column as "Rust" have been rewritten in Rust. Services not marked as "Rust" remain in their original state.

Service Original
Language
Rewote Description
frontend Go Rust Exposes an HTTP server to serve the website. Does not require signup/login and generates session IDs for all users automatically.
cartservice C# Rust Stores the items in the user's shopping cart in Redis and retrieves it.
productcatalogservice Go Rust Provides the list of products from a JSON file and ability to search products and get individual products.
currencyservice JavaScript Converts one money amount to another currency. Uses real values fetched from European Central Bank. It's the highest QPS service.
paymentservice JavaScript Charges the given credit card info (mock) with the given amount and returns a transaction ID.
shippingservice Go Gives shipping cost estimates based on the shopping cart. Ships items to the given address (mock)
emailservice Python Sends users an order confirmation email (mock).
checkoutservice Go Retrieves user cart, prepares order and orchestrates the payment, shipping and the email notification.
recommendationservice Python Recommends other products based on what's given in the cart.
adservice Java Rust Provides random text ads.

This project was developed by forking the source code of microservices-demo as of March 11, 2024. Updates made to microservices-demo after that date have not been incorporated.


Purpose

The purpose of rewriting microservices-demo in Rust was to see how a web system running on a Kubernetes cluster would perform in Rust. microservices-demo was just the right size project for learning and experimentation.
For the Rust implementation, I used the axum web framework for the frontend and tonic for the gRPC library.
On the frontend, I adopted a method of splitting the screen into components, where each component generates HTML. This was inspired by React. Despite generating HTML on the server-side, it became a program with a component-oriented approach similar to React. For more details on these implementations, please refer to Rewriting in Rust.
I believe there are still many areas for improvement in this implementation. If you have any suggestions or feedback, please feel free to share them.


Screenshots

Home Page Checkout Screen
Screenshot of store homepage Screenshot of checkout screen

Quickstart

Development in Rust

For information on how to develop this project in Rust, please see the Development in Rust.

Running on Docker Desktop

  1. Install Git, Docker Desktop and Skaffold

  2. Select “Enable Kubernetes” in Docker Desktop Settings - Kubernetes

  3. Clone the repository.

    git clone https://github.com/mnitta220/microservices-demo-rust.git
    cd microservices-demo-rust/
  4. Run kubectl get nodes to verify docker-desktop control plane is running.

  5. Run skaffold run (first time will be slow, it can take ~30 minutes). This will build and deploy the application. If you need to rebuild the images automatically as you refactor the code, run skaffold dev command.

  6. Run kubectl get pods to verify the Pods are ready and running.

  7. Run kubectl port-forward deployment/frontend 8080:8080 to forward a port to the frontend service.

  8. Navigate to http://localhost:8080 to access the web frontend.


  • Cleanup

    • If you've deployed the application with skaffold run command, you can run skaffold delete to clean up the deployed resources.

Running on GKE

  1. Ensure you have the following requirements:

  2. Clone the repository.

    git clone https://github.com/mnitta220/microservices-demo-rust.git
    cd microservices-demo-rust/
  3. Set the Google Cloud project and region and ensure the Google Kubernetes Engine API is enabled.

    export PROJECT_ID=<PROJECT_ID>
    export REGION=us-central1
    gcloud services enable container.googleapis.com \
      --project=${PROJECT_ID}

    Substitute <PROJECT_ID> with the ID of your Google Cloud project.

  4. Confirm the services have been enabled for your project.

    gcloud services list --enabled --project=${PROJECT_ID}
  5. Create a GKE cluster and get the credentials for it.

    gcloud container clusters create-auto online-boutique \
      --project=${PROJECT_ID} --region=${REGION}

    Creating the cluster may take a few minutes.

  6. Deploy Online Boutique to the cluster.

    kubectl apply -f ./release/kubernetes-manifests.yaml
  7. Wait for the pods to be ready.

    kubectl get pods

    After a few minutes, you should see the Pods in a Running state:

    NAME                                     READY   STATUS    RESTARTS   AGE
    adservice-76bdd69666-ckc5j               1/1     Running   0          2m58s
    cartservice-66d497c6b7-dp5jr             1/1     Running   0          2m59s
    checkoutservice-666c784bd6-4jd22         1/1     Running   0          3m1s
    currencyservice-5d5d496984-4jmd7         1/1     Running   0          2m59s
    emailservice-667457d9d6-75jcq            1/1     Running   0          3m2s
    frontend-6b8d69b9fb-wjqdg                1/1     Running   0          3m1s
    paymentservice-68596d6dd6-bf6bv          1/1     Running   0          3m
    productcatalogservice-557d474574-888kr   1/1     Running   0          3m
    recommendationservice-69c56b74d4-7z8r5   1/1     Running   0          3m1s
    redis-cart-5f59546cdd-5jnqf              1/1     Running   0          2m58s
    shippingservice-6ccc89f8fd-v686r         1/1     Running   0          2m58s
    
  8. Access the web frontend in a browser using the frontend's external IP.

    kubectl get service frontend-external | awk '{print $4}'

    Visit http://EXTERNAL_IP in a web browser to access your instance of Online Boutique.

  9. Congrats! You've deployed the default Online Boutique. To deploy a different variation of Online Boutique (e.g., with Google Cloud Operations tracing, Istio, etc.), see Deploy Online Boutique variations with Kustomize.

  10. Once you are done with it, delete the GKE cluster.

gcloud container clusters delete online-boutique \
  --project=${PROJECT_ID} --region=${REGION}

Deleting the cluster may take a few minutes.


When updating the source code and deploying it to GKE, build the Docker image for that service with the following command:
docker image build -t <image-name>:<tag-name> .
Then push it to your registry with the following command:
docker image push <image-name>:<tag-name>
Replace the image: in /release/kubernetes-manifests.yaml with the new image. For example, if you want to update the frontend image, replace:
image: masahironitta/microservices-demo-rust-frontend:0.1.0

Use Terraform to provision a GKE cluster and deploy Online Boutique

The /terraform folder contains instructions for using Terraform to replicate the steps from Quickstart (GKE) above.

Other deployment variations

Deploy Online Boutique variations with Kustomize

The /kustomize folder contains instructions for customizing the deployment of Online Boutique with different variations such as:

Development

The method for developing this project on a local PC is described in the Rewriting in Rust.

See also original Development guide.


Performance comparison

I measured the response time by accessing http://localhost:8080/ using the Thunder Client extension in VSCode after starting it with skaffold run on my local PC. The results of three measurements for both the original and Rust version are as follows:

Original Rust version
First time 52ms 11ms
Second time 13ms 10ms
Third time 15ms 12ms

About

Rewrote the microservices-demo provided by Google in Rust.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 33.9%
  • Python 33.2%
  • Go 11.4%
  • JavaScript 5.3%
  • CSS 4.9%
  • Shell 4.2%
  • Other 7.1%