怎么构建多平台的Ignite集群

56次阅读
没有评论

共计 4968 个字符,预计需要花费 13 分钟才能阅读完成。

本篇内容介绍了“怎么构建多平台的 Ignite 集群”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

构建多平台的 Ignite 集群:Java+.NET

Ignite 集群可以由它支持的任意平台启动的节点组成,包括 Java、.NET 和 C ++。本文会介绍如何通过 NuGet 和 Maven 运行一个.NET/Java 集群

前提条件

本文适用于对 Java 不熟悉的.NET 开发人员,反之亦然,因此描述的会比较详细。本文会使用如下的软件:

Visual Studio 2015(包括 NuGet; 免费社区版);

IntelliJ IDEA (包括 Maven; 免费社区版)。

目标

连接 Java 和.NET 节点;

使用 Java 和.NET 类,通过同样的名字和字段访问 Ignite 缓存中的共享数据;

运行持续查询,观察来自另一个平台的实时数据更新。

Java 工程设置

启动 IntelliJ IDEA,然后点击“Create New Project”:

选择 Maven 然后点击“Next”:

输入 Maven 信息,点击“Next”然后“Finish”:

完成之后,会看到新项目打开的 pom.xml 文件:

为 project 片段增加 Ignite 依赖:

dependencies 
  dependency 
  groupId org.apache.ignite /groupId 
  artifactId ignite-core /artifactId 
  version 1.7.0 /version 
  /dependency 
 /dependencies

IDEA 可能会询问是否导入项目改变,点击任意的链接:

在 src\main\java 中添加 Demo 类,代码如下:

import org.apache.ignite.Ignition;
public class Demo { public static void main(String[] args) { Ignition.start();
 }
}

通过 Shift+F10 运行,然后在 IDEA 的控制台上确认节点是否启动:

通过 Ctrl+F2 或者停止按钮终止程序。

.NET 工程设置

启动 Visual Studio 然后点击 File – New – Project:

选择 Visual C# – Windows – Console Application:

确保在上边选择了.NET Framework 版本 4 及以上

点击“OK”,然后就会生成一个空的控制台工程;

打开 NuGet 控制台:Menu – Tools – NuGet Package Manager – Package Manager Console;

输入 Install-Package Apache.Ignite:

点击回车,然后就会输出 Successfully installed Apache.Ignite 1.7.0 to IgniteMultiPlatform 这样的消息。

将 Program.cs 的内容改成如下这样:

using System;
using Apache.Ignite.Core;
class Program
 static void Main(string[] args)
 { Ignition.Start();
 Console.ReadKey();
 }
}

通过 Ctrl-F5 启动程序,然后在控制台中确认 Ignite 节点已经启动:

调整 Java 节点的配置以发现.NET 节点

现在,就可以同时在 IDEA 中启动 Java 节点,在 Visual Studio 中启动.NET 节点,这时会在他们中的一个发现如下的错误:

IgniteSpiException: Local node s binary configuration is not equal to remote node s binary configuration [locBinaryCfg={globSerializer=null, compactFooter=true, globIdMapper=org.apache.ignite.binary.BinaryBasicIdMapper}, rmtBinaryCfg=null]

这个错误是说,.NET 节点在 BinaryConfiguration 中只支持 BinaryBasicIdMapper 和 BinaryBasicNameMapper,需要在 Java 中显式地进行设置,将 Ignition.start(); 行改成如下的代码:

BinaryConfiguration binCfg = new BinaryConfiguration();
binCfg.setIdMapper(new BinaryBasicIdMapper());
binCfg.setNameMapper(new BinaryBasicNameMapper());
IgniteConfiguration cfg = new IgniteConfiguration().setBinaryConfiguration(binCfg);
Ignition.start(cfg);

这时同时启动 Java 和.NET 节点,验证他们可以发现对方:

[15:04:17] Topology snapshot [ver=2, servers=2, clients=0, CPUs=8, heap=7.1GB]

通过 Ignite 缓存进行数据交换

现在各个节点已经联通,之后会在每个平台上写一个简单的聊天程序来演示数据的交换。代码非常简单,因为 API 是相同的,并且语言语法也差不多。首先,定义名字和成员完全相同的类。

Java Message 类

右击 src\main\java 项目文件夹然后选择 New – Java Class,输入 Message 名字,代码如下:

public class Message { public Message(String author, String text) {
 this.author = author;
 this.text = text;
 }
 final String author;
 final String text;
}

.NET Message 类

右击 Solution Explorer 的项目节点,然后选择 Add – Class…,输入 Message 名字,代码如下:

class Message
 public Message(string author, string text)
 {
 Author = author;
 Text = text;
 }
 public string Author { get; }
 public string Text { get; }
}

Basic 映射器是区分大小写的,并且会忽略命名空间(包),因此这两个类是可以互相映射的,可以在一个平台中将 Message 实例注入缓存,然后在另一个平台中获取。现在开始写聊天程序本身,逻辑比较简单:用户输入一个聊天信息,然后将其注入缓存,持续查询会收到所有的缓存更新通知并且显示他们。

Java 聊天程序

将 main 方法的代码改成如下:

// Retrieve user name
System.out.print( Hi, enter your name:  
Scanner consoleScanner = new Scanner(System.in);
String name = consoleScanner.nextLine();
// Get or create cache
IgniteCache Long, Message  cache = ignite.getOrCreateCache( chat 
// Initialize unique ID sequence
IgniteAtomicSequence messageId = ignite.atomicSequence(chatId , 0, true);
// Set up continuous query
ContinuousQuery Long, Message  qry = new ContinuousQuery ();
qry.setLocalListener(iterable -  {
 // This will be invoked immediately on each cache update
 for (CacheEntryEvent ? extends Long, ? extends Message  evt : iterable)
 System.out.println(evt.getValue().author +  :   + evt.getValue().text);
cache.query(qry);
// Run the chat loop
while (true) {
 System.out.print(   
 
 String msgText = consoleScanner.nextLine();
 Long msgId = messageId.incrementAndGet();
 
 cache.put(msgId, new Message(name, msgText));
}

.NET 聊天程序

在 Ignite.NET 中有两处不同(这些特性预计会在下一版本中实现):

需要在 BinaryConfiguration 中注册一个用于缓存的类型(Java 会自动做这个事);

API 中还不支持 Lambda 表达式,需要单独地实现 ICacheEntryEventListener K, V 接口。

因此,创建一个单独的类,代码如下:

using System;
using System.Collections.Generic;
using Apache.Ignite.Core.Cache.Event;
class CacheListener : ICacheEntryEventListener long, Message 
 public void OnEvent(IEnumerable ICacheEntryEvent long, Message  evts)
 { foreach (var evt in evts)
 Console.WriteLine($ {evt.Value.Author}: {evt.Value.Text} 
 }
}

然后更新 Main 方法:

// Retrieve user name
Console.Write( Hi, enter your name:  
var name = Console.ReadLine();
// Register Message type
var cfg = new IgniteConfiguration
 BinaryConfiguration = new BinaryConfiguration(typeof(Message))
// Start Ignite and retrieve cache
var ignite = Ignition.Start(cfg);
var cache = ignite.GetOrCreateCache long, Message ( chat 
// Initialize unique ID sequence
var messageId = ignite.GetAtomicSequence(chatId , 0, true);
// Set up continuous query
cache.QueryContinuous(new ContinuousQuery long, Message (new CacheListener()));
// Run the chat loop
while (true)
 Console.Write(   
 var msgText = Console.ReadLine();
 var msgId = messageId.Increment();
 cache[msgId] = new Message(name, msgText);
}

“怎么构建多平台的 Ignite 集群”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计4968字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)