Bootiful GCP:使用 Spring Cloud GCP 进行关系数据访问 (2/8)

工程 | Josh Long | 2018 年 8 月 23 日 | ...

大家好 Spring 爱好者!在这个简短的 8 部分系列中,我们将了解 Spring Cloud 与 Google Cloud Platform 的集成,称为 Spring Cloud GCP。Spring Cloud GCP 代表了 Google 和 Pivotal 之间共同努力的成果,旨在为 Spring Cloud 开发人员在使用 Google Cloud Platform 时提供一流的体验。Pivotal Cloud Foundry 用户将享受甚至更容易与 GCP 服务代理集成。我写这些文章时得到了 Google Cloud Developer Advocate,也是我的好朋友 Ray Tsang 的投入。您还可以在我们的 Google Next 2018 会议 Bootiful Google Cloud Platform 中了解 Spring Cloud GCP 的演练。谢谢你,伙计!和往常一样,如果您有反馈,我很乐意听取您的意见

本系列共有八篇文章。 这里是全部

](https://springframework.org.cn/blog/2018/09/06/bootiful-gcp-supporting-observability-with-spring-cloud-gcp-stackdriver-trace-6-8)

  • [Bootiful GCP:使用 Spring Cloud GCP 连接到其他 GCP 服务 (7/8)

](https://springframework.org.cn/blog/2018/09/10/bootiful-gcp-use-spring-cloud-gcp-to-connect-to-other-gcp-services-7-8)

在本期中,我们将了解如何将基于 Spring Boot 的应用程序连接到在 Google Cloud Platform 上运行的 SQL 数据存储,例如 PostgreSQL 或 MySQL。 GCP 提供托管的 SQL 服务,包括 MySQL 和 PostgreSQL。 让我们配置一个 MySQL 实例,并从基于 Spring Boot 的应用程序中使用它。 您需要先在 Google Cloud 中启用 SQL API

 gcloud services enable sqladmin.googleapis.com

然后,您需要在特定区域中配置一个新的 Google Cloud SQL 实例。 默认值为 MySQL。 (这可能需要几分钟!)

gcloud sql instances create reservations --region=us-central1

然后,在该 Google Cloud SQL 实例中配置一个新的数据库

gcloud sql databases create reservations --instance reservations

有几种方法可以建立与此 CloudSQL 实例的安全连接,例如,使用 SSL Socket Factory 配置 JDBC 驱动程序和/或使用 SQL 代理。 为了简化所有配置,您可以添加 GCP MySQL 启动器:org.springframework.cloud : spring-cloud-gcp-starter-sql-mysql。 此启动器,再加上少量配置,可以将您的应用程序连接到平台。

您说什么配置? 好吧,Spring 应用程序具有 bean 定义(在 Java 代码中)和属性定义(在任何数量的不同属性源中,包括 .yaml.properties 文件,正如我们稍后将看到的,GCP 的 RuntimeConfig 机制)。 您可以使用配置文件有选择地激活特定的 bean 定义和属性定义。 Spring 配置文件是任意标签,用于标识可以有选择地激活的不同 bean 和属性配置。 在此示例中,我们使用 mysql 配置文件。

让我们看一下 mysql 配置文件的配置,在 src/main/resources/application.properties

application.properties。

spring.cloud.gcp.sql.database-name=reservations
spring.cloud.gcp.sql.instance-connection-name=pgtm-jlong:us-central1:reservations

spring.datasource.initialization-mode=always
spring.datasource.hikari.maximum-pool-size=2
  • 这些属性标识客户端应连接到哪个 GCP SQL 实例和数据库
  • 告诉 Spring 通过运行 src/main/resources/schema.sql(如果存在)来初始化模式

src/main/resources/schema.sql 包含创建预订表的 DDL。

在此示例中,我们让 Spring Boot 配置数据库。 还有其他方法。 我们可以使用 gcloud sql connect $GCP_SQL_INSTANCE,其中 $GCP_SQL_INSTANCE 应替换为您的 GCP SQL 实例名称。 在这种情况下,SQL 实例名称为 reservations。 这会将您转储到连接到远程数据库的 MySQL 会话中,您可以在其中以交互方式发出相关的模式 DDL。

这是一个演示,因此让我们也安装一些示例数据。 您可以创建一个文件 src/main/resources/data.sql,Spring 将在应用程序启动时执行该文件,或者您可以使用 gcloud sql connect。 无论哪种方式,请运行以下语句。

insert into reservations(name) values('ray');
insert into reservations(name) values('josh');

现在您可以像使用任何其他 SQL 数据库一样,从 Spring 应用程序访问该数据库。

package com.example.gcp.mysql;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.Collection;

@SpringBootApplication
public class MySqlApplication {

        private final Log log = LogFactory.getLog(getClass());

        private final JdbcTemplate template;

        private final RowMapper<Reservation> rowMapper =
            (rs, rowNum) -> new Reservation(rs.getLong("id"), rs.getString("name"));

        MySqlApplication(JdbcTemplate template) {
                this.template = template;
        }

        @EventListener(ApplicationReadyEvent.class)
        public void ready() {
                Collection<Reservation> reservations = this.template
                    .query("select * from reservations", this.rowMapper);
                reservations.forEach(reservation -> log.info("reservation: " + reservation.toString()));
        }

        public static void main(String args[]) {
                SpringApplication.run(MySqlApplication.class, args);
        }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Reservation {
        private Long id;
        private String reservationName;
}

运行应用程序并确认结果反映在输出中。

此时您应该熟悉了; 您可以使用 Spring Data JPA 和 Spring Data REST、JOOQ、MyBatis 或任何各种基于 JDBC 的技术,以及 Spring Boot,来使用此 GCP 管理的 MySQL 实例。

在这篇文章中,我们使用了由 Google Cloud 管理的 MySQL。 这并不是特别有趣; 这不是您应该使用 GCP 的原因。 任何人都可以运行 MySQL! 在下一期中,我们将研究将 Google Cloud Spanner 与 Spring Cloud GCP 一起使用。 请务必下周一回来查看下一期。

获取 Spring 新闻资讯

通过 Spring 新闻资讯保持联系

订阅

抢占先机

VMware 提供培训和认证,以加速您的进步。

了解更多

获取支持

Tanzu Spring 在一个简单的订阅中提供对 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件。

了解更多

即将举行的活动

查看 Spring 社区中所有即将举行的活动。

查看全部