- Introduction
- Usage
- Contributing Code
Introduction
This is a demo project based on GCP Online Boutique with added support for REST and Thrift APIs. It is designed to be used during cloud-native development for testing across different APIs–REST, gRPC, and Thrift. It is extensible in support of future functionality (e.g. GraphQL).
Use Cases
- Use as a test project for developing cloud-native dev tools.
- Learn how to implement Thrift (and REST) support for an existing project.
Implemented by the Skyramp team.
Implementation
For each microservice in the repo, REST and Thrift implementations open up separate ports to listen to the corresponding traffic. Implementations are in the rest.go
and thrift.go
files in the top-level directory for each service.
Default ports for REST and Thrift are hardcoded in the main.go
file in each service directory. For example, the carts
microservice has the following ports defined:
const (
serverCrt = "server.crt"
serverKey = "server.key"
defaultThriftPort = "50000"
defaultRestPort = "60000"
)
Usage
1. Install pre-requisite tools
- docker
- curl (required for REST)
- jq (optional)
- kubectl (optional)
2. Clone sample services
git clone https://github.com/letsramp/sample-microservices.git
cd sample-microservices/src
3. Build and Push Images (Optional)
Images for sample-microservices are already available in a publically accessible registry. To use your own registry, follow these steps.
Build images from source
docker compose build
Push services to container registry
Update the host path/s in the docke-compose.yaml file to point to your own docker registry. Push images by running:
docker compose push
4. Deploy
Deploy Online Boutique by running the following command:
docker compose up
Now, familiarize yourself with the application by navigating to http://127.0.0.1:8080 on your browser.
📝 NOTE: If you are using Docker Desktop, you can look at the logs for
cart service
to see that we've opened up separate ports for gRPC(7070), REST(60000), and Thrift(50000) traffice to the service.
5. Interacting with the microservices
To demonstrate the REST and Thrift implementations, we've created simple clients (src/clients
) for the "add to cart" scenario for each of the APIs. The client code is accessible from the clients
container in the cluster.
To connect to the clients
container, run the following command in your terminal:
docker compose exec -it clients ash
Optionally, you can download and install Docker Desktop to follow along.
If you are using Docker Desktop, click on the clients_1
container and go to the CLI from there.
Add to cart using gRPC API
- In the
clients
container, navigate to the grpc/golang folder and download the requiredgo
modules.
cd /grpc/golang
go mod download
- In the
addCart.go
file, you will notice that we open a connection to port 7070 ofcart
microservice which listens to grpc traffic.
conn, err := grpc.Dial("cartservice:7070", grpc.WithInsecure())
- Run the code in the file to add an item to the cart.
go run ./cmd/cart
Expected result
Successfully added the item to the cart.
Having seen how to successfully add an item to the cart with a gRPC client calling the gRPC endpoint, we can see how to do the same through the REST and Thrift endpoints.
Add to cart using REST API
Since cURL is already installed in the clients
container, you can issue a request directly from the CLI of the container to add an item to the cart.
curl -X 'POST' 'http://cartservice:60000/cart/user_id/abcde' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
-d '{
"product_id": "OLJCESPC7Z",
"quantity": 1
}'
Again, notice that the request is being sent to port 60000
of cart
microservice which listens to REST traffic.
Result
{"success":"200 OK"}
Add to cart using Thrift API
- In the
clients
container, navigate to the thrift/golang folder and download the requiredgo
modules for the Thrift client.
Setup
cd /thrift/golang
go mod download
- In the
addCart.go
file, you will notice that we open a connection to port 50000 ofcart
microservice which listens to Thrift traffic.
clientAddr := "cartservice:50000"
- Now, run the code in the file to add an item to the cart.
go run ./cmd/cart
Result:
"Successfully added the item to the cart."
Verify contents of cart
You can now fetch the contents of the cart using REST API to see that a total of 3 items were added (one using each supported API).
curl -X 'GET' \
'http://cartservice:60000/cart/user_id/abcde' \
-H 'accept: application/json'
Result:
{"user_id":"abcde","items":[{"product_id":"OLJCESPC7Z","quantity":3}]}
Contributing Code
PRs are welcome!
This project follows the CNCF Code of Conduct .