Spring Cloud GCP provides various cloud computing services among them one of the excellent service provided by Google is Cloud Spanner.
What is Cloud Spanner?
Cloud Spanner is a globally distributed, highly scalable databases with strong consistency and transactional capabilities.
Advantages over Cloud SQL
I have already mentioned that Cloud Spanner is globally distributed, highly scalable databases. Cloud SQL is not globally distributed and highly scalable databases. So, based on the requirement, you have to choose the Database. Each and every database has its advantages and disadvantages. Now based on the advantages one has to pick or select the right database based on the requirements.
Integration with Spring Boot
Cloud Spanner does not provide official support for Spring Boot out of the box. However, one can still integrate with Spring Boot using Spanner client libraries as provided by Google.
Steps to install and Set up Cloud Spanner
The first step is to register for Google Cloud Console using a Google account.
- Set Up Cloud Spanner
- Create a GCP project from the Google Console. I have a
- Enable the Google Cloud Spanner API
- Create a Spanner instance and Spanner database in that GCP project.
- Install Google Cloud SDK
- Download the Google Cloud SDK.
- Install the SDK following the instructions on that same page.
- After successful installation, it will run the gcloud init command
- It will authenticate with your Google account that you are using in the Google Cloud Platform
- Once authenticated, it will ask to select the Project, provide the no of the project
- Then it will ask for the Zone and Region. I have chosen the asia-south1-c region, which is available at option no 35. It can vary. But based on your location, you choose your region.
- Use the same Google Account to connect/configure to various GCP Services from local
- Open the Google Cloud SDK from your machine and run the below command
‘gcloud auth application-default login’ - The above command will store the credentials of your Google Account in your local machine. By default, the location is C:/Users/<UserName>/AppData/Roaming/gcloud/application_default_credentials.json.
- Open the Google Cloud SDK from your machine and run the below command
- Create a Spanner Instance in Google Console
- Provide the name of the instance. I have provided the name of the instance as springcavaj-user.
- Select the instance region. In my case, I have selected asia-south1-c region.
- Choose the no. of nodes.
- Optional Settings like – Backup, retention, etc.
- Click on the Create button to create the instance.
- Create the Spanner database
- Click on the Database tab of your Spanner instance.
- Click on Create Database
- Enter the name of the database. I have provided the name as springcavajspanner.
- Choose the schema of the database.
- Create tables, and indexes for the database. Even you can create it later.
- Click on the Create button to create the database.
Spring Boot Integration with Spanner
- To integrate with Spring Boot you need the Spanner client libraries or if you are using Maven then the dependencies.
- I am using Maven for this application, the main dependency used is google-cloud-spanner. For Gradle use the same dependency name.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
</dependency>
- You don’t need to specify the version as you will use Google Dependency Management from where you will get the associative dependencies versions.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-dependencies</artifactId>
<version>3.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
- After these, using the classes from the library (google-cloud-spanner) you can perform the CRUD operations in Cloud Spanner.
Discussion on spring-cloud-gcp-spanner-masterclass application
Spring Boot doesn’t provide direct support to implement Cloud Spanner. For that, you need a dependency named as google-cloud-spanner. By using this dependency you can use the inbuilt classes and can do CRUD operations in Cloud Spanner. I have created a Demo application named spring-cloud-gcp-spanner-masterclass and uploaded it to my personal GitHub account. One can clone the project and test it locally. All the steps that are required to clone the project from GitHub and run it locally by installing the software are mentioned in the README.md file of the application.
- Brief Description – In this application, I have used Java, Spring Boot, Maven, Spring-REST and Spring Google Cloud Spanner.
- Software Used –
- Spring Tool Suite-4.7.0-RELEASE – If the latest version is available then download that one
- Apache Maven 3.6.3 – If the latest version is available then download that one
- Git 2.27.0 – Latest version as available
- Google Cloud SDK – To configure the Google account as used in Google Cloud Console to configure the required services
- Postman v8.3.0 – To test the REST Service
- The list of the software used in this application and their required download links with the installation steps are briefly described in the README.md file of the spring-cloud-gcp-pub-sub-masterclass repository.
- Project Components – I have used Maven as the building tool and used the classes present in the dependency google-cloud-spanner. And in application.properties file we need to define certain properties.
spanner.projectId=spring-gcp-sql-storage
spanner.instanceId=springcavaj-user
spanner.databaseId=springcavajspanner
spring.cloud.gcp.credentials.location=file:<Folder location>/application_default_credentials.json
- spanner.projectId = spring-gcp-sql-stoarge ⇾ It ensures that your application connects to the appropriate Google Cloud Spanner resources within your specified Google Cloud Project.
- spanner.instanceId = springcavaj-user ⇾ It refers to the identifier or the name of the Spanner instance.
- spanner.databaseId = springcavajspanner ⇾ It refers to the name of the specific database present in the spanner instance.
- Structure of the project – If you are using Maven as the building tool, then the project structure is always the same. PFB the screenshot.
- Spanner Library Integration with Spring Boot – As, I have mentioned earlier Spring Boot doesn’t provide direct implementation with the Spanner Database so we need to use the Spanner library. But on using the library you can use the Spring data JPA with Hibernate concept. So, you can’t use the concept of Repository Interface which extends the required Repository based on the underlying DB whether it is SQL or NoSQL. In the case of Spanner, you have to specify one Config class.
A code snippet of the Config class named SpringGCPSpannerConfig.java
@Configuration
public class SpringGCPSpannerConfig {
@Value("${spanner.projectId}")
private String projectId;
@Value("${spanner.instanceId}")
private String instanceId;
@Value("${spanner.databaseId}")
private String databaseId;
@Bean
public Spanner spanner() {
SpannerOptions options = SpannerOptions.newBuilder().setProjectId(projectId).build();
return options.getService();
}
@Bean
public DatabaseClient databaseClient(Spanner spanner) {
return spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
}
}
This configuration class will connect to Google Cloud Spanner instance. It returns the DatabaseClient of com.google.cloud.spanner package. And it was defined as a spring bean. So you can autowire in your Service class and can perform the CRUD operations with Spanner GCP instance.
- Testing the application – There is no UI component, so I have used Postman to test the REST API Endpoints. But you can integrate the UI segment and can do proper POC on your own.
- You clone the application from GitHub and set up the application locally in any one of the IDEs like Spring Tool Suite (STS) or Eclipse.
- Right-click on the application
- Click the Option Run As
- Select the option Spring Boot App.
- It will start the application in port no 7122
GitHub Code Link
Download the Source Code from GitHub
Common Faced Problems
Spring Cloud using Google Cloud Spanner Common Problems
Interview FAQs
Spring Cloud using Google Cloud Spanner Interview FAQs
Other Useful Links of Spring Cloud using Google
Spring Cloud Google using Google Cloud SQL and Google Cloud Storage
Spring Cloud using Google Cloud Pub-Sub
Other Useful Links
Spring Data using RDBMS (MySQL DB) and Spring REST
Spring Data using NoSQL DB and Spring REST
Spring Data using Cypher Neo4j DB and Spring REST
Spring Data using Redis and Spring REST
Spring Apache Kafka – Producer & Consumer
Spring Kafka Confluent – Set Up
Pingback: Spring Cloud Using Google Cloud Pub-Sub - springcavaj
Pingback: Spring Cloud Using Google Cloud SQL and Google Cloud Storage - springcavaj
Pingback: Mongo Kafka Connector - springcavaj
Pingback: Spring Apache Kafka – Producer and Consumer - springcavaj
Pingback: Spring RabbitMQ - springcavaj
Pingback: Spring REST - springcavaj
Pingback: Spring Data – Neo4j - springcavaj
Pingback: Spring Data – JPA - springcavaj
Pingback: Spring Data – Mongo - springcavaj
google reviews social proof.