Ignite 快速开始

Ignite 快速开始

一、创建IgniteServer

  • 创建 Maven工程,在pom.xm中添加
<dependency> 
<groupId>org.apache.ignite</groupId> 
<artifactId>ignite-core</artifactId> 
<version>${ignite.version}</version> 
</dependency> 

<dependency> 
<groupId>org.apache.ignite</groupId> 
<artifactId>ignite-spring</artifactId> 
<version>${ignite.version}</version> 
</dependency> 

<dependency> 
<groupId>org.apache.ignite</groupId> 
<artifactId>ignite-indexing</artifactId> 
<version>${ignite.version}</version> 
</dependency> 
  • 在IgniteServerApplication类中添加如下代码
public static void main(String[] args) { 
Ignite ignite = Ignition.start("config/server-ignite.xml");
} 
  • 添加配置文件 server-ignite.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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<!-- Imports default Ignite configuration --> 
<import resource="example-default.xml"/> 

<bean parent="ignite.cfg"/> 
</beans> 
  • 添加 example-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 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="peerClassLoadingEnabled" value="true"/> 

<!-- 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"/>
<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> 

<!-- 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"> 
<!--<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> 
</list> 
</property> 
</bean> 
</property> 
</bean> 
</property> 
</bean> 
</beans> 
  • 启动 IgniteServer程序

二、创建IgniteClient

  • 创建 Maven工程,在pom.xm中添加
<dependency> 
<groupId>org.apache.ignite</groupId> 
<artifactId>ignite-core</artifactId> 
<version>${ignite.version}</version> 
</dependency> 

<dependency> 
<groupId>org.apache.ignite</groupId> 
<artifactId>ignite-spring</artifactId> 
<version>${ignite.version}</version> 
</dependency> 

<dependency> 
<groupId>org.apache.ignite</groupId> 
<artifactId>ignite-indexing</artifactId> 
<version>${ignite.version}</version> 
</dependency> 
  • 在 IgniteClientApplication类中添加如下代码
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("127.0.0.1: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(); 
} 

/** 
* A compute tasks that prints out a node ID and some details about its OS and JRE. 
* Plus, the code shows how to access data stored in a cache from the compute task. 
*/ 
private static class RemoteTask implements IgniteRunnable { 
@IgniteInstanceResource 
Ignite ignite; 

@Override 
public void run() { 
System.out.println(">> Executing the compute task"); 

System.out.println( 
" Node ID: " + ignite.cluster().localNode().id() + "\n" + 
" OS: " + System.getProperty("os.name") + 
" JRE: " + System.getProperty("java.runtime.name")); 

IgniteCache<Integer, String> cache = ignite.cache("myCache"); 

System.out.println(">> " + cache.get(1) + " " + cache.get(2)); 
} 
} 
  • 启用IgniteClient程序
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值