Apache Ignite With Spring Boot | Ignite Documentation

Ignite Summit 2024 — Call For Speakers Now Open — Learn more

Edit

Apache Ignite With Spring Boot

Overview

Spring Boot is a widely used Java framework that makes it easy to create stand-alone Spring-based applications.

Apache Ignite provides two extensions that automate Ignite configuration withing the Spring Boot environment:

  • ignite-spring-boot-autoconfigure-ext - autoconfigures ignite server and client nodes within Spring Boot.

  • ignite-spring-boot-thin-client-autoconfigure-ext - autoconfigures Ignite Thin Client with Spring Boot.

Autoconfiguration of Apache Ignite Servers and Clients

You need to use ignite-spring-boot-autoconfigure-ext extension to autoconfigure Ignite servers or clients (aka. thick clients) with Spring Boot.

The extension can be added with Maven as follows:

<dependency>
  <groupId>org.apache.ignite</groupId>
  <artifactId>ignite-spring-boot-autoconfigure-ext</artifactId>
   <version>1.0.0</version>
</dependency>

Once added, Spring will create an Ignite instance on start-up automatically.

Set Ignite Up Via Spring Boot Configuration

You can use a regular Spring Boot configuration to set Ignite-specific settings. Use ignite as a prefix:

ignite:
  igniteInstanceName: properties-instance-name
  communicationSpi:
    localPort: 5555
  dataStorageConfiguration:
    defaultDataRegionConfiguration:
      initialSize: 10485760 #10MB
    dataRegionConfigurations:
      - name: my-dataregion
        initialSize: 104857600 #100MB
  cacheConfiguration:
    - name: accounts
      queryEntities:
      - tableName: ACCOUNTS
        keyFieldName: ID
        keyType: java.lang.Long
        valueType: java.lang.Object
        fields:
          ID: java.lang.Long
          amount: java.lang.Double
          updateDate: java.util.Date
    - name: my-cache2

Set Ignite Up Programmatically

There are two ways to configure Ignite programmatically.

1. Create IgniteConfiguration Bean

Just create a method that returns IgniteConfiguration bean that will be used to initialize an Ignite node with the settings you set:

@Bean
  public IgniteConfiguration igniteConfiguration() {
     // If you provide a whole ClientConfiguration bean then configuration properties will not be used.
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setIgniteInstanceName("my-ignite");
    return cfg;
  }

2. Customize IgniteConfiguration Created With Spring Boot Configuration

If you want to customize IgniteConfiguration that was initially created with Spring Boot configuration file, then provide an implementation of IgniteConfigurer interface for your application context.

First, IgniteConfiguration will be loaded from the Spring Boot configuration and then that instance will be passed to the configurer for extra settings.

@Bean
  public IgniteConfigurer nodeConfigurer() {
    return cfg -> {
      //Setting some property.
      //Other will come from `application.yml`
      cfg.setIgniteInstanceName("my-ignite");
    };
  }

Autoconfiguration of Apache Ignite Thin Client

You need to use ignite-spring-boot-thin-client-autoconfigure-ext extension to autoconfigure Ignite Thin Client with Spring Boot.

<dependency>
    <groupId>org.apache.ignite</groupId>
  <artifactId>ignite-spring-boot-thin-client-autoconfigure-ext</artifactId>
  <version>1.0.0</version>
</dependency>

Once added, Spring will create an instance of Ignite Thin client connection on start-up automatically.

Set Thin Client Up Via Spring Boot Configuration

You can use a regular Spring Boot configuration to configure IgniteClient object. Use ignite-client for Ignite-specific settings:

ignite-client:
  addresses: 127.0.0.1:10800 # this is mandatory property!
  timeout: 10000
  tcpNoDelay: false

Set Thin Client Up Programmatically

You can use two ways to configure IgniteClient object programmatically.

1. Create ClientConfiguration bean

Just create a method that returns ClientConfiguration bean. IgniteClient object will use that bean upon startup:

@Bean
  public ClientConfiguration clientConfiguration() {
    // If you provide a whole ClientConfiguration bean then configuration properties will not be used.
    ClientConfiguration cfg = new ClientConfiguration();
    cfg.setAddresses("127.0.0.1:10800");
    return cfg;
  }

2. Customize ClientConfiguration Created With Spring Boot Configuration

If you want to customize ClientConfiguration bean created from the Spring Boot configuration file, then provide an implementation of IgniteClientConfigurer interface in your application context.

First, ClientConfiguration will be loaded from the Spring Boot configuration and then an instance will be passed to the configurer.

@Bean
    IgniteClientConfigurer configurer() {
        //Setting some property.
        //Other will come from `application.yml`
        return cfg -> cfg.setSendBufferSize(64*1024);
    }

Examples

Refer to several available examples for more details.