项目作者: pacphi

项目描述 :
A Project Reactor implementation of book inventory employing Java, Spring Boot and Cloud Spanner R2DBC dependencies.
高级语言: Java
项目地址: git://github.com/pacphi/spring-books.git
创建时间: 2020-04-29T23:28:19Z
项目社区:https://github.com/pacphi/spring-books

开源协议:

下载


Spring Books Cloud Spanner R2DBC Sample

A derivative work based upon the cloud-spanner-spring-data-r2dbc-sample.

Prerequisites

If you want to run this sample on a Cloud Foundry foundation or Kubernetes cluster with cf-for-k8s installed, then you’ll need the

and valid account credentials

In addition your platform operator must have installed and configured the gcp-service-broker.

Clone

  1. git clone https://github.com/pacphi/spring-books.git

Build

  1. cd spring-books
  2. ./gradlew build

Run

This sample creates a table called BOOK on application startup, and deletes it prior to application shutdown.

Locally

Run the sample from the command line, providing the following properties

  • spanner.instance
  • spanner.database
  • gcp.project
  • gcp.service_account_key_json_file
  1. ./gradlew bootRun -Dspring-boot.run.jvmArguments="-Dspanner.instance={SPANNER-INSTANCE} -Dspanner.database={SPANNER-DATABASE} -Dgcp.project={GCP-PROJECT} -Dgcp.service_account_key_json_file={PATH-TO-ACCOUNT-KEY-JSON-FILE}"

Visit http://localhost:8080/index.html in your favorite browser.

on cf-for-k8s

Login to a foundation and target an organization and space

  1. cf api {api-host}
  2. cf auth {username} {password}
  3. cf t -o {organization} -s {space}

Create a Google Spanner service instance

  1. cf create-service google-spanner sandbox spanner-sandbox-instance

Fetch the instance id of the spanner-sandbox-instance

  1. gcloud spanner instances list

Note the NAME of the instance

Create a database within the instance

  1. gcloud spanner databases create library --instance={name}

Replace the occurrence of {name} above with instance name of your spanner-sandbox-instance.

A single service instance may support multiple databases. In this case we’re creating a database named library.

Push the app (but don’t start it up)

  1. cf push --no-start

The supplied manifest.yml sets the required environment variables.

Java Config via java-cfenv will take care to auto-fetch Google Spanner instance credentials at startup which are used to setup a connection and support on-demand database transactions.

Bind the Google Spanner service instance to the app

  1. cf bind-service spring-books spanner-sandbox-instance

Set an environment variable

  1. cf set-env spring-books GCP_SERVICE_ACCOUNT_KEY_JSON '{key-file-contents}'

We need to set an environment variable that contains the content of the service account JSON key file in order to authenticate requests from the application to the Google Spanner instance. Replace {key-file-contents} above with an actual service account key in JSON format.

Start the app

  1. cf start spring-books

Visit the route for the app you just pushed in your favorite browser.

What does this app do?

Basically allows a librarian to maintain a simple inventory of books.

Try the different actions available:

  • listing books
  • adding a new book
  • searching for a book by its ID

Teardown

  1. cf unbind-service spring-books spanner-sandbox-instance
  2. cf delete-service spanner-sandbox-instance -f
  3. cf delete spring-books -r -f