Skip to content
Permalink
Browse files
IGNITE-12525: spring-boot autoconfigure modules added (#6)
  • Loading branch information
nizhikov committed Jan 28, 2020
2 parents f322752 + e5fd215 commit 575ec510e4d48b1e0c2ec7e4cf8d596e7de82f70
Show file tree
Hide file tree
Showing 23 changed files with 1,698 additions and 0 deletions.
@@ -14,6 +14,7 @@ pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
pom-installed.xml
release.properties
dependency-reduced-pom.xml
buildNumber.properties
@@ -0,0 +1,45 @@
Apache Ignite Spring Boot Autoconfigure Module
----------------------------------------

Apache Ignite Spring Boot Autoconfigure module provides autoconfiguration capabilities for Spring-boot based applications.

Features
---------------------------
This module provides the ability to integrate `Ignite` into you spring-boot application with zero(or minimal) configuration.

After you add this module as a dependency to your spring-boot application
`Ignite` node will be configured and injected into `BeanFactory`.

Algorithm to configure `Ignite` is the following:
1. If `IgniteConfiguration` bean exists in the `BeanFactory` it will be used.
2. If `IgniteConfiguration` bean doesn't exist following rules are applied:
2.1. Default `Ignite` configuration created.
2.2. If `IgniteConfigurer` bean exists in `BeanFactory` it will be used to customize `IgniteConfiguration`.
If a user wants to set custom SPI instances or similar hardcoded values
one should do it with `IgniteConfigurer` implementation.
2.3 Application properties applied to `IgniteConfiguration`. Prefix for the properties is `ignite`.

Importing Spring Boot Autoconfigure Module In Maven Project
----------------------------------------

If you are using Maven to manage dependencies of your project, you can add Spring module
dependency like this (replace '${ignite.version}' with actual Ignite version you are
interested in):

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<dependencies>
...
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>spring-boot-ignite-autoconfigure</artifactId>
<version>${ignite.version}</version>
</dependency>
...
</dependencies>
...
</project>

@@ -0,0 +1,122 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.springframework.boot.autoconfigure;

import java.util.Date;
import java.util.List;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.DataRegionConfiguration;
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;

/** Example of Ignite auto configurer. */
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class AutoConfigureExample {
/** Main method of the application. */
public static void main(String[] args) {
SpringApplication.run(AutoConfigureExample.class, args);
}

/** Providing configurer for the Ignite. */
@Bean
public IgniteConfigurer configurer() {
return cfg -> {
//Setting consistent id.
//See `application.yml` for the additional properties.
cfg.setConsistentId("consistent-id");
cfg.setCommunicationSpi(new TcpCommunicationSpi());
};
}

/** Service using autoconfigured Ignite. */
@Bean
public CommandLineRunner runner() {
return new CommandLineRunner() {
/** Ignite instance. */
@Autowired
private Ignite ignite;

/** Method will be executed on application startup. */
@Override public void run(String... args) throws Exception {
System.out.println("ServiceWithIgnite.run:");
//This property comes from configurer. See AutoConfigureExample.
System.out.println(" IgniteConsistentId: " + ignite.configuration().getConsistentId());

//Other properties are set via application-node.yml.
System.out.println(" IgniteInstanceName: " + ignite.configuration().getIgniteInstanceName());
System.out.println(" CommunicationSpi.localPort: " +
((TcpCommunicationSpi)ignite.configuration().getCommunicationSpi()).getLocalPort());
System.out.println(" DefaultDataRegion initial size: " +
ignite.configuration().getDataStorageConfiguration().getDefaultDataRegionConfiguration().getInitialSize());

DataRegionConfiguration drc =
ignite.configuration().getDataStorageConfiguration().getDataRegionConfigurations()[0];

System.out.println(" " + drc.getName() + " initial size: " + drc.getInitialSize());
System.out.println(" Cache in cluster:");

for (String cacheName : ignite.cacheNames())
System.out.println(" " + cacheName);

cacheAPI();
sqlAPI();
}

/** Example of the SQL API usage. */
private void sqlAPI() {
//This cache configured in `application.yml`.
IgniteCache<Long, Object> accounts = ignite.cache("accounts");

//SQL table configured via QueryEntity in `application.yml`
String qry = "INSERT INTO ACCOUNTS(ID, AMOUNT, UPDATEDATE) VALUES(?, ?, ?)";

accounts.query(new SqlFieldsQuery(qry).setArgs(1, 250.05, new Date())).getAll();
accounts.query(new SqlFieldsQuery(qry).setArgs(2, 255.05, new Date())).getAll();
accounts.query(new SqlFieldsQuery(qry).setArgs(3, .05, new Date())).getAll();

qry = "SELECT * FROM ACCOUNTS";

List<List<?>> res = accounts.query(new SqlFieldsQuery(qry)).getAll();

for (List<?> row : res)
System.out.println("(" + row.get(0) + ", " + row.get(1) + ", " + row.get(2) + ")");
}

/** Example of the Cache API usage. */
private void cacheAPI() {
//This cache configured in `application.yml`.
IgniteCache<Integer, Integer> cache = ignite.cache("my-cache2");

System.out.println("Putting data to the my-cache1...");

cache.put(1, 1);
cache.put(2, 2);
cache.put(3, 3);

System.out.println("Done putting data to the my-cache1...");
}
};
}
}
@@ -0,0 +1,23 @@
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

0 comments on commit 575ec51

Please sign in to comment.