温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么构建多平台的Ignite集群

发布时间:2021-12-16 16:19:45 来源:亿速云 阅读:119 作者:iii 栏目:云计算

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

构建多平台的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”: 怎么构建多平台的Ignite集群

  • 选择Maven然后点击“Next”: 怎么构建多平台的Ignite集群

  • 输入Maven信息,点击“Next”然后“Finish”: 怎么构建多平台的Ignite集群 怎么构建多平台的Ignite集群

  • 完成之后,会看到新项目打开的pom.xml文件: 怎么构建多平台的Ignite集群

  • project片段增加Ignite依赖:

<dependencies>
    <dependency>
        <groupId>org.apache.ignite</groupId>
        <artifactId>ignite-core</artifactId>
        <version>1.7.0</version>
    </dependency>
</dependencies>
  • IDEA可能会询问是否导入项目改变,点击任意的链接: 怎么构建多平台的Ignite集群

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

import org.apache.ignite.Ignition;

public class Demo {
    public static void main(String[] args) {
        Ignition.start();
    }
}
  • 通过Shift+F10运行,然后在IDEA的控制台上确认节点是否启动:

怎么构建多平台的Ignite集群

  • 通过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中只支持BinaryBasicIdMapperBinaryBasicNameMapper,需要在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集群”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI