如何在apache中给多个静态ip点火缓存配置xml文件

时间:2018-05-08 12:48:54

标签: spring apache spring-boot ignite

我使用与apache集成的Spring Boot微服务点燃以在缓存中存储密钥和一些值。

在点火缓存配置文件中,我试图在4个服务器上使用相同的端口共享我的缓存,即,我应该能够获取但不能共享4个缓存但是它只选择2个服务器对于群集,在其他服务器中,它会点燃缓存作为单独的缓存启动,而不是群集。请帮忙。

我是否需要对群集4服务器进行任何其他配置更改。

Cacheconfig.xml

<property name="discoverySpi">
  <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
    <property name="ipFinder">
      <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
        <property name="addresses">
          <list>
            <!-- In distributed environment, replace with actual host IP address. -->
            <!-- <value>127.0.0.1:47500..47509</value> -->
            <value>158.xxx.xx.xxx</value><!--server1 ip-->
            <value>158.xxx.xx.xxx</value><!--server2 ip-->
            <value>158.xxx.xx.xxx</value><!--server3 ip-->
            <value>4444</value><!--my port-->
          </list>
        </property>
      </bean>
    </property>
  </bean>
</property>

这里我已将默认IP地址替换为所需的实际服务器IP地址。

但我可以看到,当我按照这种方式<value>158.xxx.xx.xxx:4444..158.xxx.xx.xxx:4444</value>时,它可以用于在两台服务器之间共享。

如果要将超过2个服务器IP地址添加到列表中,则无效。

请帮助共享3台以上服务器的缓存。

以下是用于启动点火并激活群集的代码

import java.util.Enumeration;
import java.util.Properties;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteException;
import org.apache.ignite.Ignition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class IgniteCacheManager {
    private static final Logger LOGGER = LoggerFactory.getLogger(IgniteCacheManager.class); 
    private  Ignite ignite;
    public Ignite getIgnite() {
        return ignite;
    }
    @Autowired
    private IgniteCacheManager(AppSpecificIgniteProperties igniteProperties) {
        Properties p = System.getProperties();
        Enumeration<Object> keys = p.keys();
        LOGGER.debug("-----------------------------SYSTEM PROPERTIES Start--------------------------------------");
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            String value = (String) p.get(key);
            LOGGER.info(key);
            LOGGER.info(value);
        }
        LOGGER.debug("-----------------------------SYSTEM PROPERTIES End--------------------------------------");
        try {

            // Ignite cache will start
            ignite=Ignition.start(igniteProperties.getConfigFile());            
            //Cluster Activation 
            ignite.cluster().active(true);
            LOGGER.info("IGNITE CACHE STARTED");
        } catch (IgniteException e) {
            LOGGER.error(e.getMessage(), e);
            throw e;
        }

    }
    public IgniteCache<String, Integer> getOrCreateCache(String name){

        return  ignite.getOrCreateCache(name);

    }
}

此处igniteProperties.getproperties是DiscoverySpi和缓存到期配置的cacheconfig.xml文件。 要将服务器添加到群集是否必须进行任何其他更改。 请帮忙。

以下是启动点火时使用的缓存配置

<?xml version="1.0" encoding="UTF-8"?>
<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 abstract="true" id="ignite.cfg"
        class="org.apache.ignite.configuration.IgniteConfiguration">

        <!-- Set to true to enable distributed class loading for examples, default 
            is false. -->
        <!-- <property name="clientMode" value="true"/> -->
        <property name="peerClassLoadingEnabled" value="true" />
        <property name="cacheConfiguration">
            <list>
                <bean class="org.apache.ignite.configuration.CacheConfiguration">
                    <property name="name" value="hcache" />
                    <property name="expiryPolicyFactory">
                        <!-- <bean id="expiryPolicy" class="javax.cache.expiry.CreatedExpiryPolicy" 
                            factory-method="factoryOf"> -->
                        <bean id="expiryPolicy" class="javax.cache.expiry.CreatedExpiryPolicy"
                            factory-method="factoryOf">
                            <!-- CreatedExpiryPolicy is used to inform the cache provider to remove 
                                the entry after a specified time since the entry’s addition to the cache. -->
                            <constructor-arg>
                                <bean class="javax.cache.expiry.Duration">
                                    <constructor-arg value="HOURS" />
                                    <constructor-arg value="1" />

                                </bean>
                            </constructor-arg>
                        </bean>
                    </property>
                </bean>
                <bean class="org.apache.ignite.configuration.CacheConfiguration">
                    <property name="name" value="dcache" />
                    <property name="expiryPolicyFactory">
                        <bean id="expiryPolicy" class="javax.cache.expiry.CreatedExpiryPolicy"
                            factory-method="factoryOf">
                            <constructor-arg>
                                <bean class="javax.cache.expiry.Duration">
                                    <constructor-arg value="HOURS" />
                                    <constructor-arg value="24" />
                                </bean>
                            </constructor-arg>
                        </bean>
                    </property>
                </bean>
                <bean class="org.apache.ignite.configuration.CacheConfiguration">
                    <property name="name" value="wcache" />
                    <property name="expiryPolicyFactory">
                        <bean id="expiryPolicy" class="javax.cache.expiry.CreatedExpiryPolicy"
                            factory-method="factoryOf">
                            <constructor-arg>
                                <bean class="javax.cache.expiry.Duration">
                                    <constructor-arg value="DAYS" />
                                    <constructor-arg value="7" />
                                </bean>
                            </constructor-arg>
                        </bean>
                    </property>
                </bean>
            </list>
        </property>

        <!-- Enable task execution events for examples. -->
        <property name="includeEventTypes">
            <list>
                <!--Task execution events -->
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_STARTED" />
                <util:constant
                    static-field="org.apache.ignite.events.EventType.EVT_TASK_FINISHED" />
                <!-- This event is triggered every time a task finished with an exception -->
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FAILED" />

                <util:constant
                    static-field="org.apache.ignite.events.EventType.EVT_TASK_TIMEDOUT" />
                <util:constant
                    static-field="org.apache.ignite.events.EventType.EVT_TASK_SESSION_ATTR_SET" />
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_REDUCED" />

                <!--Cache events -->
                <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>
        <!-- <property name="eagerTtl" value="true" /> -->
        <property name="dataStorageConfiguration">
            <bean class="org.apache.ignite.configuration.DataStorageConfiguration">
                <property name="defaultDataRegionConfiguration">
                    <bean class="org.apache.ignite.configuration.DataRegionConfiguration">
                        <property name="persistenceEnabled" value="true" />
                    </bean>
                </property>
            </bean>
        </property>

        <!-- Explicitly configure TCP discovery SPI to provide list of initial 
            nodes. -->
            <property name="discoverySpi">
            <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
                <property name="ipFinder">
                    <!-- Ignite provides several options for automatic discovery that can 
                        be used instead os static IP based discovery. For information on all options 
                        refer to our documentation: http://apacheignite.readme.io/docs/cluster-config -->
                    <!-- Uncomment static IP finder to enable static-based discovery of 
                        initial nodes. -->
                    <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder"> -->
                    <bean
                        class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
                        <property name="addresses">
                            <list>
                                <!-- In distributed environment, replace with actual host IP address. -->
                                <!-- <value>127.0.0.1:47500..47509</value> -->
                                <value>server1</value>
                                <value>server2</value>
                                <value>server3</value>
                                <value>server4</value>
                            </list>
                        </property>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>

2 个答案:

答案 0 :(得分:1)

如果您不想维护很长的地址列表,可以使用其他discovery mechanisms

您也不必列出本节中的每个IP。一旦节点从列表中找到一个服务器,它就会自动了解集群中的所有其他节点。

答案 1 :(得分:0)

在您提供的示例中,就我的理解而言,存在几个问题:

<value>158.xxx.xx.xxx</value> - 此行应具有端口或端口范围。 <value>4444</value> - 您无法在其他线路上提供端口,您应该将其列为地址。 <value>158.xxx.xx.xxx:4444..158.xxx.xx.xxx:4444</value> - 您可以拥有端口范围,但不能以这种方式拥有IP地址范围。

   <property name="addresses">
      <list>
        <!-- In distributed environment, replace with actual host IP address. -->
        <value>158.xxx.xx.xxx:47500..47509</value><!--server1 ip-->
        <value>158.xxx.xx.xxx:47500..47509</value><!--server2 ip-->
        <value>158.xxx.xx.xxx:47500..47509</value><!--server3 ip-->
      </list>
    </property>

如果您是新手,我建议您不要更改默认端口,因为它需要更改其他设置,您的目标应该是让基本群集正常工作。

此配置应适用于任意数量节点的群集,前提是服务器1,2或3至少有一个节点正在运行。其余节点将通过发现找到。