【问题标题】:Spring Boot REST on Google App Engine throws 502Google App Engine 上的 Spring Boot REST 抛出 502
【发布时间】:2017-09-29 20:29:07
【问题描述】:

我已经为此苦苦挣扎了大约一个星期,但我在 SO 上看到的任何东西都没有为我工作。我有一个基于 Spring Boot 构建的 REST API,我正在尝试将其部署到 Google App Engine。在本地运行很好,当我在 GAE 模拟器上运行它时也可以正常运行;但是,一旦我使用 mvn appengine:deploy 进行部署,我会成功构建,但是在尝试端点时,我只会得到 502。我不确定日志在哪里,所以这真的让我很头疼。

我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.admin</groupId>
  <artifactId>admin-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- Exclude this for deployment only -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- other project dependencies -->
    <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>5.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.10.2</version>
    </dependency>
    <!-- end other project specific dependencies -->

    <!-- Dependencies provided during deployment -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- End dependencies for deployment -->

    <!-- Dependencies for local -->
    <!--        <dependency> -->
    <!--            <groupId>org.springframework.boot</groupId> -->
    <!--            <artifactId>spring-boot-starter-tomcat</artifactId> -->
    <!--            <scope>provided</scope> -->
    <!--        </dependency> -->
    <!-- End dependencies for local -->
  </dependencies>

  <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.3.1</version>
            <configuration>
                <project>project-id-from-GAE-here</project
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

我的应用程序类是一个简单的带注释的@SpringBootApplication 类。我有 2 个带有 @RestController 注释的控制器和简单的 @RequestMapping 方法。其中一个 REST 控制器用于 /_ah/health 端点,该端点返回 200,因为一篇文章提到由于缺少运行状况检查端点而触发了持续重启。

我有一个简单的 application.yml 用于我的值注入,并创建了一个 app.yaml 文件放在 src/main/appengine

runtime: java
env: flexible
threadsafe: true
manual_scaling:
  instances: 1
handlers:
- url: /.*
  script: this field is required, but ignored
runtime_config:
  jdk: openjdk8

我对此完全不知所措。这是我的第一次 GAE 部署,希望还有更多的工作要做。 (我还没有使用 Docker,因为我使用的是 Windows 10 机器,当我在其上加载 Docker 时会中断。)

更新 我确实注意到我忘记将我的项目 ID 放在 maven 插件下。一旦我这样做了,我就会收到关于找不到 app.yaml 的错误。我意识到,由于我使用的是灵活的环境,因此我不需要 appengine-web.xml,而且我的文件被误命名为 app.yml 而不是 app.yaml。我用这个更新了我的问题,成功部署后我仍然收到 502。

【问题讨论】:

  • 我在使用 spring security 时遇到了相同的 502。我在 app.yaml 中指定了资源以改进机器。然后它解决了。

标签: java spring google-app-engine spring-boot maven-gae-plugin


【解决方案1】:

我意识到我在 GAE 的试用期包括技术支持,因此我联系了他们。要求我的 pom 和 app.yaml。他们的回应是在我的 app.yaml 中添加以下内容“由于 Java 以消耗大量内存而闻名,因此消耗的开销进程超过了大约 0.4GB 值”

resources:
  cpu: 2
  memory_gb: 2.3
  disk_size_gb: 10
  volumes:
  - name: ramdisk1
    volume_type: tmpfs
    size_gb: 0.5

他们还提供了这个链接供参考:https://cloud.google.com/appengine/docs/flexible/java/configuring-your-app-with-app-yaml#resource-settings

一旦我这样做了,我的应用程序就可以正常工作了。

编辑

如果您有一个简单的应用程序,请设置cpu:1,因为您需要按 CPU 小时数计费,而您的每日配额是 28 个,如果使用 2 个 CPU,您最终会得到 48 个 CPU 小时数。我在 1 个月内用完了 300 美元的免费信用额度,因为他们的教程设置了一个 RDB(80 美元),它没有被使用,也没有显示杀死项目。还要确保在部署新内容时删除旧版本,否则您将需要为每个已启动的版本付费。

【讨论】:

  • 我试图弄清楚发生了什么,直到我找到你的答案。非常感谢!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 2021-11-02
  • 1970-01-01
  • 2021-03-31
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多