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
- Open your staybooking project in Intellij and create a new CorsFilter class under the
com.eve.staybooking.filter
package.
- 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.
- In your staybooking project, run Maven clean to remove the temporary files generated from your previous build.
- 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.
- 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.
- Go back to the Google Cloud Platform homepage and click the Activate Cloud Shell button.
- 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.
gcloud config set project [project_id]
- In the Cloud Shell window, create a new directory with the following command.
- Upload the staybooking.jar from your local computer to Google Cloud Shell.
- Create a new Dockerfile under the staybooking directory.
vim Dockerfile
- 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
-
Under the staybooking directory, create another new file called app.yaml.
vim app.yaml
-
Put the following contents into the app.yaml file and quit from the editor.
-
Make sure you have all files ready.
-
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
前后端分开部署
https://stackoverflow.com/questions/45805722/gcloud-app-deploy-error-response-13-an-internal-error-occurred
find . -name 'Icon*' -type f -delete
改前端程序里的domain里
然后本地启动 localhost:3000
数据库也得记的改成云端的
Shutdown GCP Service
- Go to the Google Cloud Platform homepage at https://console.cloud.google.com.
- Open the navigation menu and choose settings under IAM & Admin.
- Use the Shutdown button to terminate all the services you’ve created on GCP.