Sample application of Spring Data JDBC for ScalarDB with Microservice Transactions
This tutorial describes how to create a sample Spring Boot application for microservice transactions by using Spring Data JDBC for ScalarDB.
For details about these features, see Two-phase Commit Transactions and Guide of Spring Data JDBC for ScalarDB.
Prerequisites for this sample application
- OpenJDK LTS version (8, 11, 17, or 21) from Eclipse Temurin
- Docker 20.10 or later with Docker Compose V2 or later
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.
You need to have a license key (trial license or commercial license) to use ScalarDB Cluster. If you don't have a license key, please contact us.
Sample application
Overview
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 through transactions with a two-phase commit interface in ScalarDB.
There are two microservices called the Customer Service and the Order Service based on the Database-per-service pattern in this sample application.
The Customer Service manages customers' information including credit card information like a credit limit and a credit total. The Order Service is responsible for order operations like placing an order and getting order histories. Each service has gRPC endpoints. Clients call the endpoints, and the services call the endpoints each other as well. The Customer Service and the Order Service use MySQL and Cassandra through ScalarDB, respectively.
Each service accesses the databases through its own dedicated ScalarDB Cluster.
Both ScalarDB Clusters access a small coordinator database used for the Consensus Commit protocol. In this sample application, for ease of setup and explanation, the coordinator database is co-located in the same Cassandra instance of the Order Service, but of course, the coordinator database can be managed as a separate database.
Also, application-specific error handling, authentication processing, etc. are omitted in the sample application since it focuses on explaining how to use ScalarDB. For details about handling exceptions in ScalarDB, see How to handle exceptions.
Service endpoints
The endpoints defined in the services are as follows:
-
Customer Service
getCustomerInfo
payment
prepare
validate
commit
rollback
repayment
-
Order Service
placeOrder
getOrder
getOrders
What you can do in this sample application
The sample application supports the following types of transactions:
- Get customer information through the
getCustomerInfo
endpoint of the Customer Service. - Place an order by using a line of credit through the
placeOrder
endpoint of the Order Service and thepayment
,prepare
,validate
,commit
, androllback
endpoints of the Customer Service.- Checks if the cost of the order is below the customer's credit limit.
- If the check passes, records the order history and updates the amount the customer has spent.
- Get order information by order ID through the
getOrder
endpoint of the Order Service and thegetCustomerInfo
,prepare
,validate
,commit
, androllback
endpoints of the Customer Service. - Get order information by customer ID through the
getOrders
endpoint of the Order Service and thegetCustomerInfo
,prepare
,validate
,commit
, androllback
endpoints of the Customer Service. - Make a payment through the
repayment
endpoint of the Customer Service.- Reduces the amount the customer has spent.
The getCustomerInfo
endpoint works as a participant service endpoint when receiving a transaction ID from the coordinator.
Configuration for ScalarDB Cluster
The configuration for ScalarDB Cluster for the Customer Service is as follows:
scalar.db.storage=multi-storage
scalar.db.multi_storage.storages=cassandra,mysql
scalar.db.multi_storage.storages.cassandra.storage=cassandra
scalar.db.multi_storage.storages.cassandra.contact_points=cassandra-1
scalar.db.multi_storage.storages.cassandra.username=cassandra
scalar.db.multi_storage.storages.cassandra.password=cassandra
scalar.db.multi_storage.storages.mysql.storage=jdbc
scalar.db.multi_storage.storages.mysql.contact_points=jdbc:mysql://mysql-1:3306/
scalar.db.multi_storage.storages.mysql.username=root
scalar.db.multi_storage.storages.mysql.password=mysql
scalar.db.multi_storage.namespace_mapping=customer_service:mysql,coordinator:cassandra
scalar.db.multi_storage.default_storage=mysql
scalar.db.consensus_commit.isolation_level=SERIALIZABLE
scalar.db.cluster.node.standalone_mode.enabled=true
scalar.db.sql.enabled=true
# License key configurations
scalar.db.cluster.node.licensing.license_key=
scalar.db.cluster.node.licensing.license_check_cert_pem=
scalar.db.sql.connection_mode
: This configuration decides how to connect to ScalarDB.scalar.db.storage
: Specifyingmulti-storage
is necessary to use Multi-storage Transactions in ScalarDB.scalar.db.multi_storage.storages
: Your storage names must be defined here.scalar.db.multi_storage.storages.cassandra.*
: These configurations are for thecassandra
storage, which is one of the storage names defined inscalar.db.multi_storage.storages
. You can configure all thescalar.db.*
properties for thecassandra
storage here.scalar.db.multi_storage.storages.mysql.*
: These configurations are for themysql
storage, which is one of the storage names defined inscalar.db.multi_storage.storages
. You can configure all thescalar.db.*
properties for themysql
storage here.scalar.db.multi_storage.namespace_mapping
: This configuration maps the namespaces to the storage. In this sample application, operations forcustomer_service
namespace tables are mapped to themysql
storage and operations fororder_service
namespace tables are mapped to thecassandra
storage. You can also define which storage is mapped for thecoordinator
namespace that is used in Consensus Commit transactions.scalar.db.multi_storage.default_storage
: This configuration sets the default storage that is used for operations on unmapped namespace tables.scalar.db.sql.default_transaction_mode
: Specifyingtwo_phase_commit_transaction
is necessary to use Two-Phase Commit Transactions mode in ScalarDB.scalar.db.consensus_commit.isolation_level
: This configuration decides the isolation level used for ConsensusCommit.
For details, see Multi-Storage Transactions.
The configuration for ScalarDB Cluster for the Order Service is as follows:
scalar.db.storage=cassandra
scalar.db.contact_points=cassandra-1
scalar.db.username=cassandra
scalar.db.password=cassandra
scalar.db.consensus_commit.isolation_level=SERIALIZABLE
scalar.db.cluster.node.standalone_mode.enabled=true
scalar.db.sql.enabled=true
# License key configurations
scalar.db.cluster.node.licensing.license_key=
scalar.db.cluster.node.licensing.license_check_cert_pem=
scalar.db.storage
:cassandra
is specified since this servcise uses only Cassandra as an underlying database.scalar.db.contact_points
: This configuration specifies the contact points (e.g., host) for connecting to Cassandra.scalar.db.username
: This configuration specifies the username for connecting to Cassandra.scalar.db.password
: This configuration specifies the password for connecting to Cassandra.scalar.db.sql.default_namespace_name
: This configuration sets the default namespace toorder_service
, eliminating the need for the application to specify namespaces.scalar.db.sql.default_transaction_mode
: Specifyingtwo_phase_commit_transaction
is necessary to use Two-Phase Commit Transactions mode in ScalarDB.scalar.db.consensus_commit.isolation_level
: This configuration decides the isolation level used for ConsensusCommit.
In this sample application, the ScalarDB Clusters are running in standalone mode (scalar.db.cluster.node.standalone_mode.enabled=true
).
Also, you need to set the license key (trial license or commercial license) for the ScalarDB Clusters in the configuration file. For details, see How to Configure a Product License Key.
Setup
Clone the ScalarDB samples repository
Open Terminal, then clone the ScalarDB samples repository by running the following command:
git clone https://github.com/scalar-labs/scalardb-samples
Then, go to the directory with this sample by running the following command:
cd scalardb-samples/spring-data-microservice-transaction-sample