久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

怎么構(gòu)建多平臺(tái)的Ignite集群

共計(jì) 4968 個(gè)字符,預(yù)計(jì)需要花費(fèi) 13 分鐘才能閱讀完成。

本篇內(nèi)容介紹了“怎么構(gòu)建多平臺(tái)的 Ignite 集群”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

構(gòu)建多平臺(tái)的 Ignite 集群:Java+.NET

Ignite 集群可以由它支持的任意平臺(tái)啟動(dòng)的節(jié)點(diǎn)組成,包括 Java、.NET 和 C ++。本文會(huì)介紹如何通過(guò) NuGet 和 Maven 運(yùn)行一個(gè).NET/Java 集群

前提條件

本文適用于對(duì) Java 不熟悉的.NET 開(kāi)發(fā)人員,反之亦然,因此描述的會(huì)比較詳細(xì)。本文會(huì)使用如下的軟件:

Visual Studio 2015(包括 NuGet; 免費(fèi)社區(qū)版);

IntelliJ IDEA (包括 Maven; 免費(fèi)社區(qū)版)。

目標(biāo)

連接 Java 和.NET 節(jié)點(diǎn);

使用 Java 和.NET 類,通過(guò)同樣的名字和字段訪問(wèn) Ignite 緩存中的共享數(shù)據(jù);

運(yùn)行持續(xù)查詢,觀察來(lái)自另一個(gè)平臺(tái)的實(shí)時(shí)數(shù)據(jù)更新。

Java 工程設(shè)置

啟動(dòng) IntelliJ IDEA,然后點(diǎn)擊“Create New Project”:

選擇 Maven 然后點(diǎn)擊“Next”:

輸入 Maven 信息,點(diǎn)擊“Next”然后“Finish”:

完成之后,會(huì)看到新項(xiàng)目打開(kāi)的 pom.xml 文件:

為 project 片段增加 Ignite 依賴:

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

IDEA 可能會(huì)詢問(wèn)是否導(dǎo)入項(xiàng)目改變,點(diǎn)擊任意的鏈接:

在 src\main\java 中添加 Demo 類,代碼如下:

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

通過(guò) Shift+F10 運(yùn)行,然后在 IDEA 的控制臺(tái)上確認(rèn)節(jié)點(diǎn)是否啟動(dòng):

通過(guò) Ctrl+F2 或者停止按鈕終止程序。

.NET 工程設(shè)置

啟動(dòng) Visual Studio 然后點(diǎn)擊 File – New – Project:

選擇 Visual C# – Windows – Console Application:

確保在上邊選擇了.NET Framework 版本 4 及以上

點(diǎn)擊“OK”,然后就會(huì)生成一個(gè)空的控制臺(tái)工程;

打開(kāi) NuGet 控制臺(tái):Menu – Tools – NuGet Package Manager – Package Manager Console;

輸入 Install-Package Apache.Ignite:

點(diǎn)擊回車,然后就會(huì)輸出 Successfully installed Apache.Ignite 1.7.0 to IgniteMultiPlatform 這樣的消息。

將 Program.cs 的內(nèi)容改成如下這樣:

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

通過(guò) Ctrl-F5 啟動(dòng)程序,然后在控制臺(tái)中確認(rèn) Ignite 節(jié)點(diǎn)已經(jīng)啟動(dòng):

調(diào)整 Java 節(jié)點(diǎn)的配置以發(fā)現(xiàn).NET 節(jié)點(diǎn)

現(xiàn)在,就可以同時(shí)在 IDEA 中啟動(dòng) Java 節(jié)點(diǎn),在 Visual Studio 中啟動(dòng).NET 節(jié)點(diǎn),這時(shí)會(huì)在他們中的一個(gè)發(fā)現(xiàn)如下的錯(cuò)誤:

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]

這個(gè)錯(cuò)誤是說(shuō),.NET 節(jié)點(diǎn)在 BinaryConfiguration 中只支持 BinaryBasicIdMapper 和 BinaryBasicNameMapper,需要在 Java 中顯式地進(jìn)行設(shè)置,將 Ignition.start(); 行改成如下的代碼:

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

這時(shí)同時(shí)啟動(dòng) Java 和.NET 節(jié)點(diǎn),驗(yàn)證他們可以發(fā)現(xiàn)對(duì)方:

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

通過(guò) Ignite 緩存進(jìn)行數(shù)據(jù)交換

現(xiàn)在各個(gè)節(jié)點(diǎn)已經(jīng)聯(lián)通,之后會(huì)在每個(gè)平臺(tái)上寫一個(gè)簡(jiǎn)單的聊天程序來(lái)演示數(shù)據(jù)的交換。代碼非常簡(jiǎn)單,因?yàn)?API 是相同的,并且語(yǔ)言語(yǔ)法也差不多。首先,定義名字和成員完全相同的類。

Java Message 類

右擊 src\main\java 項(xiàng)目文件夾然后選擇 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 的項(xiàng)目節(jié)點(diǎn),然后選擇 Add – Class…,輸入 Message 名字,代碼如下:

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

Basic 映射器是區(qū)分大小寫的,并且會(huì)忽略命名空間(包),因此這兩個(gè)類是可以互相映射的,可以在一個(gè)平臺(tái)中將 Message 實(shí)例注入緩存,然后在另一個(gè)平臺(tái)中獲取。現(xiàn)在開(kāi)始寫聊天程序本身,邏輯比較簡(jiǎn)單:用戶輸入一個(gè)聊天信息,然后將其注入緩存,持續(xù)查詢會(huì)收到所有的緩存更新通知并且顯示他們。

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 中有兩處不同(這些特性預(yù)計(jì)會(huì)在下一版本中實(shí)現(xiàn)):

需要在 BinaryConfiguration 中注冊(cè)一個(gè)用于緩存的類型(Java 會(huì)自動(dòng)做這個(gè)事);

API 中還不支持 Lambda 表達(dá)式,需要單獨(dú)地實(shí)現(xiàn) ICacheEntryEventListener K, V 接口。

因此,創(chuàng)建一個(gè)單獨(dú)的類,代碼如下:

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);
}

“怎么構(gòu)建多平臺(tái)的 Ignite 集群”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-08-16發(fā)表,共計(jì)4968字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒(méi)有評(píng)論)
主站蜘蛛池模板: 和田县| 郓城县| 东乌珠穆沁旗| 镇雄县| 枝江市| 呼和浩特市| 思南县| 望奎县| 屏边| 上虞市| 沂水县| 中山市| 昂仁县| 灌阳县| 德令哈市| 额尔古纳市| 佛冈县| 象州县| 罗山县| 织金县| 荔浦县| 汝州市| 石棉县| 原平市| 临澧县| 油尖旺区| 郓城县| 新余市| 清远市| 长兴县| 密山市| 忻州市| 昌平区| 阳江市| 龙井市| 德阳市| 镇坪县| 通化县| 乌审旗| 滦平县| 襄城县|