Learn how to deploy JEE applications in Google Compute Engine (IaaS offering) and Google App Engine (PaaS offering) in this tutorial by Ram Kulkarni, a software developer with over two decades of experience developing enterprise applications.

Deploying in Google Cloud

In this section, you will see how to deploy JEE applications in Google Compute Engine (IaaS offering) and Google App Engine (PaaS offering). Compute Engine (https://cloud.google.com/compute/) can be considered an AWS EC2 counterpart, and App Engine (https://cloud.google.com/appengine ) an Elastic Beanstalk counterpart. You need to have a Google account to log into Cloud Console at https://console.cloud.google.com. You need to have at least one project created in Google Cloud to deploy applications. When you log into the Cloud Console, it will prompt you to create a project if there are no projects already available:

Deploy JEE applications in Google Cloud

All you need to enter in the Create Project page is the name of the project. The Project ID will be automatically selected for you. You should keep this Project ID handy, because many SDK commands need a Project ID as one of their parameters.

If you already have projects, but want to create a new project for this book, open the Google Cloud Console web page and go to the IAM & admin | Manage resources page. Click the Create Project link on the page.

Setting up Google Cloud Tools

Setting up Google Cloud Tools requires multiple steps. Start with installing the SDK.

Installing the Google Cloud SDK

Download the SDK from https://cloud.google.com/sdk/. Unzip it and run the following command from the bin folder:

gcloud init

See https://cloud.google.com/sdk/docs/initializing for more options regarding initializing the SDK.

Installing Java extensions for the App Engine SDK

Run the following command (make sure that the Cloud SDK is installed and configured):

gcloud components install app-engine-java

See https://cloud.google.com/sdk/docs/managing-components for details on managing Google Cloud components.

Next, set the default project name for gcloud commands:

gcloud config set project <your-project-name-here>

Installing Google Cloud Tools for Eclipse

To install the plugin for Google Cloud in Eclipse, open Eclipse Marketplace (select the menu Help | Eclipse Marketplace…). Search for Google Cloud Tools:

Deploy JEE applications in Google Cloud

Setting Eclipse Preferences for Google Cloud Tools

Open Eclipse Preferences and go to the Google Cloud Tools preferences:

Deploy JEE applications in Google Cloud

Enter the path to the folder where you unzipped the SDK in the SDK location field.

Deploying the application in Google Compute Engine

In this section, you will learn to create an instance of a VM in Google Compute Engine and deploy a JEE application in it. You’ll install Docker in a VM in Compute Engine and deploy a CourseManagement service in it. But first, you need to create a VM using either the Google Cloud Console web page or Terminal on the host machine.

Creating a VM instance in Google Compute Engine

Log in to the Google Cloud Console (https://console.cloud.google.com) and go to the Compute Engine | VM Instances page. Click the Create Instance link. Create an instance using Debian GNU/Linux boot disk. Make sure you select the Allow HTTP traffic and Allow HTTPS traffic options.

Installing Docker in a VM instance

In the VM instances page, select the instance you want to use and drop down the SSH options (in the Connect column in the table):

Deploy JEE applications in Google Cloud

Opening an SSH connection to a VM instance

Select Open in browser window. This option opens a browser window and opens an SSH shell in the VM instance. Run the following commands in the shell to install Docker:

sudo apt-get updateGets the latest version of packages and dependencies
curl -fsSL get.docker.com -o get-docker.shDownloads the Docker installer script
sudo sh get-docker.shRuns the installer script

Once Docker is installed, you need to execute a few commands so that the Docker command can be called without using sudo (Docker runs as the root):

sudo groupadd dockerCreates a Docker user group. It probably already exists.
sudo usermod -aG docker $USERAdds a current user to the Docker group.

See https://docs.docker.com/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user for more details.

Log out of the shell and log back in (close the shell window and open a new shell window). If all of the preceding commands have been executed successfully, then you should be able to run the docker ps command without sudo.

Next, you need to install docker-compose in the instance (see https://docs.docker.com/compose/install/). Execute the following commands (the version number might be different in the command to install docker-compose):

sudo curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-composeDownloads docker-compose
sudo chmod +x /usr/local/bin/docker-composeMakes docker-composeexecutable

The source code for this tutorial can be found at https://github.com/PacktPublishing/Java-EE-8-Development-with-Eclipse-Third-Edition/tree/master/Chapter13. It includes a folder named coursemanagement-docker-compose. Upload all files in that folder to the VM instance. You can either upload from the browser shell window or use the gcloud command from your host machine. In the browser shell, click on the Settings icon in the upper-right corner and select the Upload File option. Upload all of the files that are in the coursemanagement-docker-compose folder. To upload from Terminal, execute the following gcloud command after changing the folder to coursemanagement-docker-compose:

gcloud compute scp * <your-instance-name-here>:~/

This command copies all of the files in the current folder (which in this case is coursemanagement-docker-compose) to the user’s home folder in the instance.

Whichever method you use to upload the files, make sure that you have the following files in the VM instance:

  • course-management-db.dockerfile
  • course-management-service.dockerfile
  • docker-compose.yml
  • course-management-db.sql
  • coursemanagementspring-0.0.1-SNAPSHOT.jar

In the browser shell for the VM instance, execute the following command to set up the database and REST service in Docker containers:

docker-compose up -d

Once the command is executed successfully, browse to http://<instance_external_ip>/course_management/courses. You will just see an empty JSON array, because there is no data in the database. You can find the external IP of your instance from the Compute Engine | VM Instances page.

Run the docker-compose down command to shut down the containers.

Deploying the application in Google App Engine

App Engine is Google’s Platform as a Service (PaaS) offering, similar to Elastic Beanstalk from Amazon. In this section, you will learn how to deploy the CourseManagementREST service using Google App Engine.

Make a copy of the CourseManagementREST project. Right-click on the project in Eclipse Project Explorer and select Copy. Right-click anywhere in Project Explorer and select Paste. Eclipse will prompt you to name the project. Name it CourseManagementREST-GAE. You will deploy this project using Google App Engine.

Configure the project as an App Engine project. Right-click on the CourseManagementREST-GAE project in Project Explorer and select Configure | Convert to App Engine Standard Project.

Note that if you are creating a new project for deployment to Google App Engine, then go to the File | New | Google App Engine Standard Java Project menu. Alternatively, you can go to the drop-down menu from the Google Cloud Platform icon in the toolbar and select Create New Project | Google App Engine Standard Java Project.

Before you deploy the project, remove web.xml from the src/main/webapp/WEB-INF folder. Google App Engine’s Java platform uses the Jetty server and it does not need web.xml for this deployment.

Note that you may see an error stating web.xml is missing and <failOnMissingWebXml> is set to true pom.xml after deleting web.xml. To suppress this error, add the following property in pom.xml: <properties>    <failOnMissingWebXml>false</failOnMissingWebXml>   </properties>

To test this application locally, go to the Servers view, right-click on it, and select New | Server. Then, expand the Google group and select App Engine Standard:

Deploy JEE applications in Google Cloud

Click Next and add the CourseManagementREST-GAE project for deployment:

Deploy JEE applications in Google Cloud

Click Finish and start the server from the Server view. Then, browse to http://localhost:8080/services/course/get/1 to verify that the application has been deployed properly.

If you get errors regarding the JDK version in pom.xml, add the following section in pom.xml: <properties>  <maven.compiler.source>1.8</maven.compiler.source>  <maven.compiler.target>1.8</maven.compiler.target></properties>

Before you deploy this project to Google App Engine, you should ensure that an application has been created in Google App Engine. Browse to https://console.cloud.google.com/appengine and check if any application exists. If not, you can create an application from that page. Alternately, you can run the following command in Terminal:

gcloud app create

To deploy this project to Google App Engine, select the project in Project Explorer and the drop-down menu from the Google Cloud Platform toolbar button:

Deploy JEE applications in Google Cloud

Select the Deploy to App Engine Standard… menu:

Deploy JEE applications in Google Cloud

Select your Google account from the drop-down menu and, then, select the Google Cloud project you want the application deployed to.

Once the project is deployed, browse to https://<your_project_id>.appsport.com/services/course/get/1 to verify it.

To stop the application, you need to disable the application—open https://console.cloud.google.com and go to App Engine | Settings and click the Disable Application button.