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会议 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 Cloud GCP,然后使用Spring Cloud GCP设置一个简单的项目。

Google Cloud Platform 非常庞大!它有大量的特性。其他平台也是如此。那么,为什么选择Google Cloud Platform呢?我认为使用GCP有两个主要原因。首先,Google的数据中心无疑是世界上最先进的,其运营能力也是首屈一指的。让Google来运营和保护您的数据中心。除此之外,Google在其规模下必须解决一些令人惊叹的问题,通过GCP,我们可以从这些发展中受益。尤其是Google独有的数据服务,堪称传奇。例如, Google的Tensorflow 代表了机器学习的尖端,GCP甚至提供了对专门的、为运行TensorFlow工作负载优化的 张量处理单元 (TPU) 的访问。 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) 构件。开始一个新Spring Cloud GCP项目最简单的方法是从 Spring Initializr 生成一个新项目,然后选择GCP Support。这是最小的pom.xml构件的样子。

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 CCP与MySQL或PostgreSQL等SQL数据库进行通信。

获取 Spring 新闻通讯

通过 Spring 新闻通讯保持联系

订阅

领先一步

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

了解更多

获得支持

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件,只需一份简单的订阅。

了解更多

即将举行的活动

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

查看所有