Access Aerospike from Spring Boot Based gRPC App Built with Gradle

Bootstrap The Project

Bootstrap The Project

  • Project — Gradle Project
  • Language — Java
  • Spring Boot — 2.33
  • Packaging — Jar
  • Java — 11
  • Dependencies
    Lombok
    Spring Security
    Spring Boot Actuator (Optional)
    Spring REST Docs (Optional)
    Spring Web

Gradle Configuration

gRPC Support

  • Add following in the plugins
id 'com.google.protobuf' version '0.8.12'
  • Add a sourceSets section to define where to find auto-generated classes
sourceSets { main{  java {   srcDir 'src/main/protoGen'  } }}
  • Add a protobuf section to for protobuf compiler
  • Add following code in gradle dependencies
implementation 'io.github.lognet:grpc-spring-boot-starter:3.5.6'

Aerospike Support

implementation 'org.springframework.data:spring-data-commons' implementation 'com.aerospike:spring-data-aerospike:2.4.0.RELEASE'

Project Structure

main/Java

  • com.nur.spring.aerospike.configuration
    configuration packages, as the name implies, contains various configuration classes. AerospikeRepositoryConfiguration is used to configure target aerospike cluster, user credentials and namespace. SecurityConfiguration controls url permission.
  • com.nur.spring.aerospike.domain
    domain classes are populated by the repositories with data fetched.
  • com.nur.spring.aerospike.repository
    contains repository interface definition. It is worth noting that there is no need to provide implementation classes of these interfaces. Proxy implementations are generated during compilation.
  • com.nur.spring.aerospike.service
    Service classes implement business logic. There are two types of service classes.
    1. GrpcService - GrpcService classes are annotated with @GrpcService. They intercept incoming requests.
    2. Service — Service classes are annotated with @Service. They implement business logic.

main/proto

main/protoGen

Execution Flow

Access Aerospike

  • The library provides AerospikeRepository<T, ID> where
  • The interface can be extended to create a new interface to manage specific domain types.
  • Domain type T is used as the aerospike set name.
  • ID is the key to query
  • Each field in the domain type represents a bin in aerospike.
public interface StorefrontProductRepository extends AerospikeRepository<StorefrontProductDocument, String> {}

Defining document entity

@Value // (1)
@Document(collection = “my_aerospike_set”) // (2)
@AllArgsConstructor
public class GreeterDocument {
@Field(“my_aerospike_greeting_bin”) // (3)
String GreetingText;
}

Explanation

  1. @Value — makes the class immutable, all fields are made private and final, toString(), equals(), hashCode(), field getters and all args constructor are generated.
  2. @Document(collection = “my_aerospike_set”) — @Document marks a class as an entity to be persisted in aerospike, By default, class name is used as the aerospike set name but this can be overridden by collection element as is done in the example above.
  3. @Field(“my_aerospike_greeting_bin”) — configures the name of a field to be used when persisting the document. This value will be used as a bin name instead of the original field name.

Source code

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store