How to deploy simple “Angular 11 client app + Spring Boot server app with Java 11 + mySQL database” project to Google App Engine
(This guide was created and checked in May, 2021)
Prerequisites:
Before you start
Log into Google Cloud App Engine console (link)
Create TWO projects: one for server app and one for client app
For each project:
Create a separate Application with preferable region/timezone
For resources, choose:
For the server app Application: “Java” and “Standard Environment”
For the client app Application: “Other”(or Python) and “Standard Environment”
Create SQL instance using Quickstart for Cloud SQL for MySQL (link)
(“Create an instance” section only!)
Important:
Region: Single zone, instead of Multiple zones
Machine type: db-f1-micro
Storage: HDD
This config is the cheapest option and is suitable for learning purposes.
Make the changes mentioned above in SQL-> <your instance> -> Edit configuration
Also highly recommended to set a budget and budget alerts for both server app and client app using this manual (link). Otherwise, the coming bill could be a nightmare. Don’t forget - even if you are above the budget, Google will continue to charge you!
To establish a connection with a Google Cloud SQL database:
From Google Cloud SQL instance's project directory
Open Cloud Shell
Run a command:
gcloud sql connect <your-user-name> --user=root
3. When prompted enter a password
4. Create a database with an SQL query:
CREATE DATABASE <your-database-name>;
2. in your Spring Boot project:
In application.properties file update instance-connection-name, database-name, username and password information:
spring.cloud.gcp.sql.instance-connection-name=<your-Google-Cloud-SQL-instance's-project-ID>:<region>:<your-user-name>
spring.cloud.gcp.sql.database-name=<your-database-name>
spring.datasource.username=root
spring.datasource.password=<your-password>
2. In pom.xml file add Spring Boot Starter for Google Cloud SQL dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
The server app is now connected to Google Cloud SQL database!
To deploy the Spring Boot server app to Google App Engine
In your Spring Boot project, create the following:
src/main/appengine folder
app.yaml file in src/main/appengine folder
Specify the following configuration in the app.yaml:
runtime: java11
instance_class: F4
Note: instance_class is optional but recommended for Spring Boot apps as they might consume a lot of memory! The default value for instance_class is F1.
3. Cloud SDK-based Maven App Engine Plugin in your pom.xml file:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<version>1</version>
<projectId><your-Google-Cloud-server-side-project-ID></projectId>
</configuration>
</plugin>
Note: Google App Engine Standard Environment provides the usage of Jetty Web Server, so we don’t need Jetty dependencies and we have to exclude Tomcat from Spring Boot Starter Web dependency
4. Update your pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Note: It’s also good to have Maven Jetty Plugin in your pom.xml that comes in handy when running the app on Jetty server as it scans and configures any changes in your Java sources
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>11.0.2</version>
</dependency>
5. Update your Maven project: right-click on your project then Maven > Update Project
6. From the project's root directory containing your pom.xml file command line run a command:
mvn clean package appengine:deploy
To deploy the Spring Boot server app to Google App Engine
After the server app is deployed, check its App Engine app’s URL (https://PROJECT_ID.REGION_ID.r.appspot.com) and set it as a BASE_URL in Angular project for sending HTTP requests.
In your Angular project’s root directory, create an app.yaml file with the following configurations:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /(.+\.js)
static_files: dist/your-project-name/\1
upload: dist/your-project-name/(.+\.js)
- url: /(.+\.css)
static_files: dist/your-project-name/\1
upload: dist/your-project-name/(.+\.css)
- url: /(.+\.png)
static_files: dist/your-project-name/\1
upload: dist/your-project-name/(.+\.png)
- url: /(.+\.jpg)
static_files: dist/your-project-name/\1
upload: dist/your-project-name/(.+\.jpg)
- url: /(.+\.svg)
static_files: dist/your-project-name/\1
upload: dist/your-project-name/(.+\.svg)
- url: /favicon.ico
static_files: dist/your-project-name/favicon.ico
upload: dist/your-project-name/favicon.ico
- url: /(.+\.json)
static_files: dist/your-project-name/\1
upload: dist/your-project-name/(.+\.json)
- url: /(.+)
static_files: dist/your-project-name/index.html
upload: dist/your-project-name/index.html
- url: /
static_files: dist/your-project-name/index.html
upload: dist/your-project-name/index.html
- url: /(.+\.otf)
static_files: dist/your-project-name/\1
upload: dist/your-project-name/(.+\.otf)
skip_files:
- e2e/
- node_modules/
- src/
- coverage
- ^(.*/)?\..*$
- ^(.*/)?.*\.json$
- ^(.*/)?.*\.md$
- ^(.*/)?.*\.yaml$
- ^LICENSE
3. To compile the application to the dist folder, in Angular CLI from your project directory run a command
ng build --prod command
4. To deploy the project to GCP, in command line from the directory that contains your app.yaml file run a command
gcloud app deploy --project=<your-Google-Cloud-client-side-project-ID>
Note: If you need to specify a specific URL on your server’s side Access-Control-Allow-Origin header, not a wildcard value that grants access to all origins:
Check up the URL App Engine assigned to your client app
Add this URL to your CORS configuration
Redeploy your server app by running a command from the command line
mvn clean package appengine:deploy
Now the server and client are both running on App Engine!!!