All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.iot.common.config.IgniteAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 2.3.3.RELEASE
Show newest version
package com.feingto.iot.common.config;

import com.feingto.iot.common.config.annotation.EnableIgniteAutoConfiguration;
import com.feingto.iot.common.config.properties.IgniteProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteMessaging;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.DataRegionConfiguration;
import org.apache.ignite.configuration.DataStorageConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.logger.slf4j.Slf4jLogger;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * Ignite 自动注入配置
 *
 * @author longfei
 */
@Slf4j
@Configuration
@ConditionalOnBean(annotation = EnableIgniteAutoConfiguration.class)
public class IgniteAutoConfiguration {
    private static final String CACHE_REGION = "iot-cache-region";
    private static final String DATA_REGION = "iot-data-region";
    private static final String CACHE_SUBSCRIBE = "iot-cache-subscribe";
    private static final String CACHE_RETAINED = "iot-cache-retained";
    private static final String CACHE_MESSAGE = "iot-cache-message";

    @Bean("igniteProperties")
    public IgniteProperties config() {
        return new IgniteProperties();
    }

    @Bean("igniteCache")
    public Ignite ignite() {
        IgniteProperties.Broker brokerConfig = config().getBroker();
        IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
        igniteConfiguration.setIgniteInstanceName(brokerConfig.getName());
        igniteConfiguration.setGridLogger(new Slf4jLogger(LoggerFactory.getLogger("org.apache.ignite")));
        igniteConfiguration.setSystemThreadPoolSize(config().getThreadPoolSize());
        // Non-persistent data regions
        DataRegionConfiguration notPersistenceRegion = new DataRegionConfiguration()
                .setPersistenceEnabled(false)
                .setName(CACHE_REGION);
        if (config().getNotPersistenceInitialSize() > 0) {
            notPersistenceRegion.setInitialSize(config().getNotPersistenceInitialSize() * 1024 * 1024);
        }
        if (config().getNotPersistenceMaxSize() > 0) {
            notPersistenceRegion.setMaxSize(config().getNotPersistenceMaxSize() * 1024 * 1024);
        }
        // Persistent data regions
        DataRegionConfiguration persistenceRegion = new DataRegionConfiguration()
                .setPersistenceEnabled(true)
                .setName(DATA_REGION);
        if (config().getPersistenceInitialSize() > 0) {
            persistenceRegion.setInitialSize(config().getPersistenceInitialSize() * 1024 * 1024);
        }
        if (config().getPersistenceMaxSize() > 0) {
            persistenceRegion.setMaxSize(config().getPersistenceMaxSize() * 1024 * 1024);
        }
        // Persistent data disk storage path
        igniteConfiguration.setDataStorageConfiguration(new DataStorageConfiguration()
                .setDefaultDataRegionConfiguration(notPersistenceRegion)
                .setDataRegionConfigurations(persistenceRegion)
                .setWalPath(config().getWalPath())
                .setWalArchivePath(config().getWalArchivePath())
                .setStoragePath(config().getPersistenceStorePath()));
        TcpDiscoverySpi tcpDiscoverySpi = new TcpDiscoverySpi();
        if (brokerConfig.isEnableMulticastGroup()) {
            // Multicast-based IP finder
            tcpDiscoverySpi.setIpFinder(new TcpDiscoveryMulticastIpFinder()
                    .setMulticastGroup(brokerConfig.getMulticastGroup()));
        } else {
            // IP Finder which works only with pre-configured list of IP addresses specified
            tcpDiscoverySpi.setIpFinder(new TcpDiscoveryVmIpFinder()
                    .setAddresses(Arrays.asList(brokerConfig.getStaticIpAddresses())));
        }
        igniteConfiguration.setDiscoverySpi(tcpDiscoverySpi);
        Ignite ignite = Ignition.start(igniteConfiguration);
        ignite.cluster().active(true);
        return ignite;
    }

    @Bean("igniteMessaging")
    public IgniteMessaging igniteMessaging() {
        return ignite().message(ignite().cluster().forRemotes());
    }

    @Bean("igniteSubscribe")
    public IgniteCache igniteSubscribe() {
        return ignite().getOrCreateCache(new CacheConfiguration()
                .setDataRegionName(DATA_REGION)
                .setCacheMode(CacheMode.PARTITIONED)
                .setName(CACHE_SUBSCRIBE));
    }

    @Bean("igniteRetained")
    public IgniteCache igniteRetained() {
        return ignite().getOrCreateCache(new CacheConfiguration()
                .setDataRegionName(DATA_REGION)
                .setCacheMode(CacheMode.PARTITIONED)
                .setName(CACHE_RETAINED));
    }

    @Bean("igniteMessage")
    public IgniteCache igniteMessage() {
        return ignite().getOrCreateCache(new CacheConfiguration()
                .setDataRegionName(DATA_REGION)
                .setCacheMode(CacheMode.PARTITIONED)
                .setName(CACHE_MESSAGE));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy