Skip to content

16 GAE Deploy

16 GAE Deploy

Goal

  • Deploy the StayBooking backend to Google App Engine(GAE).

Support Cross-Origin Resource Sharing (CORS) Request

Wikipedia: Cross-origin resource sharing is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served.

To enable CORS on the backend, we need to handle the CORS preflight requests and return the dedicated CORS header to the frontend. More details about the CORS preflight: https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

  1. Open your staybooking project in Intellij and create a new CorsFilter class under the com.eve.staybooking.filter package.
package com.eve.staybooking.filter;

public class CorsFilter {
}
  1. Copy the following content to the CorsFilter. In the code, we return for the CORS preflight request(the request with the OPTIONS method).
package com.eve.staybooking.filter;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type");
        if ("OPTIONS".equalsIgnoreCase(httpServletRequest.getMethod())) {
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        } else {
            filterChain.doFilter(httpServletRequest, httpServletResponse);
        }
    }
}

Deploy Your Backend to Google App Engine

Google App Engine(GAE) is a managed application platform for developing and hosting web applications at scale. Official Documentation: https://cloud.google.com/appengine/docs.

  1. In your staybooking project, run Maven clean to remove the temporary files generated from your previous build.

Screenshot 2024-08-02 at 19.08.05

  1. Run Maven install to generate the new archive file of your project. Make sure the staybooking-0.0.1-SNAPSHOT.jar file is generated under the target folder.

Screenshot 2024-08-02 at 19.08.59

  1. Right-click on the staybooking-0.0.1-SNAPSHOT.jar, and select Open in Finder on Mac or Open in Explorer on Windows, rename the file to staybooking.jar. This step is very important, the file name matters.

Screenshot 2024-08-02 at 19.09.41

Screenshot 2024-08-02 at 19.10.01

  1. Go back to the Google Cloud Platform homepage and click the Activate Cloud Shell button.

Screenshot 2024-08-02 at 19.12.58

  1. In the Cloud Shell window, use the following command to set the project. Remember to replace the project_id with your project id, and you can find your project id under the GCP dashboard.

Screenshot 2024-08-02 at 19.14.17

gcloud config set project [project_id]

  1. In the Cloud Shell window, create a new directory with the following command.
mkdir staybooking
cd staybooking

Screenshot 2024-08-02 at 19.33.16

  1. Upload the staybooking.jar from your local computer to Google Cloud Shell.

Screenshot 2024-08-02 at 19.33.51

Screenshot 2024-08-02 at 19.34.51

Screenshot 2024-08-02 at 19.36.47

  1. Create a new Dockerfile under the staybooking directory.

vim Dockerfile

  1. Copy the following content into the Dockerfile and quit from the editor.
FROM openjdk:18
WORKDIR /
ADD staybooking.jar staybooking.jar
EXPOSE 8080
CMD java -jar staybooking.jar

Screenshot 2024-08-02 at 19.50.44

  1. Under the staybooking directory, create another new file called app.yaml.

    vim app.yaml

  2. Put the following contents into the app.yaml file and quit from the editor.

    runtime: custom
    instance_class: F4
    env: flex
    service: default
    network:
      name: default
      subnetwork_name: default
    automatic_scaling:
      min_num_instances: 1
      max_num_instances: 2
    

    Screenshot 2024-08-02 at 19.52.47

  3. Make sure you have all files ready.

    Screenshot 2024-08-02 at 19.54.52

  4. Use the following command to deploy your service to Google App Engine, you should get a URL for your service once the whole process is finished.

    gcloud app deploy

    Screenshot 2024-08-02 at 20.02.33

前后端分开部署

Screenshot 2024-08-02 at 20.04.48

https://stackoverflow.com/questions/45805722/gcloud-app-deploy-error-response-13-an-internal-error-occurred

find . -name 'Icon*' -type f -delete

Screenshot 2024-08-02 at 20.20.40

改前端程序里的domain里

Screenshot 2024-08-02 at 20.21.34

Screenshot 2024-08-02 at 20.22.08

Screenshot 2024-08-02 at 20.27.50

然后本地启动 localhost:3000

数据库也得记的改成云端的

Shutdown GCP Service

  1. Go to the Google Cloud Platform homepage at https://console.cloud.google.com.

Screenshot 2024-08-02 at 21.08.22

  1. Open the navigation menu and choose settings under IAM & Admin.

Screenshot 2024-08-02 at 21.08.48

  1. Use the Shutdown button to terminate all the services you’ve created on GCP.

Screenshot 2024-08-02 at 21.09.09

Screenshot 2024-08-02 at 21.10.01

Screenshot 2024-08-02 at 21.10.15