Building Microservices with Spring Data, Cassandra, and Stargate.io

By Cédrick Lunven

DataStax
Building Real-World, Real-Time AI

--

This post introduces developers to the world’s most popular Java framework, Spring, along with Apache Cassandra®, DataStax Astra, and Stargate.io. To learn how to build microservices with Spring Data, Cassandra, and Stargate, sign up for our hands-on workshop at SpringOne on Sept. 2, 2021.

Starting a new project typically involves wrangling different databases and APIs that change completely depending on what you’re working on. Not to mention lengthy manuals, tutorials, and hours scrolling through forums.

This is where frameworks swoop in. With pre-written code and extensible features, frameworks give developers a flexible template that they can build on and reuse for almost any application. That means you don’t need to code from scratch every time you start a new project, and you can launch your apps with much less time and effort.

One of the most popular open-source frameworks, for Java applications, in particular, is Spring. Spring gives developers a comprehensive set of extensions and third-party libraries that make it easier to build cloud-ready apps in Java. With its flexible, modular architecture that connects to databases and Java-based web services, you can create high-performing, enterprise-level applications that are easy to test and reuse.

With the renowned SpringOne conference coming up, this post gives you a head start on what Spring can do and how you can connect it to other world-class technologies (like Apache Cassandra and Stargate.io) to streamline your development ecosystem.

Spring

Let’s start with a deeper dive into the Spring Framework. This widely popular framework boasts a modular architecture that lets you selectively use the parts your application actually needs, allowing you to easily build anything from reactive, cloud-based microservices for the web to streaming data flows for the enterprise.

At the heart of the Spring Framework are two fundamental features: inversion of control (IoC) and dependency injection (DI).

  • Inversion of control essentially transfers the control of objects to the Spring Framework. Unlike traditional programming, where custom code makes calls to a library, IoC allows the framework to control the flow of a program. This keeps Java classes independent of each other for increased modularity and extensibility.
  • Dependency injection is a pattern in Spring that’s used to implement IoC, where the Spring container is in charge of “injecting” objects into the right dependencies. This allows for the loose coupling of components and shifts the responsibility of managing components onto the container.

To make developing apps with this framework even easier, there are various projects under the Spring umbrella that you can pick and choose depending on what you’re working on. For the purpose of the upcoming workshop at SpringOne, we’ll introduce you to the two main projects you’ll be using: Spring Boot and Spring Data.

Spring Boot

Spring Boot helps you easily build and run standalone, production-ready Spring applications with minimal configuration. Since it’s based on the Spring Framework, it comes with a collection of dependencies that you can simply plug into any Spring application to get up and running in a fraction of the time.

The following figure shows an example of how Spring Boot stitches together the different Spring modules you’d need to run an app.

Figure 1: Spring Boot architecture.

Spring Boot also comes with additional benefits like remote admin access, embedded HTTP servers to test web apps (e.g. Tomcat or Jetty), and production-ready features including metrics, health checks, and externalized configuration. Think of Spring Boot as the glue that holds different frameworks and your code together to keep your app running smoothly.

Spring Data

Spring Data provides a consistent interface to make it easier to work with different databases. To achieve this, Spring Data abstracts away the details of specific database drivers and instead provides you with Data Access Objects (DAOs), which map application calls to the persistence layer to simplify data operations.

Take a look at the figure below for a better idea of how Spring Data ties in with the other Spring components we’ve covered so far.

Figure 2: Spring components on top of a Cassandra database.

In this scenario, the app is backed by Cassandra, which as you may already know is the “world’s most scalable NoSQL database”. But as powerful as Cassandra is for its high performance, availability, and resilience, it’s also infamous for its complexity — slowing down app development and making it difficult for new developers to get their projects off the ground.

Streamline Spring app development with Stargate

Simply put: if you want to interact with a database, you need to use the right language for it. For example, you’d use Cassandra Query Language (CQL) to create, read, update, delete the data in a Cassandra database. This inevitably means juggling different languages for different databases, or limiting your options so you can stick with what you already know.

To bring order to the chaos, DataStax created Stargate — a single open-source data gateway that bridges your app with your databases so you can interact with any form of data in a familiar way.

Stargate provides a collection of APIs that you likely already recognize, such as:

  • REST to work with your data in a flexible, RESTful format
  • Document API for schemaless data management using JSON
  • GraphQL to easily query your data from any Cassandra database

Whichever API you choose, Stargate allows you to interact with your data more efficiently and abstracts away the complexities of Cassandra, so you can focus on building features rather than battling with infrastructure.

To bring the power of Cassandra to as many developers as possible, we made it available by default in DataStax Astra DB, which comes with its own bag of tricks to speed up Spring-based application development.

Simplify Spring applications with Astra DB

For the unfamiliar, Astra DB is our open-source, serverless database-as-a-service that simplifies building cloud-ready applications backed by Cassandra. With a free tier or pay-as-you-go pricing, and the freedom to choose any of the major cloud providers, Astra DB radically reduces deployment time and simplifies database and streaming operations.

With Astra DB you not only get access to Stargate but also admin APIs to provision your instances and create users and roles. A client wrapping every API interaction is provided in the form of an SDK (Software Development Kit). On top of this client is a Spring Boot starter that initializes Spring beans and context for easy integration with any Spring application.

Figure 3: Architecture integrating Astra SDK in a Spring application.

To get started with Astra and dig into the benefits of Stargate for Spring apps, simply sign up for a free Astra DB account.

Join our hands-on workshop at SpringOne

Now that you’re familiar with the concepts, you’re ready to roll up your sleeves and put them into practice at SpringOne. SpringOne is known as the “premier conference for developers, DevOps pros, and app leaders,” and it’s the place to be if you’re passionate about development.

This year you have the unique opportunity to join the DataStax Developers for a 90-minute workshop as we show you how to build cloud-native microservices using Spring and Cassandra. You’ll also learn:

  • How to connect, deploy and use Stargate.io
  • How to build a Spring Boot app from scratch
  • How to connect the app to Stargate and Astra DB using Spring Data and Astra SDK
  • How to create a web page and list values from your database

We’ll provide you with everything you need in a GitHub repository, so all you have to do to participate is turn on your laptop.

Interested? Sign up here to join our workshop on “Building Microservices with Spring Data, Cassandra, and Stargate.io” this September 2nd at 3:00 PM BST / 7 AM PT / 4 PM CEST. See you there!

Make sure to subscribe to our event alert to get notified about our latest developer workshops. For exclusive posts on all things data: Cassandra, streaming, Kubernetes, and more; follow DataStax on Medium.

Resources

  1. Building a Spring Microservice with a NoSQL DB!
  2. DataStax GitHub: Building Spring Boot/data Application leveraging Cassandra and Stargate
  3. Stargate APIs | GraphQL, REST, Document
  4. Using Cassandra with the Java Spring Framework
  5. Intro to Inversion of Control and Dependency Injection with Spring
  6. DataStax Academy
  7. DataStax Certifications
  8. DataStax Workshops

--

--

DataStax
Building Real-World, Real-Time AI

DataStax provides the real-time vector data tools that generative AI apps need, with seamless integration with developers' stacks of choice.