Google App Engine

note

Google App Engine - 图1

This help topic is in development and will be updated in the future.

note

Google App Engine - 图2

You can check out a full google appengine sample, here: https://github.com/ktorio/ktor-samples/tree/1.3.0/deployment/google-appengine-standard

Preparing

You first need to install the gcloud cli. You can grab it from here: https://cloud.google.com/sdk/docs/ and follow the described steps to install it.

For example, a macOS setup might look something like this:

  1. > wget https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-194.0.0-darwin-x86_64.tar.gz
  2. > tar -xzf google-cloud-sdk-194.0.0-darwin-x86_64.tar.gz
  3. > cd google-cloud-sdk
  4. > ./install.sh
  5. Welcome to the Google Cloud SDK!
  6. To help improve the quality of this product, we collect anonymized usage data
  7. and anonymized stacktraces when crashes are encountered; additional information
  8. is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose
  9. to opt out of this collection now (by choosing 'N' at the below prompt), or at
  10. any time in the future by running the following command:
  11. gcloud config set disable_usage_reporting true
  12. Do you want to help improve the Google Cloud SDK (Y/n)? n
  13. Your current Cloud SDK version is: 194.0.0
  14. The latest available version is: 194.0.0
  15. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  16. Components
  17. ├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤
  18. Status Name ID Size
  19. ├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤
  20. Not Installed App Engine Go Extensions app-engine-go 151.3 MiB
  21. Not Installed Cloud Bigtable Command Line Tool cbt 4.0 MiB
  22. Not Installed Cloud Bigtable Emulator bigtable 3.8 MiB
  23. Not Installed Cloud Datalab Command Line Tool datalab < 1 MiB
  24. Not Installed Cloud Datastore Emulator cloud-datastore-emulator 17.9 MiB
  25. Not Installed Cloud Datastore Emulator (Legacy) gcd-emulator 38.1 MiB
  26. Not Installed Cloud Pub/Sub Emulator pubsub-emulator 33.4 MiB
  27. Not Installed Emulator Reverse Proxy emulator-reverse-proxy 14.5 MiB
  28. Not Installed Google Container Local Builder container-builder-local 3.7 MiB
  29. Not Installed Google Container Registry's Docker credential helper │ docker-credential-gcr │ 2.5 MiB │
  30. │ Not Installed │ gcloud Alpha Commands │ alpha │ < 1 MiB │
  31. │ Not Installed │ gcloud Beta Commands │ beta │ < 1 MiB │
  32. │ Not Installed │ gcloud app Java Extensions │ app-engine-java │ 118.9 MiB │
  33. │ Not Installed │ gcloud app PHP Extensions │ app-engine-php │ 21.9 MiB │
  34. │ Not Installed │ gcloud app Python Extensions │ app-engine-python │ 6.2 MiB │
  35. │ Not Installed │ gcloud app Python Extensions (Extra Libraries) │ app-engine-python-extras │ 27.8 MiB │
  36. │ Not Installed │ kubectl │ kubectl │ 12.2 MiB │
  37. │ Installed │ BigQuery Command Line Tool │ bq │ < 1 MiB │
  38. │ Installed │ Cloud SDK Core Libraries │ core │ 7.4 MiB │
  39. │ Installed │ Cloud Storage Command Line Tool │ gsutil │ 3.4 MiB │
  40. └───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘
  41. To install or remove components at your current SDK version [194.0.0], run:
  42. $ gcloud components install COMPONENT_ID
  43. $ gcloud components remove COMPONENT_ID
  44. To update your SDK installation to the latest version [194.0.0], run:
  45. $ gcloud components update
  46. Modify profile to update your $PATH and enable shell command
  47. completion?
  48. Do you want to continue (Y/n)? Y
  49. The Google Cloud SDK installer will now prompt you to update an rc
  50. file to bring the Google Cloud CLIs into your environment.
  51. Enter a path to an rc file to update, or leave blank to use
  52. [/Users/user/.zshrc]:
  53. Backing up [/Users/user/.zshrc] to [/Users/user/.zshrc.backup].
  54. [/Users/user/.zshrc] has been updated.
  55. ==> Start a new shell for the changes to take effect.
  56. For more information on how to get started, please visit:
  57. https://cloud.google.com/sdk/docs/quickstarts

After that, you can start a new shell, and you should have access to the gcloud cli. For example:

  1. > gcloud --version
  2. Google Cloud SDK 194.0.0
  3. bq 2.0.30
  4. core 2018.03.16
  5. gsutil 4.29

You will also need to install some components with the cli (gcloud components install app-engine-java):

  1. > gcloud components install app-engine-java
  2. Your current Cloud SDK version is: 194.0.0
  3. Installing components from version: 194.0.0
  4. ┌────────────────────────────────────────────────────┐
  5. These components will be installed.
  6. ├──────────────────────────────┬─────────┬───────────┤
  7. Name Version Size
  8. ├──────────────────────────────┼─────────┼───────────┤
  9. gRPC python library
  10. gRPC python library 1.9.1 7.6 MiB
  11. gcloud app Java Extensions 1.9.63 118.9 MiB
  12. gcloud app Python Extensions 1.9.67 6.2 MiB
  13. └──────────────────────────────┴─────────┴───────────┘
  14. For the latest full release notes, please visit:
  15. https://cloud.google.com/sdk/release_notes
  16. Do you want to continue (Y/n)? Y
  17. ╔════════════════════════════════════════════════════════════╗
  18. ╠═ Creating update staging area ═╣
  19. ╠════════════════════════════════════════════════════════════╣
  20. ╠═ Installing: gRPC python library ═╣
  21. ╠════════════════════════════════════════════════════════════╣
  22. ╠═ Installing: gRPC python library ═╣
  23. ╠════════════════════════════════════════════════════════════╣
  24. ╠═ Installing: gcloud app Java Extensions ═╣
  25. ╠════════════════════════════════════════════════════════════╣
  26. ╠═ Installing: gcloud app Python Extensions ═╣
  27. ╠════════════════════════════════════════════════════════════╣
  28. ╠═ Creating a backup and activating a new installation ═╣
  29. ╚════════════════════════════════════════════════════════════╝
  30. Performing post-processing steps...done.
  31. Update done!

For your project, you can use gradle and the official appengine-gradle-plugin. So a build.gradle would look like this:

  1. buildscript {
  2. ext.appengine_version = '1.9.60'
  3. ext.appengine_plugin_version = '1.3.4'
  4. repositories {
  5. jcenter()
  6. }
  7. dependencies {
  8. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  9. classpath "com.google.cloud.tools:appengine-gradle-plugin:$appengine_plugin_version"
  10. }
  11. }
  12. apply plugin: 'kotlin'
  13. apply plugin: 'war'
  14. apply plugin: 'com.google.cloud.tools.appengine'
  15. // appengine does not honor this property, so we are forced to use deep Maven tree layout
  16. // webAppDirName = file('webapp')
  17. sourceSets {
  18. main.kotlin.srcDirs = [ 'src/main/kotlin' ]
  19. }
  20. repositories {
  21. jcenter()
  22. }
  23. dependencies {
  24. compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  25. compile "io.ktor:ktor-server-servlet:$ktor_version"
  26. compile "io.ktor:ktor-html-builder:$ktor_version"
  27. compile "org.slf4j:slf4j-jdk14:$slf4j_version"
  28. providedCompile "com.google.appengine:appengine:$appengine_version"
  29. }
  30. kotlin.experimental.coroutines = 'enable'
  31. task run(dependsOn: appengineRun)

Once everything is configured, you can now run the application locally, using the gradle task appengineRun:

In this case, these commands are executed in the root of the ktor-samples repository https://github.com/ktorio/ktor-samples/:

  1. ./gradlew :google-appengine-standard:appengineRun

It should start the server in http://localhost:8080/ and the admin in http://localhost:8080/_ah/admin.

Deploying

First, we need to create a project gcloud projects create demo-demo-123456 --set-as-default:

  1. > gcloud projects create demo-demo-123456 --set-as-default
  2. Create in progress for [https://cloudresourcemanager.googleapis.com/v1/projects/demo-demo-123456].
  3. Waiting for [operations/pc.7618150612308930095] to finish...done.
  4. Updated property [core/project] to [demo-demo-123456].

And then we need to create an application using gcloud app create:

  1. > gcloud app create
  2. You are creating an app for the project [demo-demo-123456].
  3. WARNING: Creating an App Engine application for a project is irreversible, and the region
  4. cannot be changed. More information about regions is at
  5. <https://cloud.google.com/appengine/docs/locations>.
  6. Please choose the region where you want your App Engine application
  7. located:
  8. [1] europe-west2 (supports standard and flexible)
  9. [2] us-central (supports standard and flexible)
  10. [3] europe-west (supports standard and flexible)
  11. [4] europe-west3 (supports standard and flexible)
  12. [5] us-east1 (supports standard and flexible)
  13. [6] us-east4 (supports standard and flexible)
  14. [7] asia-northeast1 (supports standard and flexible)
  15. [8] asia-south1 (supports standard and flexible)
  16. [9] australia-southeast1 (supports standard and flexible)
  17. [10] southamerica-east1 (supports standard and flexible)
  18. [11] northamerica-northeast1 (supports standard and flexible)
  19. [12] cancel
  20. Please enter your numeric choice: 1
  21. Creating App Engine application in project [demo-demo-123456] and region [europe-west2]....done.
  22. Success! The app is now created. Please use `gcloud app deploy` to deploy your first app.

Now we can deploy the application using gradle appengineDeploy:

  1. > gradle :google-appengine-standard:appengineDeploy
  2. Starting a Gradle Daemon (subsequent builds will be faster)
  3. Reading application configuration data...
  4. Mar 23, 2018 6:32:09 AM com.google.apphosting.utils.config.IndexesXmlReader readConfigXml
  5. INFORMATION: Successfully processed /Users/user/projects/ktor-samples/deployment/google-appengine-standard/build/exploded-google-appengine-standard/WEB-INF/appengine-generated/datastore-indexes-auto.xml
  6. Beginning interaction for module default...
  7. 0% Scanning for jsp files.
  8. 0% Generated git repository information file.
  9. Success.
  10. Temporary staging for module default directory left in /Users/user/projects/ktor-samples/deployment/google-appengine-standard/build/staged-app
  11. Services to deploy:
  12. descriptor: [/Users/user/projects/ktor-samples/deployment/google-appengine-standard/build/staged-app/app.yaml]
  13. source: [/Users/user/projects/ktor-samples/deployment/google-appengine-standard/build/staged-app]
  14. target project: [demo-demo-123456]
  15. target service: [default]
  16. target version: [20180323t063212]
  17. target url: [https://demo-demo-123456.appspot.com]
  18. Beginning deployment of service [default]...
  19. Some files were skipped. Pass `--verbosity=info` to see which ones.
  20. You may also view the gcloud log file, found at
  21. [/Users/user/.config/gcloud/logs/2018.03.23/06.32.10.739209.log].
  22. #============================================================#
  23. #= Uploading 38 files to Google Cloud Storage =#
  24. #============================================================#
  25. File upload done.
  26. Updating service [default]...
  27. ..............done.
  28. Setting traffic split for service [default]...
  29. .......done.
  30. Deployed service [default] to [https://demo-demo-123456.appspot.com]
  31. You can stream logs from the command line by running:
  32. $ gcloud app logs tail -s default
  33. To view your application in the web browser run:
  34. $ gcloud app browse
  35. BUILD SUCCESSFUL in 42s
  36. 6 actionable tasks: 2 executed, 4 up-to-date

Now you can view your application in your browser with gcloud app browse. It will open the application. In this case: https://demo-demo-123456.appspot.com