Apache Ignite 集群安装

Apache Ignite具有非常先进的集群能力,包括逻辑集群组和自动发现。Apache Ignite节点之间会自动发现对方,这有助于必要时扩展集群,而不需要重启整个集群。开发者可以利用Ignite的混合云支持,允许公有云(比如AWS)和私有云之间建立连接,向他们提供两者的好处。

在启动时,节点被分配以下两种角色之一:服务器节点客户端节点。服务器节点是集群的主力;它们缓存数据、执行计算任务等。客户端节点作为常规节点加入拓扑,但它们不存储数据。客户端节点用于将数据流式传输到集群中并执行用户查询。

要形成集群,每个节点必须能够连接到所有其他节点。为确保这一点,必须配置适当的发现机制。

发现机制

节点可以自动发现彼此并形成集群。这允许您在需要时进行横向扩展,而无需重新启动整个集群。开发人员还可以利用 Ignite 的混合云支持,允许在私有云和公共云(如 Amazon Web Services)之间建立连接,为他们提供两全其美的体验。

Ignite 提供了两种用于不同使用场景的发现机制实现:

集群配置

本文使用版本为 ignite v2.13.1

默认安装 jdk 1.8 版本即可,解压安装包,在集群每台主机上的 $IGNITE_HOME/config 目录下增加 default.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 id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">

    <!-- 对等类加载是否启用,默认为true不开启很容易报错 -->
    <property name="peerClassLoadingEnabled" value="true"/>

    <!-- 系统线程池大小 (max(8, total number of cores)) -->
    <property name="systemThreadPoolSize" value="24"/>

    <!-- 公共线程池大小 (max(8, total number of cores)) -->
    <property name="publicThreadPoolSize" value="8"/>

    <!-- 查询线程池大小 (max(8, total number of cores)) -->
    <property name="queryThreadPoolSize" value="8"/>

    <!-- 服务线程池大小 (max(8, total number of cores)) -->
    <property name="serviceThreadPoolSize" value="8"/>

    <!-- 源线程池大小 (max(8, total number of cores)) -->
    <property name="stripedPoolSize" value="8"/>

    <!-- 数据流线程池大小(max(8, total number of cores) -->
    <property name="dataStreamerThreadPoolSize" value="8"/>

    <!-- 平衡线程池大小-->
    <property name="rebalanceThreadPoolSize" value="8"/>

    <!-- 用户验证是否开启 默认为false 开启后默认用户名密码都是ignite -->
<!--   
       <property name="authenticationEnabled" value="true"/>
-->

    <!-- 对象序列化过程 -->
    <property name="marshaller">
        <bean class="org.apache.ignite.internal.binary.BinaryMarshaller" />
    </property>

    <!-- 数据存储配置 -->
    <property name="dataStorageConfiguration">
        <bean class="org.apache.ignite.configuration.DataStorageConfiguration">

            <!--并发性水平   可由自己实际情况而定 -->
            <property name="concurrencyLevel" value="200"/>

            <!-- 设置内存页大小 (getconf PAGESIZE) -->
            <property name="pageSize" value="#{4 * 1024}"/>

            <!-- Size of the WAL (Write Ahead Log) segment -->
            <property name="walSegmentSize" value="#{1024 * 1024 * 1024}"/>

            <!--In our experience LOG_ONLY is a good compromise between durability and performance.-->
            <property name="walMode" value="LOG_ONLY"/>
            <!-- Enable write throttling. -->
            <property name="writeThrottlingEnabled" value="true"/>

            <!-- 检查点频率-->
            <!--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="defaultDataRegionConfiguration">

                <bean class="org.apache.ignite.configuration.DataRegionConfiguration">
                    <!--是否持久化到磁盘 true为持久化 -->
                    <property name="persistenceEnabled" value="true"/>

                    <property name="name" value="vehicle_Region"/>

                    <!-- 2G initial size. 初始化内存-->
                    <property name="initialSize" value="#{2L * 1024 * 1024 * 1024}" />

                    <!-- 4G maximum size. 最大内存大小-->
                    <property name="maxSize" value="#{4L * 1024 * 1024 * 1024}" />

                    <!-- 4G 内存页缓存大小-->
                    <property name="checkpointPageBufferSize" value="#{4L *1024* 1024 * 1024L}" />
                </bean>

            </property>

            <!-- Defining several data regions for different memory regions 持久化数据存储目录 -->
            <property name="storagePath" value="/home/ignite/storage" />
            <property name="walArchivePath" value="/home/ignite/walArchive" />
            <property name="walPath" value="/home/ignite/wal" />

        </bean>
    </property>

    <property name="metricsLogFrequency" value="0"/>
    <!--失败检测 超时时长-->
    <property name="failureDetectionTimeout" value="#{60 * 60 * 1000}"/>
    <!-- 服务worker 之间交互 timeout 时间,默认 10s -->
    <property name="systemWorkerBlockedTimeout" value="#{60 * 60 * 1000}"/>
    <!-- 服务出现故障自动重启 -->
    <property name="failureHandler">
        <bean class="org.apache.ignite.failure.RestartProcessFailureHandler"/>
    </property>

    <property name="cacheConfiguration">
        <bean class="org.apache.ignite.configuration.CacheConfiguration">
            <!-- Set a cache name. -->
            <property name="name" value="memdb2"/>

            <!-- Set asynchronous rebalancing. -->
            <property name="rebalanceMode" value="ASYNC"/>

            <!-- Set cache mode. 分区模式,副本为 2 -->
            <property name="cacheMode" value="PARTITIONED"/>
            <property name="backups" value="2"/>
            <!-- 副本同步模式: -->
            <!--  PRIMARY_SYNC (默认模式,primary 写成功即可算成功,从backup节点读数据,有可能读到的任然是旧数据)  -->
            <!--  FULL_SYNC  (写cache的操作在primary节点和backup节点都成功写入后返回, 保证了写入成功后节点之间的数据都一样)  -->
            <!--  FULL_ASYNC (写cache的操作不用等primary节点和backup节点成功写入即可返回, 读primary节点的数据都有可能读到旧数据)  -->
            <property name="writeSynchronizationMode" value="PRIMARY_SYNC"/>

            <!-- 分区丢失处理: -->
            <!--  IGNORE (默认模式,即使出现了partition loss的情况,Ignite会自动忽略并且会清空和partion loss相关的状态不会触发EVT_CACHE_REBALANCE_PART_DATA_LOST 事件)  -->
            <!--  READ_WRITE_ALL  (Ignite允许所有的读写操作,就好像partition loss没发生过)  -->
            <!--  READ_WRITE_SAFE (允许对没有丢失的partition的读写操作,但是对已经丢失的partition的读写操作会失败并抛异常)  -->
            <!--  READ_ONLY_ALL (允许对丢失的和正常的partition的读操作,但是写操作会失败并抛异常)  -->
            <!--  READ_ONLY_SAFE (所有的写操作和对丢失partition的读操作都会失败并抛异常。允许对正常的partition的读操作)  -->
            <property name="partitionLossPolicy" value="READ_WRITE_ALL"/>

        <!-- enable disk page compression for this cache -->
<!--
            <property name="diskPageCompression" value="SNAPPY"/>
-->
            <!-- optionally set the compression level -->
            <property name="diskPageCompressionLevel" value="10"/>
        </bean>
    </property>

    <!-- Set batch size. -->
    <property name="rebalanceBatchSize" value="#{1 * 1024 * 1024 * 1024}"/>

    <!-- Set throttle interval. -->
    <property name="rebalanceThrottle" value="100"/>

    <!--
               Explicitly configure TCP discovery SPI to provide list of initial nodes.
      Ignite自己本身有发现机制,只需要配置静态IP即可相互发现;单机只需要配置自己即可
    -->
    <property name="discoverySpi">
        <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
            <property name="ipFinder">
                <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
                    <property name="addresses">
                    <!--此处放置全部节点IP 如下方-->
                        <list>
                            <value>192.168.165.3:47500..47509</value>
                            <value>192.168.165.4:47500..47509</value>
                            <value>192.168.165.5:47500..47509</value>
                        </list>
                    </property>
                </bean>
            </property>
        </bean>
    </property>

</bean>
</beans>
                                                                                                                                                                                   193,1        底端

通过bin/ignite.sh   config/default.xml启动服务,一般Ignite有服务发现机制,无论有多少个节点,都可以只配置如上面配置的集群中的3个IP就可以,这个三个节点需要提前启动,其他新加入的节点,会通过这三个节点,路由到集群中的其他机器。

客户端访问

通过集群API查看集群的状态。

public class ClusterDemo {
    public static void main(String[] args) {
        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setPeerClassLoadingEnabled(true);
        cfg.setClientMode(true);
        // Setting up an IP Finder to ensure the client can locate the servers.
        TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
        ipFinder.setAddresses(Collections.singletonList("192.168.165.3:47500..47509"));
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));


        // Starting the node
        Ignite ignite = Ignition.start(cfg);

        IgniteCluster cluster = ignite.cluster();
        Collection<BaselineNode> baselineNodes = cluster.currentBaselineTopology();
        System.out.println(baselineNodes);

        ClusterGroup remoteGroup = cluster.forRemotes();
        System.out.println(remoteGroup.hostNames());

        //  所有的缓存
        System.out.println(ignite.cacheNames());
        ignite.close();
    }
}

缓存读写

通过客户端API创建缓存,读写数据

public class HelloWorld {
    public static void main(String[] args) throws IgniteException {
        // Preparing IgniteConfiguration using Java APIs
        IgniteConfiguration cfg = new IgniteConfiguration();

        // The node will be started as a client node.
        cfg.setClientMode(true);

        // Classes of custom Java logic will be transferred over the wire from this app.
        cfg.setPeerClassLoadingEnabled(true);

        // Setting up an IP Finder to ensure the client can locate the servers.
        TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
        ipFinder.setAddresses(Collections.singletonList("192.168.165.2:47500..47509"));
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
        // Starting the node
        Ignite ignite = Ignition.start(cfg);

        // Create an IgniteCache and put some values in it.
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
        cache.put(1, "Hello");
        cache.put(2, "World!");

        System.out.println(">> Created the cache and add the values.");

        // Executing custom Java compute task on server nodes.
        ignite.compute(ignite.cluster().forServers()).broadcast(new RemoteTask());

        System.out.println(">> Compute task is executed, check for output on the server nodes.");

        // Disconnect from the cluster.
        ignite.close();
    }

基线拓扑

基线拓扑是一组用于保存数据的节点。引入基线拓扑的概念是为了让您能够控制何时要 重新平衡集群中的数据。例如,如果您有一个由 3 个节点组成的集群,其中数据在节点之间分布,并且您再添加 2 个节点,则重新平衡过程会在所有 5 个节点之间重新分布数据。当基线拓扑发生变化时,就会发生重新平衡过程,这可以自动发生,也可以手动触发。

基线拓扑仅包括服务器节点;客户端节点从不包括在内,因为它们不存储数据。

基线拓扑的目的是:

  • 当服务器节点在短时间内离开集群时,避免不必要的数据传输,例如,由于偶尔的网络故障或计划的服务器维护。

  • 让您能够控制何时要重新平衡数据。

启用基线拓扑自动调整后,基线拓扑会自动更改。这是纯内存集群的默认行为。对于持久集群,必须手动启用基线拓扑自动调整功能。默认情况下,它被禁用,您必须手动更改基线拓扑。您可以使用控制脚本更改基线拓扑。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值