An open API service indexing awesome lists of open source software.

https://github.com/ddobrin/quotes-workshop


https://github.com/ddobrin/quotes-workshop

Last synced: 6 months ago
JSON representation

Awesome Lists containing this project

README

          

# Quotes Service - JIT and Native Java Build & Deployment to Cloud Run

## Originally published
https://github.com/GoogleCloudPlatform/serverless-production-readiness-java-gcp/tree/main/services/quotes

## VertexAI and Gemini settings
Set the following environment variables to build and run the app
```shell
export VERTEX_AI_GEMINI_PROJECT_ID=
export VERTEX_AI_GEMINI_LOCATION=
export VERTEX_AI_GEMINI_MODEL=<>
```
# Build

### Create a Spring Boot Application
```
# clone the repo
git clone https://github.com/ddobrin/quotes-workshop
```

### Validate that you have Java 21 and Maven installed
```shell
java -version
```

## Setup GraalVM
In order to build JIT or Native Java app images, please set up Java and GraalVM and the associated Java 21 distributions.
A simple installer is available from [SDKMan](https://sdkman.io/install)
```shell
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk version

sdk install java 21.0.2-graal
```

### Validate that GraalVM for Java is installed if building native images
```shell
java -version

# should indicate this or later version
java version "21" 2023-09-19
Java(TM) SE Runtime Environment Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15, mixed mode, sharing)
```

### Validate that the starter app is good to go
```
./mvnw package -Pproduction spring-boot:run
```

From a terminal window, test the app
```
curl localhost:8083/start

# Output
Hello from your local environment!

# get quotes from the app
curl localhost:8083/quotes
curl localhost:8083/random-quote
```

### Build a JIT and Native Java application image
```
./mvnw package -DskipTests -Pproduction

./mvnw native:compile -Pnative -DskipTests -Pproduction
```

### Start your app with AOT enabled
```shell
java -Dspring.aot.enabled=true -jar target/quotes-1.0.0.jar
```
### Build a JIT Docker image with Dockerfiles
```shell
# build an image with jlink
docker build . -f ./containerize/Dockerfile-jlink -t quotes-jlink

# build an image with a fat JAR
docker build -f ./containerize/Dockerfile-fatjar -t quotes-fatjar .

# build an image with custom layers
docker build -f ./containerize/Dockerfile-custom -t quotes-custom .
```
### Build a JIT and Native Java Docker Image with Buildpacks
```
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=quotes -Pproduction

./mvnw spring-boot:build-image -DskipTests -Pnative -Dspring-boot.build-image.imageName=quotes-native -Pproduction
```

### Test the locally built images on the local machine
```shell
docker run --rm -p 8080:8083 quotes

docker run --rm -p 8080:8083 quotes-native
```
### Build, test with CloudBuild in Cloud Build
```shell
gcloud builds submit --machine-type E2-HIGHCPU-32

gcloud builds submit --config cloudbuild-docker.yaml --machine-type E2-HIGHCPU-32

gcloud builds submit --config cloudbuild-native.yaml --machine-type E2-HIGHCPU-32
```
# Deploy
### Tag and push images to a registry
If you have built the image locally, tag it first and push to a container registry
```shell
# tag the image
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
echo $PROJECT_ID

# tag and push JIT image
docker tag quotes gcr.io/${PROJECT_ID}/quotes
docker push gcr.io/${PROJECT_ID}/quotes

# tag and push Native image
docker tag quotes-native gcr.io/${PROJECT_ID}/quotes-native
docker push gcr.io/${PROJECT_ID}/quotes-native
```

### Deploy Docker images to Cloud Run

Check existing deployed Cloud Run Services
```shell
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
echo $PROJECT_ID

gcloud run services list
```

Deploy the Quotes JIT image
```shell
# note the URL of the deployed service
gcloud run deploy quotes \
--image gcr.io/${PROJECT_ID}/quotes \
--region us-central1 \
--memory 2Gi --allow-unauthenticated
```

Deploy the Quotes Native Java image
```shell
# note the URL of the deployed service
gcloud run deploy quotes-native \
--image gcr.io/${PROJECT_ID}/quotes-native \
--region us-central1 \
--memory 2Gi --allow-unauthenticated
```

### Test the application
Start the containerized app locally:
```shell
# validate that app has started
curl localhost:8080:/start

# get quotes from the app
curl localhost:8083/quotes
curl localhost:8083/random-quote

# add a new quote to the repository
curl --location 'http://localhost:8083/quotes' \
--header 'Content-Type: application/json' \
--data '{
"author" : "Isabel Allende",
"quote" : "The longer I live, the more uninformed I feel. Only the young have an explanation for everything.",
"book" : "City of the Beasts"
}'

# Validate that the quote has been added
curl localhost:8083/quotes
```

Test the application in Cloud Run
```shell
# find the Quotes URL is you have not noted it
gcloud run services list | grep quotes
✔ quotes us-central1 https://quotes-...-uc.a.run.app
✔ quotes-native us-central1 https://quotes-native-...-uc.a.run.app
# validate that app passes start-up check
curl :/start

# get quotes from the app
curl :/quotes
curl :/random-quote

# add a new quote to the repository
curl --location ':/quotes' \
--header 'Content-Type: application/json' \
--data '{
"author" : "Isabel Allende",
"quote" : "The longer I live, the more uninformed I feel. Only the young have an explanation for everything.",
"book" : "City of the Beasts"
}'
```

If you have deployed the app with security enabled, (no --allow-unauthenticated flag) you can test it with a Bearer token. You can use also an alternative [HTTP test client](https://httpie.io/)
```shell
TOKEN=$(gcloud auth print-identity-token)

# Get the URL of the deployed app
# Test JIT image
http -A bearer -a $TOKEN https:///random-quote
http -A bearer -a $TOKEN https:///quotes

# Test Native Java image
http -A bearer -a $TOKEN /random-quote
http -A bearer -a $TOKEN /quotes
```