抢占先机
VMware 提供培训和认证,以加速您的进步。
了解更多大家好,Spring 爱好者!在这个简短的 8 部分系列中,我们将了解 Google Cloud Platform 的 Spring Cloud 集成,称为 Spring Cloud GCP。Spring Cloud GCP 代表 Google 和 Pivotal 之间的共同努力,旨在为 Spring Cloud 开发人员在使用 Google Cloud Platform 时提供一流的体验。 Pivotal Cloud Foundry 用户将享受更轻松的与 GCP 服务 broker 集成。我在撰写这些文章时得到了 Google Cloud Developer Advocate 和我的好伙伴 Ray Tsang 的投入。您还可以在我们的 Google Next 2018 会议 Bootiful Google Cloud Platform 中观看 Spring Cloud GCP 的演示。谢谢你,伙伴!一如既往,如果您有反馈,我很乐意听到您的声音。
该系列共有八篇文章。 以下是全部文章
Spring Cloud GCP 项目致力于提供与 Spring 和一些非常适合 Spring 的 GCP 服务的集成。 但 GCP 是广阔的! 您可以通过 他们的直接 Java SDK 甚至直接通过他们的 REST API 来使用许多其他服务。 Spring Cloud GCP 也可以使使用这些 API 变得更容易! 在本节中,我们将与 Google Cloud Vision API 集成,该 API 支持分析图像和进行特征检测。
与往常一样,您需要启用 API
gcloud services enable vision.googleapis.com
当您使用 Spring Cloud GCP 中的自动配置时,它们会方便地获取所需的 OAuth 范围,以便与代表您的给定 API 一起使用,并且您永远不必担心它。 对于其他服务,我们需要自己完成这项工作。 幸运的是,这很容易。 使用spring.cloud.gcp.credentials.scopes
属性获取一个通用的、平台范围的、包罗万象的范围,该范围可用于请求所有基本 Google Cloud Platform API 的权限。
src/main/resources/applications.properties。
spring.cloud.gcp.credentials.scopes=https://www.googleapis.com/auth/cloud-platform
spring.cloud.gcp.credentials.encoded-key=FIXME
就这样! 现在您可以随心所欲地使用 API。 让我们启动一个简单的 REST API,您可以通过该 API 将图像作为多部分文件上传发布,并让 Google Cloud Vision API 进行特征检测。
package com.example.gcp.vision;
import com.google.api.gax.core.CredentialsProvider;
import com.google.cloud.vision.v1.*;
import com.google.protobuf.ByteString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Collections;
// curl -F "image=@$HOME/Pictures/soup.jpg" http://localhost:8080/analyze
@SpringBootApplication
public class VisionApplication {
@Bean
ImageAnnotatorClient imageAnnotatorClient(
CredentialsProvider credentialsProvider) throws IOException {
ImageAnnotatorSettings settings = ImageAnnotatorSettings
.newBuilder()
.setCredentialsProvider(credentialsProvider)
.build();
return ImageAnnotatorClient.create(settings);
}
@Slf4j
@RestController
public static class ImageAnalyzerRestController {
private final ImageAnnotatorClient client;
private final Feature labelDetection = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
private final Feature textDetection = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();
ImageAnalyzerRestController(ImageAnnotatorClient client) {
this.client = client;
}
@PostMapping("/analyze")
String analyze(@RequestParam MultipartFile image) throws IOException {
byte[] data = image.getBytes();
ByteString imgBytes = ByteString.copyFrom(data);
Image img = Image.newBuilder().setContent(imgBytes).build();
AnnotateImageRequest request = AnnotateImageRequest
.newBuilder()
.addFeatures(this.labelDetection)
.addFeatures(this.textDetection)
.setImage(img)
.build();
BatchAnnotateImagesResponse responses = this.client
.batchAnnotateImages(Collections.singletonList(request));
AnnotateImageResponse reply = responses.getResponses(0);
return reply.toString();
}
}
public static void main(String args[]) {
SpringApplication.run(VisionApplication.class, args);
}
}
MultipartFile
,我们可以轻松地从中提取字节以馈送到此 API 中。您可以使用curl
或任何其他通用 HTTP 客户端将图像 POST 到此端点。 这是它的工作方式
curl
:
curl -F "image=@/home/jlong/Desktop/soup.jpg" http://localhost:8080/analyze
有无数其他 API 可以与您合作! 在这里,我们才刚刚开始了解可能的事情。 查看此服务目录! 有诸如 Google Cloud DataStore、Google Storage、Firebase、BigQuery、Apigee、视频流服务、IoT 服务、机器学习、Google Tensorflow、Google Dataflow、Google Cloud AutoML、Cloud Natural Language、Cloud Speech-to-Text、Cloud Text-to-Speech、基因组 API、视频智能以及更多内容。