Published on

Serverless SpringBoot with Cloud Run & Cloud Datastore

Authors
  • avatar
    Name
    mrgenco
    Twitter

Two days ago I created a SpringBoot application for experimenting Google Cloud Run and Google Cloud Datastore platforms. It is a simple application with RESTful CRUD APIs.

Finally I deployed it to the Cloud Run fully managed environment as a serverless application.

img

In this post I'll be showing the steps for deploying the application.

You can access the full source code and explanations on my github account. Google Cloud Platform is offering a free tier with $300 total for all of its products. It is a generous amount for learning purposes and hoby projects. If you ask me, it is also a good amount for starting a SaaS product with its always free products.

For those who never heard of Cloud Run & Cloud Datastore before let's get started with some definitions.

Google Cloud Run is a fully managed serverless computing platform for deploying and scaling containerized applications quickly and securely. It’s going to make it very easy for people who are running containerized, stateless web servers today to get serverless benefits. Cloud Run is different from AWS lambda in this context by running containers instead of pure functions.

Google Cloud Datastore is a NoSQL document database built for automatic scaling, high performance and ease of application development. If you are familiar with Firebase products, Cloud Datastore uses same infrastructure. See this doc for further details.

Spring Cloud GCP is one of the sub projects of Spring Cloud providing tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery). It helped me a lot while developing Google Cloud Platform (GCP) applications with Spring Framework by providing auto-configuration facilities.

Setup and Deploy for local environment :

  1. Login to your GCP(Google Cloud Platform) account.

  2. Install Google Cloud SDK to your local machine. Make sure it is installed correctly by checking the version.

$ gcloud --version
Google Cloud SDK 296.0.1
bq 2.0.57
core 2020.06.10
gsutil 4.51
  1. Create a new cloud project in the console. See this link for how to create and manage projects.

  2. Make sure we are pointing to the correct project and authenticate using following commands:

    $ gcloud config set project <YOUR PROJECT ID>
    $ gcloud auth login
    
  3. Make sure your machine has app credentials to run your app.

    $ gcloud auth application-default login
    
  4. Run your application locally

    $ mvn spring-boot:run
    

Setup and Deploy for Cloud Run :

  1. Follow first 5 steps above and run following command
$ mvn install jib:build

This command will build and push our image to the GCR (Google Cloud Registery) automatically.

[INFO] Built and pushed image as gcr.io/serverless-todo-280413/todoapp
[INFO] Executing tasks:
[INFO] [============================  ] 94,4% complete
[INFO] > launching layer pushers
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  26.534 s
[INFO] Finished at: 2020-06-26T18:47:33+03:00

We don't need Docker installed on our local machine since we have jib dependency in the pom.xml file. JIB is a great way to containerize your Java application. It allows you to create optimized images without Docker using Maven or Gradle.

img

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>2.4.0</version>
    <configuration>
      <from>
        <image>gcr.io/distroless/java:8</image>
      </from>
      <to>
        <image>gcr.io/serverless-todo-280413/${project.artifactId}</image>
      </to>
    </configuration>
</plugin>
  1. Create a new Cloud Run service in the console.

img

  1. Select your uploaded image and hit CREATE button.

img

  1. Your serverless application is now READY!

Endpoints

Get all records (HTTP : GET)

YOUR_CLOUDRUN_SERVICE_URL/api/todo/all

Get single record (HTTP : GET)

YOUR_CLOUDRUN_SERVICE_URL/api/todo/get/id_value

Save new record (HTTP : POST)

YOUR_CLOUDRUN_SERVICE_URL/api/todo/save

Update single record (HTTP : POST)

YOUR_CLOUDRUN_SERVICE_URL/api/todo/update

Delete single record (HTTP : GET)

YOUR_CLOUDRUN_SERVICE_URL/api/todo/delete/id_value

Any Questions or Want to be Friends?

Feel free to contact me on github or follow me on [twitter] (https://twitter.com/mr_genco). :)

Thanks for reading!