apache ignite系列(二):配置


ignite有兩種配置方式,一種是基於XML文件的配置,一種是基於JAVA代碼的配置:

這里將ignite常用的配置集中羅列出來了,一般建議使用xml配置。

1,基於XML的配置


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

	<bean id="igniteCfg" class="org.apache.ignite.configuration.IgniteConfiguration">
         
        <!-- Consistent globally unique node identifier which survives node restarts. -->
        <!-- 全局一致性id -->
        <!--<property name="consistentId" value="cord"/>-->
	
	    <!--Set to true to enable distributed class loading for examples, default is false. -->
        <!-- 分布式計算class傳播 -->
        <property name="peerClassLoadingEnabled" value="true"/>
        
        <!-- Set deployment mode. -->
        <!-- 部署模式,控制類加載. -->
        <property name="deploymentMode" value="CONTINUOUS"/>

        <!-- Disable missed resources caching. -->
        <!-- 禁用丟失資源緩存 -->
        <property name="peerClassLoadingMissedResourcesCacheSize" value="0"/>
		
        <!-- 設為false表示服務端模式 -->
        <property name="clientMode" value="false"/>

        <!-- Network timeout -->
        <!-- 連接超時時間 -->
        <property name="networkTimeout" value="10000" />

		<!--Exclude force peer class loading of a class, even if exists locally.-->
        <!-- 配置需要傳播的class的路徑 -->
        <property name="peerClassLoadingLocalClassPathExclude">
            <list>
        <value>org.cord.*</value>
    </list>
    </property>
        
        <!-- 配置需要監聽的事件類型-->
        <property name="includeEventTypes">
            <list>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_PUT"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_REMOVED"/>
            </list>
        </property>
	
        <!-- Configure internal thread pool. -->
        <!-- 公共線程池大小-->
        <property name="publicThreadPoolSize" value="64"/>
        
        <!-- Configure system thread pool. -->
        <!-- 系統線程池大小-->
        <property name="systemThreadPoolSize" value="32"/>

        <property name="cacheConfiguration">
            <list>
                <bean class="org.apache.ignite.configuration.CacheConfiguration">
                    <!-- 緩存名-->
                    <property name="name" value="IGNITE_CACHE_KEY_PCOMM_RATE"/>
                    <!-- 原子模式類型,ATOMIC:原子型,保證性能; TRANSACTIONAL:事務型,分布式鎖-->
                    <property name="atomicityMode" value="ATOMIC"/>
                    <!--PARTITIONED:分區; REPLICATED:復制;LOCAL:本地 -->
                    <property name="cacheMode" value="REPLICATED"/>
                    <!-- 備份數量-->
                    <property name="backups" value="1"/>
                    <!-- 禁用jcache標准中緩存讀取獲取的是副本的機制 -->
                    <property name="copyOnRead" value="false"/>
                    <!-- 內存區名-->
                    <property name="dataRegionName" value="Default_Region"/>
                    <!-- 是否以二進制形式存儲-->
                    <!--<property name="storeKeepBinary" value="true"/>-->
                    <!-- 索引類型-->
                    <property name="indexedTypes">
                        <list>
                            <value>java.lang.Long</value>
                            <value>com.palic.demo.data.domain.CommRate</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>

        <!-- Redefining maximum memory size for the cluster node usage. -->
        <property name="dataStorageConfiguration">
            <bean class="org.apache.ignite.configuration.DataStorageConfiguration">
                <!-- Redefining the default region's settings -->
                <!-- 默認存儲區間配置-->
                <property name="defaultDataRegionConfiguration">
                    <bean class="org.apache.ignite.configuration.DataRegionConfiguration">
                        <!-- 存儲區名-->
                        <property name="name" value="Default_Region"/>
                        <!-- 存儲區大小-->
                        <!-- Setting the size of the default region to 1GB. -->
                        <!--<property name="maxSize" value="#{1L * 1024 * 1024 * 1024}"/>-->
                        <property name="maxSize" value="#{512 * 1024 * 1024}"/>
                        <!-- 是否開啟持久化-->
                        <!-- Enabling Apache Ignite Persistent Store. -->
                        <!--<property name="persistenceEnabled" value="true"/>-->
                    </bean>
                </property>
                <property name="dataRegionConfigurations">
                    <list>
                        <!-- 自定義內存區-->
                        <!--
                            Defining a data region that will consume up to 500 MB of RAM and
                            will have eviction and persistence enabled.
                        -->
                        <bean class="org.apache.ignite.configuration.DataRegionConfiguration">
                            <!-- Custom region name. -->
                            <property name="name" value="500MB_Region"/>
                            <!-- 100 MB initial size. -->
                            <property name="initialSize" value="#{100L * 1024 * 1024}"/>
                            <!-- 500 MB maximum size. -->
                            <property name="maxSize" value="#{500L * 1024 * 1024}"/>
                            <!-- Enabling persistence for the region. -->
                            <!--<property name="persistenceEnabled" value="true"/>-->
                        </bean>
                    </list>
                </property>
			   
                <!-- 預寫日志模式-->
                <!-- Sets property that defines behavior of wal fsync. -->
                <!--<property name="walMode">-->
                    <!--<util:constant static-field="org.apache.ignite.configuration.WALMode.DEFAULT"/>-->
                <!--</property>-->
                <property name="walMode" value="DEFAULT"/>
				
                <!-- 檢查點頻率-->
                <!--Checkpointing frequency which is a minimal interval when the dirty pages will be written to the Persistent Store.-->
                <property name="checkpointFrequency" value="180000"/>
                <!--<property name="checkpointFrequency" value="10000"/>-->
				
                <!-- 檢查點線程數-->
                <!-- Number of threads for checkpointing.-->
                <property name="checkpointThreads" value="4"/>

                <!-- 在檢查點同步完成后預寫日志歷史保留數量 -->
                <!-- Number of checkpoints to be kept in WAL after checkpoint is finished.-->
                <property name="walHistorySize" value="20"/>
                <!--<property name="walHistorySize" value="2"/>-->
				
                <!-- 持久化文件路徑 -->
                <!--&lt;!&ndash; Path where data and indexes will be persisted. &ndash;&gt;-->
                <!--<property name="storagePath" value="D:\\Test\\db" />-->

                <!--&lt;!&ndash; Path to the directory where WAL (Write Ahead Log) is stored. &ndash;&gt;-->
                <!--<property name="walPath" value="D:\\Test\\db\\wal" />-->

                <!--&lt;!&ndash; Path to the directory where WAL (Write Ahead Log) archive is stored. &ndash;&gt;-->
                <!--<property name="walArchivePath" value="D:\\Test\\db\\wal\\archive" />-->

            </bean>
        </property>
		
	    <property name="discoverySpi">
		    <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
			    <property name="localPort" value="48500"/>
				<property name="localPortRange" value="20"/>
                <property name="joinTimeout" value="0"/>
                <property name="networkTimeout" value="5000" />
				<property name="ipFinder">
				    <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
					    <property name="addresses">
                              <!-- 集群ip列表-->
						    <list>
							    <value>127.0.0.1:48500..48520</value>
							</list>
						</property>
					</bean>
				</property>
			</bean>
		</property>
		<property name="communicationSpi">
		    <bean class="org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi">
			    <property name="localPort" value="48100"/>
			</bean>
		</property>
	</bean>
</beans>

基於此XML配置啟動ignite節點的方式如下:

@Configuration
public class IgniteConfig {
    @Autowired
    private IgniteConfiguration igniteCfg;
  
    @Bean
    @ConditionalOnMissingBean
    public Ignite initIgnite() {
      //推薦借助spring bean的方式注入ignite配置信息,只需要將配置xml文件import即可
      //啟動類加上注解@ImportResource(locations={"classpath:default-config.xml"})
      Ignite ignite = Ignition.start(igniteCfg);
      //Ignite ignite = Ignition.start(classpath:default-config.xml)
    }
}

2,基於JAVA代碼的配置

......
		IgniteConfiguration cfg = new IgniteConfiguration();
		cfg.setClientMode(false);
        //配置集群發現
		cfg.setDiscoverySpi(new TcpDiscoverySpi().setLocalPort(48500).setLocalPortRange(20)
												 .setIpFinder(new TcpDiscoveryVmIpFinder().setAddresses(Arrays.asList("127.0.0.1:48500..48520"))));
        //基本配置
		cfg.setCommunicationSpi(new TcpCommunicationSpi().setLocalPort(48100));
		cfg.setDeploymentMode(CONTINUOUS);
		cfg.setPeerClassLoadingEnabled(true);
		cfg.setPeerClassLoadingLocalClassPathExclude("com.org.ignite.*");
		cfg.setIncludeEventTypes(EventType.EVT_TASK_STARTED, EventType.EVT_TASK_FINISHED, EventType.EVT_TASK_FAILED);
		cfg.setPublicThreadPoolSize(64);
		cfg.setSystemThreadPoolSize(32);
		//添加cache配置
		List<CacheConfiguration> cacheConf = new ArrayList<>();
		CacheConfiguration<String, Integer> conf = new CacheConfiguration<String, Integer>("test")
				.setCacheMode(CacheMode.REPLICATED)
				.setIndexedTypes(String.class, Integer.class)
				.setAtomicityMode(CacheAtomicityMode.ATOMIC)
				.setCopyOnRead(false)
				.setBackups(1);
		cacheConf.add(conf);
		cfg.setCacheConfiguration(cacheConf.toArray(new CacheConfiguration[]{}));

		//基於java代碼配置啟動
		Ignition.start(cfg);
......

一般建議基於XML配置,spring bean注入,如果確實需要JAVA配置,可以結合XML配置靈活處理。

​ 在ignite集群中,配置信息是可以動態傳播的,而如果是修改配置文件,則需要重啟節點才可生效,並且如果有些關鍵配置不一致,也會導致啟動節點報錯,無法加入集群。所以最好的做法是,在普通節點中只配置節點相關配置,以及集群發現配置,至於變動性最大的緩存cache配置,可以由應用節點配置,這樣便於的集中管控緩存配置。除此之外,使用xml配置,可以保證普通節點與應用節點的配置的一致性,而不需要單獨再維護一套代碼配置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM