使用 mysql 在 google 云上使用 java11 部署 Spring 引导应用程序

Deploying a Spring boot application with mysql on google cloud with java11

在 Google Cloud 上配置 mysql 后,我尝试使用 Google App Engine 部署进行部署。在本地主机上它 运行 是完美的,但是当我尝试在服务器上 运行 时它有一些问题。我找到了降低 java 版本的解决方案,但我需要 java 11,是否有任何解决方案可以使用 java 11 部署我的应用程序?

错误:

File upload done.
ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: Frontend automatic scaling should NOT have the following parameter(s): [max_total_instances, min_total_instances] (or "version.env" should be set to "flex"
- '@type': type.googleapis.com/google.rpc.BadRequest
  fieldViolations:
  - description: 'Frontend automatic scaling should NOT have the following parameter(s):
      [max_total_instances, min_total_instances] (or "version.env" should be set to
      "flex"'
    field: version.automatic_scaling
Failed to deploy '[2021-11-10 23:11:21] Maven build: backend. Project: buildtogether. Version: auto': Deployment failed with exit code: 1
Please make sure that you are using the latest version of the Google Cloud SDK.
Run ''gcloud components update'' to update the SDK. (See: https://cloud.google.com/sdk/gcloud/reference/components/update.)

我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ubul_studio</groupId>
    <artifactId>build_together</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>build_together</name>
    <description>Backend for the Build Together mobile and webapp</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <!-- Add Spring Cloud GCP Dependency BOM -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>2.0.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>com.neovisionaries</groupId>
            <artifactId>nv-i18n</artifactId>
            <version>1.22</version>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
        </dependency>
    </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>2.4.0</version>
                <configuration>
                    <version>1</version>
                    <projectId>buildtogether</projectId>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的app.yaml:

runtime: java11

instance_class: F2
env_variables:
  BUCKET_NAME: "example-gcs-bucket"

handlers:
  - url: /stylesheets
    static_dir: stylesheets

  - url: /.*
    secure: always
    redirect_http_response_code: 301
    script: auto

automatic_scaling:
  min_num_instances: 1
  max_num_instances: 8

我的application.yml:

spring:
  cloud:
    gcp:
      sql:
        database-name: buildtogether
        instance-connection-name: buildtogether:europe-central2:buildtogether
  datasource:
    password: buildtogether
    username: root
    url: jdbc:mysql://34.116.151.33:3306/buildtogether

  profiles:
    active: mysql

  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL57InnoDBDialect


  liquibase:
    change-log: classpath:database/db.changelog-master.xml
    default-schema: buildtogether
  sql:
    init:
      mode: always

请参阅 app.yml 的文档。我认为您应该指定 min_instancesmax_instances,而不是 min_num_instancesmax_num_instances

min_num_instancesmax_num_instances 适用于 python 运行时。