Use Spring Boot + Objectify and deploy to Cloud Run — 2
Aim of the project is using Objectify as a persistance API in a Spring Boot project. First we used a Data Store Emulator for local development.Now, I am going to deploy it to a Cloud Run compute service as a real cloud project.
Let’s remember first part. I created a spring boot project and used objectify for persistance level. I used data store emulator firstly in our local environment. Today we will deploy it to Cloud Run compute service. I assume that you already have basic Google Cloud and Spring Boot knowledge. I will not talk about Objectify, Spring Boot or Cloud Run deeply. You can follow the source code from here.
What is Cloud Run?
Cloud Run is a compute platform that enables you to run containers. Pricing and speed of deployment are striking features. We have to dockerize our project for use it as a Cloud Run service.
You can anable Cloud Run API from wizard.
Time to dockerize
We need a Dockerfile. Dockerfile is a text file contains commands for ganarate an image. Normally you can use docker cli for docker features as well but in this project i will use google cloud cli commands.
First off all we have to be sure that we are working on correct google cloud project. Let us check with;
gcloud config list
‘project’ variable should be same with your cloud project [project-id]. You can change project id with;
gcloud config set project [project id]
One of the most important thing here is disabled the using of data store emulator by Objectify. Instead of emulator initialize, we call the just ObjectifyService.init().
Let’s build an docker image and send it to google platform. Container Registery is the correct place to keep docker images.
Image name should be in gcr.io/[project id]/[image name] format.
gcloud builds submit --tag gcr.io/[project id]/spring-boot-and-objectify-image
After get the ‘SUCCESS’ message we can see the image in the Container Registery. Container Registery is a storage of images.
Now, generate a container from this image and deploy it to Cloud Run compute service.
gcloud beta run deploy — image gcr.io/[project id]/spring-boot-and-objectify-image — platform managed
You set name, server region and permission during deployment process. I named the service as ‘fleet-service’.
After deploy is finished, you can see the service at Cloud Run. There is a URL there and we will use it.
Click to the service name and you see the URL of ‘fleet-service’. Yes, we will use it to call our service.
Before test enable the Datastore.
You can use same postman requests but do not forget change the URL. With using our /add method, new airplane object will be added to Datastore by using Objectify.
I hope you’ve found this assay helpful.