7

实战学习掌握ignite技术运用03

 3 years ago
source link: https://blog.shareworld.vip/archives/ignite03
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

实战学习掌握ignite技术运用03

Owen Jia 2021年01月27日 41次浏览

ignite-example

这是一篇引导初学者如何使用ignite技术的实战指导,可能好多高级没有走过也可以用来参考,继续深入学习ignite技术运用。

掌握这个样例,可以说ignite入门了!
后续的进阶要在实战中积累,如springdata结合使用、集群维护等。

下载apache-ignite-2.9.1-bin.zip

下载ignite2.9.1版本的zip,本地解压即可,然后将default-config.xml文件修改为如下配置(同example配置一致)。

<?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"> <property name="peerClassLoadingEnabled" value="true"/> <property name="includeEventTypes"> <list> <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"/> <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="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> <value>127.0.0.1:47500..47509</value> </list> </property> </bean> </property> </bean> </property> </bean></beans>

最后直接启动 $ ./ignite.sh -v 即可,端口会自动分配,不用修改其他。

下载ignite-example用例

1、在apache-ignite-2.9.1-bin.zip包解压后的examples目录里面就是,可以直接复制出了用。点击下载

2、下载apache-ignite-2.9.1-src.zip解压后的example目录就是,这个是代test样例代码的。点击下载

3、下载我的样例ignite-example。点击下载

代码下载后使用idea的maven把jar下载后complie即可运行,这里需要注意配置文件的访问路径需要修改,不然起不来(因为examples的上下文在copy建立新工程时改变了,我的测试中是没有examples目录的)。

样例工程介绍

样例覆盖范围涵盖了ignite所能支持的所有场景,下面ClusterGroup和Events样例运行需要启动两个ignite节点进行配合。

ClusterGroupExample

public static void main(String[] args) throws IgniteException { try (Ignite ignite = Ignition.start("./config/example-ignite.xml")) { if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2)) return; System.out.println(); System.out.println("Compute example started."); IgniteCluster cluster = ignite.cluster(); // Say hello to all nodes in the cluster, including local node. sayHello(ignite, cluster); // Say hello to all remote nodes. sayHello(ignite, cluster.forRemotes()); // Pick random node out of remote nodes. ClusterGroup randomNode = cluster.forRemotes().forRandom(); // Say hello to a random node. sayHello(ignite, randomNode); // Say hello to all nodes residing on the same host with random node. sayHello(ignite, cluster.forHost(randomNode.node())); // Say hello to all nodes that have current CPU load less than 50%. sayHello(ignite, cluster.forPredicate(n -> n.metrics().getCurrentCpuLoad() < 0.5)); * Print 'Hello' message on remote nodes. * @param ignite Ignite. * @param grp Cluster group. * @throws IgniteException If failed. private static void sayHello(Ignite ignite, final ClusterGroup grp) throws IgniteException { // Print out hello message on all cluster nodes. ignite.compute(grp).broadcast( () -> System.out.println(">>> Hello Node: " + grp.ignite().cluster().localNode().id()));

EventsExample

public static void main(String[] args) throws Exception { try (Ignite ignite = Ignition.start("./config/example-ignite.xml")) { System.out.println(); System.out.println(">>> Events API example started."); // Listen to events happening on local node. localListen(); // Listen to events happening on all cluster nodes. remoteListen(); // Wait for a while while callback is notified about remaining puts. Thread.sleep(1000); * Listen to events that happen only on local node. * @throws IgniteException If failed. private static void localListen() throws IgniteException { System.out.println(); System.out.println(">>> Local event listener example."); Ignite ignite = Ignition.ignite(); IgnitePredicate<TaskEvent> lsnr = evt -> { System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']'); return true; // Return true to continue listening. // Register event listener for all local task execution events. ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION); // Generate task events. ignite.compute().withName("example-event-task").run(() -> System.out.println("Executing sample job.")); // Unsubscribe local task event listener. ignite.events().stopLocalListen(lsnr); * Listen to events coming from all cluster nodes. * @throws IgniteException If failed. private static void remoteListen() throws IgniteException { System.out.println(); System.out.println(">>> Remote event listener example."); // This optional local callback is called for each event notification // that passed remote predicate listener. IgniteBiPredicate<UUID, TaskEvent> locLsnr = (nodeId, evt) -> { // Remote filter only accepts tasks whose name being with "good-task" prefix. assert evt.taskName().startsWith("good-task"); System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName()); return true; // Return true to continue listening. // Remote filter which only accepts tasks whose name begins with "good-task" prefix. IgnitePredicate<TaskEvent> rmtLsnr = evt -> evt.taskName().startsWith("good-task"); Ignite ignite = Ignition.ignite(); // Register event listeners on all nodes to listen for task events. ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION); // Generate task events. for (int i = 0; i < 10; i++) { ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() { // Auto-inject task session. @TaskSessionResource private ComputeTaskSession ses; @Override public void run() { System.out.println("Executing sample job for task: " + ses.getTaskName());

项目编译中遇到maven的jar下载不了可更换镜像仓库到阿里的私服。用例运行属于传统java的application体系,直接run main方法即可。

这些用例写的非常实用,结构简单清晰,真的apache的水准。他们推一个新技术思考的非常全面,就比如doc和example,清晰还全面让人一看就懂。这样最大的好处技术使用成本非常低,和国内很多公司推广开源技术相比,大巫见小巫啊。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK