【问题标题】:spring boot + gcloud + "502 Bad Gateway"spring boot + gcloud + “502 Bad Gateway”
【发布时间】:2020-04-05 06:54:58
【问题描述】:

我正在尝试在 gcloud 上部署这个项目: https://github.com/ITger/PolishAPI_sample 本地工作正常,但在 gcloud 上我总是收到 502 错误。

mvn clean package appengine:deploy 命令运行没有问题。

在日志(gcloud app logs tail)中没有错误,只有信息和调试消息。 一个星期以来,我一直在寻找解决方案并尝试在互联网上找到许多建议,我按照谷歌、dzone 等教程进行操作,但没有运气。

感谢您的帮助

已编辑(回复 John Hanley): 广告1:我的代码超过165个java文件,怎么显示???

广告 2:app.yaml:

runtime: java
env: flex
instance_class: F2
runtime_config:
  jdk: openjdk8
#env_variables:
#  SPRING_PROFILES_ACTIVE: "gcp"
handlers:
  - url: /.*
    script: this field is required, but ignored
readiness_check:
  check_interval_sec: 15
  timeout_sec: 14
  failure_threshold: 2
  success_threshold: 2
  app_start_timeout_sec: 1200

ad 3:正如我所说,日志中只有 NO ERRORS 有弹簧信息和调试消息。

application.properties 文件:

debug=true
spring.application.name=polish-api-psd2
springfox.documentation.swagger.v2.path=/api-docs
server.port=${port:8080}
spring.jackson.date-format=pl.itger.PolishAPI.RFC3339DateFormat
#Mongo Config
spring.data.mongodb.database=PolishAPI
spring.data.mongodb.uri=mongodb+srv://polishapi-user:SECRET CLUSTER DATA
spring.data.mongodb.auto-index-creation=true
log4j.category.org.springframework.data.mongodb=DEBUG
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %40.40c:%4L - %m%n
logging.level.pl.itger=DEBUG
logging.level.root=WARN

应用引擎防火墙规则:

Priority 
1000
Action on match
Allow
IP range 
0.0.0.0/0

当我在 Google Cloud Shell 控制台中运行“**mvn clean spring-boot:run -P cloud-gcp**”时,应用启动良好,在 Web 预览中,应用通过预定义的 swagger ui 页面启动良好:

https://8080-dot-10127310-dot-devshell.appspot.com/

显示所有模型和 api。 当我在浏览器中调用时,“https://8080-dot-10127310-dot-devshell.appspot.com/api-docs”也能正常工作。使用 curl 调用该 url 上的任何休息服务都会导致“访问被禁止”。

但是,在mvn -DskipTests package appengine:deploy -P cloud-gcp 之后没有任何效果,只有502 Bad Gateway

[INFO] GCLOUD: Setting traffic split for service [default]...
[INFO] GCLOUD: ..............................................................done.
[INFO] GCLOUD: Deployed service [default] to [https://polish-api-psd2.appspot.com]
[INFO] GCLOUD:
[INFO] GCLOUD: You can stream logs from the command line by running:
[INFO] GCLOUD:   $ gcloud app logs tail -s default
[INFO] GCLOUD:
[INFO] GCLOUD: To view your application in the web browser run:
[INFO] GCLOUD:   $ gcloud app browse
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  09:15 min
[INFO] Finished at: 2019-12-12T15:49:39+01:00
[INFO] ------------------------------------------------------------------------
p_zerynger@cloudshell:~/PolishAPI_sample/server (polish-api-psd2)$

完整的日志(很短,与“mvn spring-boot:run -P cloud-gcp”之后的不同,我不知道为什么):

ar!/, jar:file:/app.jar!/BOOT-INF/lib/spring-data-jpa-2.1.10.RELEASE.jar!/, jar:file:/app.jar!/BOOT-INF/lib/spring-orm-5.2.2.RELEASE.jar!/, jar:file:/app.jar!/BOOT-INF/lib/spring-jdbc-5.2.2.RELEASE.jar!/, jar:file:/app.jar!/BOOT-INF/lib/aspectjrt-1.9.5.jar!/, jar:file:/app.jar!/BOOT-INF/lib/lombok-1.18.10.jar!/, jar:file:/app.jar!/BOOT-INF/lib/core-1.5.3.jar!/, jar:file:/app.jar!/BOOT-INF/lib/proxytoys-1.0.jar!/, jar:file:/app.jar!/BOOT-INF/lib/gson-2.8.6.jar!/, jar:file:/app.jar!/BOOT-INF/lib/annotations-17.0.0.jar!/, jar:file:/app.jar!/BOOT-INF/lib/jaxb-api-2.3.1.jar!/, jar:file:/app.jar!/BOOT-INF/lib/javax.activation-api-1.2.0.jar!/]
2019-12-12 14:48:53 default[1]
2019-12-12 14:48:53 default[1]    .   ____          _            __ _ _
2019-12-12 14:48:53 default[1]   /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
2019-12-12 14:48:53 default[1]  ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2019-12-12 14:48:53 default[1]   \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
2019-12-12 14:48:53 default[1]    '  |____| .__|_| |_|_| |_\__, | / / / /
2019-12-12 14:48:53 default[1]   =========|_|==============|___/=/_/_/_/
2019-12-12 14:48:53 default[1]   :: Spring Boot ::        (v2.2.2.RELEASE)
2019-12-12 14:48:53 default[1]
2019-12-12 14:48:54 default[1]  2019-12-12 14:48:54.093  INFO 1 --- [           main] p.i.P.PolishAPI_2_1_2_SpringBoot         : Starting PolishAPI_2_1_2_SpringBoot v1.0.1 on e1b35acb6032 with PID 1 (/app.jar started by root in /)
2019-12-12 14:48:54 default[1]  2019-12-12 14:48:54.096 DEBUG 1 --- [           main] p.i.P.PolishAPI_2_1_2_SpringBoot         : Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE
2019-12-12 14:48:54 default[1]  2019-12-12 14:48:54.098  INFO 1 --- [           main] p.i.P.PolishAPI_2_1_2_SpringBoot         : No active profile set, falling back to default profiles: default
2019-12-12 14:48:54 default[1]  2019-12-12 14:48:54.102 DEBUG 1 --- [           main] o.s.boot.SpringApplication               : Loading source class pl.itger.PolishAPI.PolishAPI_2_1_2_SpringBoot
2019-12-12 14:48:54 default[1]  2019-12-12 14:48:54.341 DEBUG 1 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'jar:file:/app.jar!/BOOT-INF/classes!/application.properties' (classpath:/application.properties)
2019-12-12 14:48:54 default[1]  2019-12-12 14:48:54.345 DEBUG 1 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6c3708b3
2019-12-12 14:52:42 default[1]  I1212 14:51:56.693214    26 callbacks_monitor.cc:66] Cloud Debugger call "JVMTI:ClassPrepare" completed after 25847 ms

+++++++++++ 在云 http 负载均衡器日志中,我发现了这个:

jsonPayload: {
  @type: "type.googleapis.com/google.cloud.loadbalancing.type.LoadBalancerLogEntry"   
  statusDetails: "failed_to_connect_to_backend"   
 }

太疯狂了……现在超时:

[INFO] GCLOUD: ERROR: (gcloud.app.deploy) Error Response: [4] App Engine Flexible timed out while configuring resources, internal operation projects/polish-api-psd2/regions/europe-west1/operations/3343fc51-2991-4da1-ba45-128bd897d92f

【问题讨论】:

  • 1) 在您的问题中显示您的代码。 2) 对于 App Engine,包括 app.yaml。 3) 显示准确的错误信息。 4) 您使用的是哪个 App Engine? 5)您的问题可能是您的应用程序没有在正确的端口上侦听。 6)我们不会去链接,你必须在你的问题中包含所有需要的东西。链接断开或失效,您可能犯了错误等。
  • 当您迁移到云端时,您需要打开一些端口才能进行交互。你这样做了吗?默认情况下,所有端口都被阻止。
  • 在本地和 AppEngine 中测试的 URL 是什么? (复制完整的 URL,包括 HTTPS,但隐藏您的项目名称)您在 AppEngine 上有自定义域吗?
  • 您的机器和 AppEngine 之间是否有代理服务器?
  • 也许我以错误的方式打开了端口 - 怎么办?我没有自定义域。没有代理服务器。谢谢。

标签: java spring-boot google-cloud-platform gcloud


【解决方案1】:

每个部署都有一个分配给它的版本号。 验证该版本号是否出现在“app engine”下的“versions”部分 您的 GCP 项目。如果确实出现了版本号,则会在 StackDriver 中提供日志,详细说明发生的任何可能的部署错误。

如果没有出现版本号,则重新部署项目,这一次启用详细标志以生成部署日志。[1]

最后,尝试部署一个最低限度的“Hello World”应用程序,如果问题与代码无关,看看是否可行。

[1]https://cloud.google.com/sdk/gcloud/reference/#--verbosity

【讨论】:

  • 版本状态流量分配实例运行时环境大小部署诊断配置1 Serving 100% 6 java Flexible 0 B 12.12.2019, 8:11:08 PM by p.zerynger@gmail.com
  • 通过消除过程,我们现在需要确定问题是否与您的代码、配置或基础架构有关。您能否使用与之前项目相同的 app.yaml 文件部署“Hello World”应用程序,看看会发生什么?
  • springboot 最低 HelloWorld 也不起作用 - 超时:......失败。 [INFO] GCLOUD: ERROR: (gcloud.app.deploy) 错误响应:[4] 等待应用基础设施恢复健康时超时。 [信息] -------- [信息] 构建失败 [信息] ------------------------------- ----------------------------------------- [INFO] 总时间:30:19 min [INFO] 完成时间:2019-12-13T19:27:39+01:00 对于这个测试,我删除了 gcloud 上的所有项目并创建了一个新项目。在 intellij 上运行良好。一分钱一分货……
  • 在同一个 HelloWorld 上再次尝试,完全相同的来源:GCLOUD: DONE [INFO] GCLOUD: ---------------------- -------------------------------------------------- -------- [INFO] GCLOUD:[INFO] GCLOUD:错误:(gcloud.app.deploy)INVALID_ARGUMENT:超出了以下配额:IN_USE_ADDRESSES(配额:8,已使用:8 + 需要:1)。 [信息] --------------------------------------------- ------------------------- [信息] 构建失败 [信息] ----------------- -------------------------------------------------- ----- [INFO] 总时间:04:08 min [INFO] 完成时间:2019-12-13T20:50:21+01:00
  • 你能在没有 Spring Boot 的情况下再次部署最低限度的“Hello World”应用程序吗?
【解决方案2】:

添加

resources:
  memory_gb: 4

在您的 yaml 文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-09
    • 2020-07-12
    • 2023-03-20
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多