{{announcement.body}}
{{announcement.title}}

Developing Spring Boot Applications with In-Memory Database

DZone 's Guide to

Developing Spring Boot Applications with In-Memory Database

Learn how to use Spring Boot Ignite extensions for developing microservices with Apache Ignite.

· Microservices Zone ·
Free Resource

I am a big fan of in-memory computing and Apache Ignite is one my favorite platform for developing high-performance backend applications. Recently, the Apache Ignite team announced a new extension for developing a Spring Boot application with the Ignite database. The full documentation of the extension is available here

The main purpose of the extension is to integrate Ignite into Spring Boot application with minimal configuration. This autoconfigure module serves in two different flavors:

  1. ignite-spring-boot-autoconfigure-ext — Provides autoconfiguration capabilities for Ignite server and client nodes within Spring Boot application. Ignite node (server/client) will be running on the same JVM that uses the Spring boot application (see figure 1 below). Read more about the Ignite cluster topology in the sample chapter of the book.

  2. ignite-spring-boot-thin-client-autoconfigure-ext — Configure an Ignite thin client for the Spring Boot application to connect to the Ignite cluster. 

In this blog post, I am going to develop a simple Spring Boot application with the "ignite-spring-boot-autoconfigure-ext" extension to explore the autoconfiguration capabilities of the module. 

Spring Boot application

To effectively demonstrate the capabilities of "ignite-spring-boot-autoconfigure-ext," I am going to create a simple Spring Boot web application that will expose a REST API "/getQuote" and use the Ignite node as an in-memory database. The project we have you work within this post is fictitious and simplistic, it would be best for you to have the source code that accompanies the article. You can check out the source code from Github

Now that you have had a moment to peruse the code, let's develop the application from scratch. It will take not more than five minutes. 

Step 1. Create a Maven project. Use the following command to create a Maven project.

Shell
 




x


1
mvn archetype:generate -DartifactId=springboot-ext-for-ignite -DgroupId=com.blu.std -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


Step 2. Add the following Spring Boot dependencies into the project POM file.

XML
 




xxxxxxxxxx
1
19


1
<dependency>
2
    <groupId>org.springframework.boot</groupId>
3
    <artifactId>spring-boot-starter-web</artifactId>
4
</dependency>
5
<dependency>
6
    <groupId>org.springframework.boot</groupId>
7
    <artifactId>spring-boot-starter-test</artifactId>
8
    <scope>test</scope>
9
    <exclusions>
10
        <exclusion>
11
            <groupId>org.junit.vintage</groupId>
12
            <artifactId>junit-vintage-engine</artifactId>
13
        </exclusion>
14
    </exclusions>
15
</dependency>
16
<dependency>
17
    <groupId>org.springframework.boot</groupId>
18
    <artifactId>spring-boot-starter-actuator</artifactId>
19
</dependency>


Also, add the snippet below into the project pom.xml file.

XML
 




xxxxxxxxxx
1


 
1
<parent>
2
    <groupId>org.springframework.boot</groupId>
3
    <artifactId>spring-boot-starter-parent</artifactId>
4
    <version>2.2.2.RELEASE</version>
5
    <relativePath/>
6
</parent>


The spring-boot-starter-parent is a special Spring Boot starter project that provides default configurations for our project. 

Step 3. Now, add Ignite Maven artifacts as dependencies show below.

XML
 




xxxxxxxxxx
1
25


1
<dependency>
2
    <groupId>org.apache.ignite</groupId>
3
    <artifactId>ignite-core</artifactId>
4
    <version>${ignite.version}</version>
5
</dependency>
6
<dependency>
7
    <groupId>com.h2database</groupId>
8
    <artifactId>h2</artifactId>
9
    <version>${h2.version}</version>
10
</dependency>
11
<dependency>
12
    <groupId>org.apache.ignite</groupId>
13
    <artifactId>ignite-indexing</artifactId>
14
    <version>${ignite.version}</version>
15
</dependency>
16
<dependency>
17
    <groupId>org.apache.ignite</groupId>
18
    <artifactId>ignite-log4j</artifactId>
19
    <version>${ignite.version}</version>
20
</dependency>
21
<dependency>
22
    <groupId>org.apache.ignite</groupId>
23
    <artifactId>ignite-spring-boot-autoconfigure-ext</artifactId>
24
    <version>1.0.0</version>
25
</dependency>


Here, ignite-spring-boot-autoconfigure-ext is the key Maven dependency. Once added, the Spring context will create an Ignite instance on startup automatically. Don't forget to replace the ignite.version parameter with your version of Ignite. I am going to Ignite version 2.8.0 for this project. 

Step 4. Create a folder named resources under the /src/main directory and add the following Ignite node configuration into the application.yml file as shown below.

YAML
 




xxxxxxxxxx
1
20


 
1
ignite:
2
  igniteInstanceName: spring-boot-embeded-ignite-instance
3
  communicationSpi:
4
    localPort: 5555
5
  dataStorageConfiguration:
6
    defaultDataRegionConfiguration:
7
      initialSize: 10485760 #10MB
8
    dataRegionConfigurations:
9
      - name: dataregion
10
        initialSize: 104857600 #100MB
11
  cacheConfiguration:
12
    - name: quote
13
      queryEntities:
14
        - tableName: QUOTE
15
          keyFieldName: ID
16
          keyType: java.lang.Long
17
          valueType: java.lang.Object
18
          fields:
19
            ID: java.lang.Long
20
            VAL: java.lang.String


Most of the Ignite configurations are uses by default. However, we added a cache/table named QUOTE with two columns: ID, VAL. 

Step 5. Add the Spring  CommandLineRunner  interface into the Java class App. The  CommandLineRunner interface will run once after the application startup and fill the QUOTE table with a few test data.

Java
 




x


1
@Bean
2
public CommandLineRunner runner(){
3
    return new CommandLineRunner() {
4
        @Autowired
5
        private Ignite ignite;
6
        @Override
7
        public void run(String... args) throws Exception {
8
            LOGGER.info("Test Ignite node setup and run some queries!");
9
            LOGGER.info("IgniteInstanceName: " + ignite.configuration().getIgniteInstanceName());
10
 
          
11
            LOGGER.info("Init the table QUOTE!!");
12
            // get the cache "quote" from the application.yml
13
            IgniteCache<Long, String> cache_quote = ignite.cache("quote");
14
 
          
15
            String qry = "MERGE into QUOTE(ID, VAL) values (?, ?)";
16
            // insert a few rows
17
            cache_quote.query(new SqlFieldsQuery(qry).setArgs(1L,"Today you are you!")).getAll();
18
            cache_quote.query(new SqlFieldsQuery(qry).setArgs(2L,"Today was good.")).getAll();
19
            cache_quote.query(new SqlFieldsQuery(qry).setArgs(3L,"Today is the only day.")).getAll();
20
            cache_quote.query(new SqlFieldsQuery(qry).setArgs(4L,"What is not started today is never finished tomorrow.")).getAll();
21
        }
22
    };
23
}


Step 6. Add a new Java class with the name DemoController , which will be the REST controller for the web application.

Java
 




xxxxxxxxxx
1
30


 
1
@RestController
2
public class DemoController {
3
 
          
4
    @Autowired
5
    private Ignite ignite;
6
 
          
7
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);
8
    private static final String QUOTE_CACHE_NAME= "quote";
9
 
          
10
    @RequestMapping("/")
11
    public String index() {
12
        return "Spring Boot Hello-world application for demo!";
13
    }
14
 
          
15
    @RequestMapping("/getQuote")
16
    public String getQuote() {
17
        LOGGER.info("getQuote method executes");
18
 
          
19
        final int ID = new Random().nextInt(4)+1;
20
 
          
21
        IgniteCache<Long, String> cache_quote = ignite.cache(QUOTE_CACHE_NAME);
22
        // Query the table QUOTE
23
        String qry = "Select * from \"quote\".QUOTE q where q.ID=?";
24
 
          
25
        List<List<?>> rows = cache_quote.query(new SqlFieldsQuery(qry).setArgs(ID)).getAll();
26
 
          
27
        return rows.get(0).get(1).toString();
28
    }
29
 
          
30
}


The above REST controller contains two methods:  index(),  getQuote(). The  getQuote()  method is mapped to the URL /getQuote. Whenever you invoke the REST API, the method will query the table QUOTE and return you a quote randomly each time.

Step 7. The last step to build & run the project. Execute the following command in your favorite terminal.

Shell
 




xxxxxxxxxx
1


1
mvn clean install && java -jar ./target/springboot-ext-for-ignite-1.0-SNAPSHOT.jar


The above command should build the project and deploy (execute) a standalone Spring Boot application. You can see the results of the command in the console. 

Now, if you invoke the  /getQuote REST API through any command-line tool like curl or browser, you should have a similar output as shown below.

Invoke /getquote

At these moments, you can also use the Ignite SQLLINE tool to connect to the Ignite node and run some queries. See the screenshot below.

Ignite SQLLINE

Finally, create a few more instances of the same application. Just like you did before, execute the following command with a different port as follows.

Shell
 




xxxxxxxxxx
1


1
java -jar -Dserver.port=8888 ./target/springboot-ext-for-ignite-1.0-SNAPSHOT.jar


Now, you have two standalone applications that form an Ignite cluster with two nodes. Use the Ignite visor command-line tool to see a summary of the cluster.

Summary

In this short blog post, we have learned how to use an ignite-spring-boot-autoconfigure-ext extension to develop a full flagged Spring boot application with an in-memory database like Ignite.

Topics:
apache ignite, extensions, in-memory data grid, microservice, spring boot

Published at DZone with permission of Shamim Bhuiyan . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}