Bootiful GCP:Spring Cloud for Google Cloud Platform 入门 (1/8)

技术 | Josh Long | 2018 年 8 月 20 日 | ...

各位 Spring 粉丝们,大家好!在这个由 8 部分组成的简短系列中,我们将探讨 Spring Cloud 与 Google Cloud Platform 的集成,即 Spring Cloud GCP。Spring Cloud GCP 是 Google 和 Pivotal 的一项联合努力,旨在为使用 Google Cloud Platform 的 Spring Cloud 开发者提供一流的体验。Pivotal Cloud Foundry 用户可以享受到与 GCP 服务代理更轻松的集成。我撰写这些文章时,参考了 Google Cloud 开发者布道师、我的朋友 Ray Tsang 的意见。您也可以在 Google Next 2018 的会议中观看 Spring Cloud GCP 的演练,会议主题是Bootiful Google Cloud Platform。谢谢我的朋友!一如既往,如果您有任何反馈,欢迎与我联系

本系列共有八篇文章。如下所示:

](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 Cloud GCP,然后搭建一个使用 Spring Cloud GCP 的简单项目。

Google Cloud Platform 非常庞大!它拥有海量功能。但其他平台也一样。那么,为什么选择 Google Cloud Platform 呢?我认为使用 GCP 有两大原因。首先,Google 的数据中心无疑是世界上最先进的,其运营智慧首屈一指。让 Google 运营和保护您的数据中心。除此之外,Google 必须解决其大规模运行中遇到的许多令人惊叹的问题,通过 GCP,我们可以从这些发展中受益。那些 Google 独有的数据服务堪称传奇。例如,Google 的 Tensorflow 代表了机器学习的尖端水平,GCP 甚至提供了对专用应用特定集成电路 (ASICs) 张量处理单元 (TPUs) 的访问,这些单元针对运行 TensorFlow 工作负载进行了优化。Google 的 Spanner 是唯一一个为云构建的企业级、全球分布式且强一致性的数据库服务,专门用于结合关系型数据库结构的优势和非关系型数据库的横向扩展能力。

身份验证

您需要注册一个GCP 账号。为了在您的本地机器上使用 Spring Cloud GCP 项目,您需要在 GCP 上设置一个项目并安装 gcloud CLI

在使用 GCP 进行任何工作之前,有一些事情需要了解和完成。首先,您需要登录。运行以下命令登录 GCP

gcloud auth application-default login

这将方便地提供默认凭据,支持您与平台进行交互。在这种情况下,您表示希望允许某些操作代表进行。

有些操作独立于特定用户持续进行。它们可能需要代表您部分权限的细粒度权限。它们可能独立于给定用户运行,例如批处理作业或每晚运行的任务。在这种情况下,使用服务账号是有意义的。稍后我们在研究跟踪时,需要配置一个服务账号

了解您的项目 ID 非常有用。你会发现很多操作都需要知道这个值。我有一个类似这样的小脚本,在我的 ~/.bashrc 中运行,并应用到每个 shell。

export PROJECT_ID=$(gcloud config list --format 'value(core.project)')

在这些示例中,我倾向于使用 CLI,但您想做的几乎所有事情也可以通过 Web 控制台完成。

Spring Cloud GCP 入门

至少目前,您需要 Spring Cloud Spring Cloud GCP 的物料清单 (BOM) Artifact。启动一个新的 Spring Cloud GCP 项目最简单的方法是从 Spring Initializr 生成一个新项目并选择 GCP Support。以下是一个最小的 pom.xml Artifact 的样子。

一个基本的 Spring Cloud GCP 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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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>
        <spring-cloud-gcp.version>1.0.0.RELEASE</spring-cloud-gcp.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>${spring-cloud-gcp.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

在本期中,我们学习了如何搭建一个基本的 Spring Cloud GCP 项目。下周四的下一期中,我们将探讨如何使用 Spring Cloud GCP 连接到 MySQL 或 PostgreSQL 等 SQL 数据库。

订阅 Spring 电子报

通过 Spring 电子报保持联系

订阅

领先一步

VMware 提供培训和认证,助力您快速取得进展。

了解更多

获取支持

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

了解更多

即将到来的活动

查看 Spring 社区所有即将到来的活动。

查看全部