Getting Started with ScalarDB Cluster SQL via Spring Data JDBC for ScalarDB
This tutorial describes how to create a sample application by using ScalarDB Cluster SQL via Spring Data JDBC for ScalarDB.
Prerequisites for this sample application
- OpenJDK LTS version (8, 11, 17, or 21) from Eclipse Temurin
- ScalarDB Cluster running on a Kubernetes cluster
- We assume that you have a ScalarDB Cluster running on a Kubernetes cluster that you deployed by following the instructions in Set Up ScalarDB Cluster on Kubernetes by Using a Helm Chart.
This sample application has been tested with OpenJDK from Eclipse Temurin. ScalarDB itself, however, has been tested with JDK distributions from various vendors. For details about the requirements for ScalarDB, including compatible JDK distributions, please see Requirements.
Sample application
This tutorial illustrates the process of creating a sample e-commerce application, where items can be ordered and paid for with a line of credit by using Spring Data JDBC for ScalarDB.
The following diagram shows the system architecture of the sample application:
+------------------------------------------------------------------------------------------------------------------------------+
| [Kubernetes Cluster] |
| |
| [Pod] [Pod] [Pod] |
+------------------------+ | |
| SQL CLI | | +-------+ +-----------------------+ |
| (indirect client mode) | --+ | +---> | Envoy | ---+ +---> | ScalarDB Cluster Node | ---+ |
+------------------------+ | | | +-------+ | | +-----------------------+ | |
| | | | | | |
| | +---------+ | +-------+ | +--------------------+ | +-----------------------+ | +------------+ |
+--+-> | Service | ---+---> | Envoy | ---+---> | Service | ---+---> | ScalarDB Cluster Node | ---+---> | PostgreSQL | |
+------------------------+ | | | (Envoy) | | +-------+ | | (ScalarDB Cluster) | | +-----------------------+ | +------------+ |
| Sample application | | | +---------+ | | +--------------------+ | | |
| with Spring Data JDBC | | | | +-------+ | | +-----------------------+ | |
| for ScalarDB | --+ | +---> | Envoy | ---+ +---> | ScalarDB Cluster Node | ---+ |
| (indirect client mode) | | +-------+ +-----------------------+ |
+------------------------+ | |
+------------------------------------------------------------------------------------------------------------------------------+
Step 1. Clone the ScalarDB Samples repository
git clone https://github.com/scalar-labs/scalardb-samples.git
cd scalardb-samples/spring-data-sample
Step 2. Modify scalardb-sql.properties
You need to modify scalardb-sql.properties
to connect to ScalarDB Cluster as well.
But before doing so, you need to get the EXTERNAL-IP
address of the service resource of Envoy (scalardb-cluster-envoy
) as follows:
kubectl get svc scalardb-cluster-envoy
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
scalardb-cluster-envoy LoadBalancer 10.105.121.51 localhost 60053:30641/TCP 16h
In this case, the EXTERNAL-IP
address is localhost
.
Next, open scalardb-sql.properties
:
vim scalardb-sql.properties
Then, modify scalardb-sql.properties
as follows:
scalar.db.sql.connection_mode=cluster
scalar.db.sql.cluster_mode.contact_points=indirect:localhost
To connect to ScalarDB Cluster, you need to specify cluster
for the scalar.db.sql.connection_mode
property.
In addition, you will use the indirect
client mode and connect to the service resource of Envoy in this tutorial.
For details about the client modes, see Developer Guide for ScalarDB Cluster with the Java API.
Step 3. Load a schema
To load a schema, you need to use the SQL CLI. You can download the SQL CLI from ScalarDB Releases. After downloading the JAR file, you can use SQL CLI for Cluster by running the following command:
java -jar scalardb-cluster-sql-cli-3.12.4-all.jar --config scalardb-sql.properties --file schema.sql
Step 4. Modify application.properties
Then, you need to modify application.properties
to connect to ScalarDB Cluster as well:
vim src/main/resources/application.properties
Similar to scalardb-sql.properties
, you need to specify cluster
for the scalar.db.sql.connection_mode
property and use the indirect
client mode.
To do so, modify application.properties
as follows:
spring.datasource.driver-class-name=com.scalar.db.sql.jdbc.SqlJdbcDriver
spring.datasource.url=jdbc:scalardb:\
?scalar.db.sql.connection_mode=cluster\
&scalar.db.sql.cluster_mode.contact_points=indirect:localhost\
&scalar.db.consensus_commit.isolation_level=SERIALIZABLE\
&scalar.db.sql.default_namespace_name=sample