This lab can be executed directly in a Cloud Workstation, Cloudshell or your environment of choice.
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
sdk install java 21.0.4-graal
git clone https://github.com/GoogleCloudPlatform/serverless-production-readiness-java-gcp.git
cd genai/image-vision-vertex-langchain/
The Maven Wrapper is an easy way to ensure a user of your Maven build has everything necessary to run your Maven build.
Run the command:
mvn wrapper:wrapper
Build the JIT app image:
./mvnw package
Start the app locally:
java -jar target/image-analysis-1.0.0.jar
Build the Native Java executable:
./mvnw native:compile -Pnative
Test the executable locally:
./target/image-analysis
Build Native Java Tests:
./mvnw clean package -Pnative,nativeTest
Run the native tests locally:
./mvnw native-tests
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=image-analysis-jit
./mvnw spring-boot:build-image -DskipTests -Pnative -Dspring-boot.build-image.imageName=image-analysis-native
Check the Docker image sizes:
docker images | grep image-analysis
image-analysis-native latest aa0f7b406966 44 years ago 372MB
image-analysis-jit latest 67b3489cbec8 44 years ago 467MB
Start the Docker images locally. The image naming conventions indicate whether the image was built by Maven|Gradle and contains the JIT|NATIVE version
docker run --rm image-analysis-jit
docker run --rm image-analysis-native
Retrieve the Project ID, as it will be required for the next GCP operations
export PROJECT_ID=$(gcloud config get-value project)
echo $PROJECT_ID
Tag and push the images to GCR:
docker tag image-analysis-jit gcr.io/${PROJECT_ID}/image-analysis-jit
docker tag image-analysis-native gcr.io/${PROJECT_ID}/image-analysis-native
docker push gcr.io/${PROJECT_ID}/image-analysis-jit
docker push gcr.io/${PROJECT_ID}/image-analysis-native
Enable the required APIs, if they are not already enabled:
gcloud services enable vision.googleapis.com
gcloud services enable cloudfunctions.googleapis.com
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable logging.googleapis.com
gcloud services enable storage-component.googleapis.com
gcloud services enable aiplatform.googleapis.com
Retrieve the Project ID and Project Number
# get the Project_ID
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
or
export PROJECT_ID=$(gcloud config get-value project)
# get the Project_Number
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
Create the GCS bucket
export BUCKET_PICTURES=vision-${PROJECT_ID}
gsutil mb -l us-central1 gs://${BUCKET_PICTURES}
gsutil uniformbucketlevelaccess set on gs://${BUCKET_PICTURES}
gsutil iam ch allUsers:objectViewer gs://${BUCKET_PICTURES}
Instructions for configuring cloud Firestore available here
Set config variables
gcloud config set project ${PROJECT_ID}
gcloud config set run/region
gcloud config set run/platform managed
gcloud config set eventarc/location us-central1
Grant pubsub.publisher
to Cloud Storage service account
SERVICE_ACCOUNT="$(gsutil kms serviceaccount -p ${PROJECT_ID})"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
--member="serviceAccount:${SERVICE_ACCOUNT}" \
--role='roles/pubsub.publisher'
Deploy to Cloud Run
# deploy JIT image to Cloud Run
gcloud run deploy image-analysis-jit \
--image gcr.io/${PROJECT_ID}/image-analysis-jit \
--region us-central1 \
--memory 2Gi --allow-unauthenticated
# deploy native Java image to Cloud Run
gcloud run deploy image-analysis-native \
--image gcr.io/${PROJECT_ID}/image-analysis-native \
--region us-central1 \
--memory 2Gi --allow-unauthenticated
Set up Eventarc triggers
gcloud eventarc triggers list --location=us-central1
gcloud eventarc triggers create image-analysis-jit-trigger \
--destination-run-service=image-analysis-jit \
--destination-run-region=us-central1 \
--location=us-central1 \
--event-filters="type=google.cloud.storage.object.v1.finalized" \
--event-filters="bucket=vision-${PROJECT_ID}" \
--service-account=${PROJECT_NUMBER}[email protected]
gcloud eventarc triggers create image-analysis-native-trigger \
--destination-run-service=image-analysis-native \
--destination-run-region=us-central1 \
--location=us-central1 \
--event-filters="type=google.cloud.storage.object.v1.finalized" \
--event-filters="bucket=vision-${PROJECT_ID}" \
--service-account=${PROJECT_NUMBER}[email protected]
Test the trigger
gsutil cp images/GeekHour.jpeg gs://vision-${PROJECT_ID}
gsutil cp images/CloudRun.png gs://vision-${PROJECT_ID}
gcloud logging read "resource.labels.service_name=image-analysis-jit AND textPayload:GeekHour" --format=json
gcloud logging read "resource.labels.service_name=image-analysis-jit AND textPayload:CloudRun" --format=json
Log capture:
gcloud logging read "resource.labels.service_name=image-analysis-jit AND textPayload:CloudRun" --format=json
gcloud logging read "resource.labels.service_name=image-analysis-native AND textPayload:CloudRun" --format=json