Google Cloud Platform is a well-documented product, and it is really easy for anybody to start from scratch and build a new app pretty quickly and easily. However, it might be overwhelmingly detailed for someone who just wants to get a feel for things and be more comfortable before diving in.
So here's a quick hand-holding guide from scratch to deploy your own GCP app in no time. All you need is a Google account (no billing needed, since we will use the free tier of GCP). Also, I'm assuming you know how to build a basic Spring Boot application.
Get Started With Google Cloud Platform:
To get started, open up https://cloud.google.com.
If you're already logged onto your Google account, you will see your profile on the top right-hand side.
This is the standard Google account (that you use for accessing Gmail) and nothing specific to GCP.
Google Cloud Platform gives you a one-year trial along with $300 worth credit to play around with all the available products, but for what we intend to implement, we do not need this.
Step 1: Start With GCP
Simply click on 'console' link on the top right-hand side corner.
You shall see this welcome screen. Update the preferences and move on and agree to the terms of service.
Step 2: Create a New Project
Since this is the first time, you will be taken to the select project screen. Next time onwards, clicking console will take you to the dashboard of the last project you edited.
Also, the list is empty the first time around, so click the '+' to create a new project.
You will see a prompt like so. In the free tier, you have a limit of 12 projects. Give it an appropriate name and choose an appropriate id. The id will determine the URL of your app. In my case, it will be hello-91117.appspot.com
So, we get the newly created project in the selection screen. Choose this project to proceed.
Step 3: Configure Your Project
GCP has multiple products at your disposal. However, to keep things simple, App Engine is the recommended place to go. You can read about App Engine's details here.
On the left-hand-side menu, navigate to the App Engine tab.
Here, we choose what your app environment should be. When you get the prompt, choose Java as the stack.
The next prompt will be to choose the region where the instances should be deployed. Choose based on your geographic location.
Once done, you will get a prompt to 'deploy'. This step will trigger a Java servlet-based hello-world tutorial, which you can follow if you are interested in that. However, we are more interested in learning how to get started with a Spring Boot app. So, lets cancel the tutorial and get down to business.
Most of our configuration steps are done.
Click the command prompt icon from the top right corner of the window. This opens up a Linux-like terminal to the VM. This is pre-installed with the basic software that we need for our tutorial.
Write Some Code
You can get my code here: https://github.com/raovinay/springboot-gcp-hello
Create a Maven-based Spring Boot project. Let's go over a few points to note about the pom.xml.
Packaging should be a WAR. Although Spring Boot can run as an executable JAR with the inbuilt Tomcat, App Engine doesn't like that.
Exclude embedded Tomcat to avoid any issues with the Jetty server.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.8.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
Explicitly include the servlet API as a dependency.
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
appengine-maven-plugin, as that is needed for running the deploy commands.
This is my project structure. I will not be going into the Spring Boot code, since learning Spring Boot is not the agenda. Just note that I have kept the code pretty bare-bones.
The one new file that stands out is the appengine-web.xml. This is required for configuring App Engine's runtime. I have configured my runtime to be Java 8. There is a host of other things you can configure here, from the number of instances, thread configurations, etc.
You can run this on your local as a normal Spring Boot project to check if it works.
At the end of step 3, we had left a console window open. We can use that to get our code into the VM. You have a couple of options. First, you can either commit your code to GitHub and then check it out with the
git clone command.
But if that is too much of a hassle, you can upload the code as a ZIP file. Click on the three dots in the right corner.
The file will be uploaded to the home folder. In my case, the ZIP file was named springboothello.zip. So the commands I fired were:
mvn clean install
This starts the app in dev mode. You can test this by clicking 'Preview on port 8080':
This will launch a browser window for preview. Note that this is not the final production URL.
Once you're happy with the preview, you can deploy this app into production with the following command:
This will launch your app, which can now be accessed by navigating to <your-project-id>.appspot.com.
Feeling adventurous? I tried a few more things on the paid tier of GCP and made a wrapper for Google's translate APIs: https://github.com/raovinay/txl8_springboot
Try something yourself and let me know!