标题: Spring Cloud 3.x 集成 Google Datastore快速入门DEMO [打印本页] 作者: 宁睿 时间: 2025-1-16 15:55 标题: Spring Cloud 3.x 集成 Google Datastore快速入门DEMO 1. 介绍
Google Cloud Datastore 是一种存储大量实时数据的高扩展性和坚固性的无模型文档型数据库,适合于实时应用。Spring Cloud 提供了轻量级应用的快速开发本领,通过美满的插件类层形成一个完备的应用开发组件体系。利用 Spring Cloud 3.x 可以完善场景化地集成 Google Cloud Datastore,轻松实现与 Google Cloud 环境的通信和数据操作。
2.数据库创建
创建数据库
Create a new Cloud Datastore database in your Google Cloud project if this has not already been done to allow Cloud Datastore to create and store entities. https://console.cloud.google.com/datastore/databases
赋予项目中利用的用户访问权限
3. 原理
在 Spring Cloud 中,通过 Spring Data 层提供对 Google Cloud Datastore 的高度封装,利用者可以像操作当地数据一样,优雅场景化地操作 Datastore 中的文档。
Spring Data 利用 Google Cloud Datastore 提供的应用端 API。
通过 DatastoreTemplate 调用基础 CRUD 操作方法,并支持自定义查询和处理。
在 Spring Cloud 中,利用配置文件就能实现 Google Cloud Datastore 的快速配置和部署。
GitHub - Harries/springcloud-demo: Spring Cloud tutorial about hystrix,eureka,config,admin,skywalking( Spring Cloud GCP/spring-cloud-gcp-data-datastore-basic-sample)
5. 测试
1. 启动应用
确保以上配置正确后,启动 Spring Boot 应用,利用 Postman 或 curl 调用程序。(因为网络原因,最好在cloud shell editor启动)
2. 提交请求例子
创建4本书
curl --location --request POST 'localhost:8080/saveBook' \
--header 'Content-Type: application/json' \
--data-raw '{
"title": "The Moon Is a Harsh Mistress",
"author": "Robert A. Heinlein",
"year": 1966
}'
curl --location --request POST 'localhost:8080/saveBook' \
--header 'Content-Type: application/json' \
--data-raw '{
"title": "Stranger in a Strange Land",
"author": "Robert A. Heinlein",
"year": 1961
}'
curl --location --request POST 'localhost:8080/saveBook' \
--header 'Content-Type: application/json' \
--data-raw '{
"title": "The Crack in Space",
"author": "Philip K. Dick",
"year": 1966
}'
curl --location --request POST 'localhost:8080/saveBook' \
--header 'Content-Type: application/json' \
--data-raw '{
"title": "Ubik",
"author": "Philip K. Dick",
"year": 1969
}'
复制代码
查询全部的书
curl --location --request GET 'localhost:8080/findAllBooks'
复制代码
根据作者查询
curl --location --request GET 'localhost:8080/findByAuthor?author=Robert A. Heinlein'
复制代码
根据日期筛选
curl --location --request GET 'localhost:8080/findByYearGreaterThan?year=1960'
复制代码
根据作者和日期筛选
curl --location --request GET 'localhost:8080/findByAuthorYear?author=Robert A. Heinlein&year=1966'