DEV Community

Cover image for Running Java App using Google Cloud Run
Sami Ekblad
Sami Ekblad

Posted on

Running Java App using Google Cloud Run

In our journey we've created a Net Promoter Score (NPS) widget and application that saves data to a Google Spreadsheet. This time, we take the app to the cloud.

Since we already used Google Sheets for storage, it felt natural to use Google Cloud Run for deployment. It offers a fully managed platform with enough of resources that allows applications to run in containers accessible via the web. Additionally, when using the Cloud Console, we do not need to install any local tools, streamlining the deployment process.

Clone, Build, Deploy

Deploying Java applications in the cloud has been a challenge. However, recent advancements have greatly simplified this process. Utilizing build packs or a Dockerfile, we can build and deploy Maven applications quickly.

Step 1: Navigate to shell.cloud.google.com to access Google Cloud's integrated shell environment. This is a full-featured IDE in your browser.

Step 2: Clone the NPS project from GitHub github.com/samie/nps. This repository contains a Vaadin add-on itself, but also the demo application project we want to deploy.

Step 3: If your project lacks a Dockerfile, you need to add one. You can use Java build packs, but a two-phase Dockerfile gives you a more definitive build. This first downloads all dependencies before creating your Vaadin application's production build.

Cloud Run automatically finds the Docerfiles in the project.

Step 4: Build and deploy to Cloud Run. Select the correct Dockerfile, in this case, demo/Dockerfile, and initiate the build. Deployment build may take several minutes (typically 3 to 4 minutes).

Upon successful deployment, you will encounter an error related to Google Sheets access:

java.lang.RuntimeException due to missing credentials.{ width=60% }

This was expected. We need the credentials to access the Google Sheet.

Configuring Secrets

Managing environment variables and secret files in cloud environments can be tricky. Google provides a "Secret Manager" tool where you can safely manage your application-specific secrets.

Create new secret for service account credentials

Step 1: Create a Secret
Create a new secret containing the service account credentials to access the Google Sheet at console.cloud.google.com/security/secret-manager. If you already created a key for the Service Account, you can paste the content of that file, or you can create a new key at console.cloud.google.com/iam-admin/serviceaccounts and choose tab "Keys" in the correct service account.

Step 2: Make Secrets Available
Make these secrets available to your deployed containers by creating a special "secrets" volume under the "Volumes" tab. Then, mount this volume to /local on the container through the "Containers" tab.

Step 3: Use secrets in the code
Adjust your application code (in this case, in NPSView.java) to check for the proper credentials file location at /local/<secret_name>.

With the secrets configured and your application adjusted, click "Deploy" to finalize the process. Your application should now be operational!

Application successfully running

The NPS feedback view is running at nps-u342s5ewoq-uc.a.run.app/
And the embedded page sample at nps-u342s5ewoq-uc.a.run.app/example.html

Conclusion

I was captivated by the simplicity of cloud-only tooling. Building directly from GitHub in browser IDE without local installations was refreshingly straightforward. The most challenging part was ensuring secure access to sensitive files, and I was a bit disappointed with how little synergy GCP offered for my architecture. Nevertheless, once you navigate its terminology and enable all the needed services, Google Cloud Platform provides robust tools to build anything.

Java application up and running in cloud with session affinity

This marks the end of the series. It was initially a set of personal memos, from crafting a custom Vaadin widget, calling APIs, and integrating cloud services. But it grew into this guide, the path of deploying Java-only applications in the cloud. I hope you enjoyed the journey as much as I did.I thank you for joining this journey.

Happy coding, and let me know what you built!

Top comments (0)