apache ignite系列(一): 简介

apache-ignite简介(一)

1,简介

​ ignite是分布式内存网格的一种实现,其基于java平台,具有可持久化,分布式事务,分布式计算等特点,此外还支持丰富的键值存储以及SQL语法(基于h2引擎),可以看成是一个分布式内存数据库。

与ignite类似的产品有gemfire(12306目前正在使用),其开源版为geode。与gemfire相比,ignite对sql的支持比较完善,提供了数据并置来提升性能,还有对分布式事物的支持以及对spring的集成都比较友好,很方便进行嵌入式集成进应用服务。

2,基本使用

​ ignite有两种使用方式: 一种是从官网下载release版本程序,解压运行部署,另外一种是通过嵌入式集成进现有应用程序。

2.1,官网二进制release版本的使用

下载地址:Downloads - Apache Ignite

下载后得到apache-ignite-fabric-2.3.0-bin.zip压缩包,解压后进入bin路径:

主要用到两个脚本: ignite.bat 启动脚本, ignitevisorcmd.bat监控脚本

执行ignite.bat脚本即可启动一个ignite服务

执行ignitevisorcmd.bat可以进入监控命令界面:

输入open命令选择配置文件,这里选择默认的0 | config\default-config.xml输入数字0即可

常用命令如下:

命令功能
top查看集群网络拓扑图
cache查看整体缓存情况
config查看节点配置
open打开一个配置文件连接集群
close关闭该连接

更多详细命令可以通过输入help命令查看命令帮助(输入help回车)。

2.2,java服务使用ignite客户端访问ignite集群

​ 通过JAVA服务使用已启动的ignite集群,JAVA服务可以使用客户端模式(Client),应用端不存储数据,或者使用服务端模式(Server)变成一个节点加入现有ignite集群,则应用端会缓存部分数据。如果是使用服务端模式的话,整个集群其实都可以使用应用节点组成集群,也就是上面所说的嵌入式集成。这样可以对节点进行定制化处理,更为灵活。

这里使用Client模式演示一下简单使用:

1) 添加相关依赖

        <dependency>
            <groupId>org.apache.ignite</groupId>
            <artifactId>ignite-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ignite</groupId>
            <artifactId>ignite-spring</artifactId>
            <version>2.3.0</version>
        </dependency>

2) 定义配置文件

default-config.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">
        <property name="clientMode" value="true"/>
	    <property name="discoverySpi">
		    <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
			    <property name="localPort" value="48500"/>
				<property name="localPortRange" value="20"/>
				<property name="ipFinder">
				    <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
					    <property name="addresses">
						    <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>

3) 启动ignite客户端并实现简单数据存取

ClientStartApplication.java

@SpringBootApplication
@ImportResource(locations={"classpath:default-config.xml"}) //ignite配置文件路径
public class ClientStartApplication implements CommandLineRunner {
    @Autowired
    private IgniteConfiguration igniteCfg;

    public static void main(String[] args) {
        SpringApplication.run(ClientStartApplication.class,args);
    }
  
    /**启动完成之后执行初始化*/
    @Override
    public void run(String... strings) {
        //启动ignite服务
        Ignite ignite = Ignition.start(igniteCfg);
        //创建cache
        IgniteCache<String, String> cache =  ignite.getOrCreateCache("test");
        //存入数据
        cache.put("cord",  "hello");
        //查询数据
        System.out.format("key[%s]->value[%s]\n", "cord", cache.get("cord"));
    }
}

执行结果如下:

[15:46:44] Ignite node started OK (id=48cfd9ce)
[15:46:44] Topology snapshot [ver=30, servers=1, clients=1, CPUs=4, heap=2.7GB]
key[cord]->value[hello]

通过ignitevisorcmd.bat查看当前集群状态与缓存情况:

visor> cache
(wrn) <visor>: No caches found.
(wrn) <visor>: Type 'help cache' to see how to use this command.

结果发现没有数据,这是因为默认的config\default-config.xml其实配置是空的,执行ignite.bat启动服务虽然也是用这个文件,但是因为有默认值,所以不影响,但是监控程序ignitevisorcmd.bat必须要根据配置文件才能连接访问集群信息,因此按如下所示修改config\default-config.xml
(其实就是在上面的default-config.xml中去掉了<property name="clientMode" value="true"/>这一项)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
       
	<bean id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
	    <property name="discoverySpi">
		    <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
			    <property name="localPort" value="48500"/>
				<property name="localPortRange" value="20"/>
				<property name="ipFinder">
				    <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
					    <property name="addresses">
						    <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>

再重新启动ignitevisorcmd.batopen修改后的config\default-config.xml:

执行top命令,可以看到两个节点的类型是不同的:

visor> top
Hosts: 1
+=================================================
|  Int./Ext. IPs  |   Node ID8(@)    | Node Type |
+=================================================
| 0:0:0:0:0:0:0:1 | 1: 875F3FCF(@n0) | Server    |
| 10.118.144.74   | 2: 48CFD9CE(@n1) | Client    |
| 127.0.0.1       |                  |           |
+-------------------------------------------------

执行cache命令,可以看到刚代码中创建的名为test的cache的信息:

visor> cache
Time of the snapshot: 08/03/18, 16:20:35
+==============================================================
|  Name(@)  |    Mode     | Nodes | Entries (Heap / Off-heap) |
+==============================================================
| test(@c0) | PARTITIONED | 2     | min: 0 (0 / 0)            |
|           |             |       | avg: 0.50 (0.00 / 0.50)   |
|           |             |       | max: 1 (0 / 1)            |
+--------------------------------------------------------------

2.3, java服务集成ignite作为服务节点

只需将java项目中的配置文件default-config.xml中的<property name="clientMode" value="true"/>改为

<property name="clientMode" value="false"/>即变为服务节点模式,这样该节点也可以存储数据。

启动之后java服务输出如下:

[00:08:45] Topology snapshot [ver=7, servers=2, clients=0, CPUs=4, heap=2.8GB]

可见servers数量有增加,说明服务节点启动成功,至此ignite简介结束。

完整的示例代码请参考:

GitHub - cording/ignite-example

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
My first acquaintance with High load systems was at the beginning of 2007, and I started working on a real-world project since 2009. From that moment, I spent most of my office time with Cassandra, Hadoop, and numerous CEP tools. Our first Hadoop project (the year 2011-2012) with a cluster of 54 nodes often disappointed me with its long startup time. I have never been satisfied with the performance of our applications and was always looking for something new to boost the performance of our information systems. During this time, I have tried HazelCast, Ehcache, Oracle Coherence as in-memory caches to gain the performance of the applications. I was usually disappointed from the complexity of using these libraries or from their functional limitations. When I first encountered Apache Ignite, I was amazed! It was the platform that I’d been waiting on for a long time: a simple spring based framework with a lot of awesome features such as DataBase caching, Big data acceleration, Streaming and compute/service grids. In 2015, I had participated in Russian HighLoad++ conference1 with my presentation and started blogging in Dzone/JavaCodeGeeks and in my personal blog2 about developing High-load systems. They became popular shortly, and I received a lot of feedback from the readers. Through them, I clarified the idea behind the book. The goal of the book was to provide a guide for those who really need to implement an in-memory platform in their projects. At the same time, the idea behind the book is not writing a manual. Although the Apache Ignite platform is very big and growing day by day, we concentrate only on the features of the platform (from our point of view) that can really help to improve the performance of the applications. We hope that High-performance in-memory computing with Apache Ignite will be the go-to guide for architects and developers: both new and at an intermediate level, to get up and to develop with as little friction as possible.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值