hazelcast入门教程_Hazelcast入门指南第6部分
hazelcast入門教程
這是有關(guān)Hazelcast的一系列文章中的第六篇。 如果一個(gè)人沒(méi)有看過(guò)過(guò)去的五年,請(qǐng)到表中的內(nèi)容后 ,我創(chuàng)建趕上。
本地客戶
在上一篇文章之后,我決定要去本地化。 是的,我將演示Hazelcast自己的Java客戶端。 Java不是唯一的本地客戶端語(yǔ)言,C ++和C#風(fēng)格可用于企業(yè)版。
為什么要本地化?
這是一個(gè)好問(wèn)題。 本地客戶可以將其固定在一條產(chǎn)品線中,而不必逃脫。 Hazelcast通過(guò)以下方式獎(jiǎng)勵(lì)本地人:
- 客戶端是集群的一部分。 這意味著人們可以創(chuàng)建存儲(chǔ)數(shù)據(jù)的場(chǎng)所,并監(jiān)聽集群中發(fā)生的事件。 這也意味著我之前的文章中討論的所有技巧都可以用作客戶端。 此優(yōu)勢(shì)不可低估。
- 配置文件類似。 這意味著不必從Hazelcast配置文件轉(zhuǎn)換為客戶端配置文件。 一個(gè)人可以復(fù)制文件,就像魔術(shù)一樣起作用。 翻譯要做的越少,丟失的內(nèi)容就越少。
任何客戶的經(jīng)驗(yàn)法則
Hazelcast客戶是我有幸設(shè)置和使用的最簡(jiǎn)單的客戶端。
例
這個(gè)簡(jiǎn)單的示例是上一篇文章開始的主題的延續(xù),緩存了昂貴的操作。
Pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.darylmathison</groupId><artifactId>HazelcastJavaClient</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><showDeprecation>true</showDeprecation></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.3.2</version><executions><execution><goals><goal>java</goal></goals></execution></executions><configuration><mainClass>com.darylmathison.hazelcastjavaclient.Main</mainClass></configuration></plugin></plugins></build><dependencies><dependency><groupId>com.hazelcast</groupId><artifactId>hazelcast-client</artifactId><version>3.3.2</version></dependency></dependencies></project>客戶
該客戶端成為集群的一部分,創(chuàng)建一個(gè)名為“ fibmap”的IMap。 如果以前沒(méi)有計(jì)算過(guò)斐波那契結(jié)果,則將其存儲(chǔ)在地圖中。 如果運(yùn)行一次客戶端,則結(jié)果存儲(chǔ)在fibmap中。 第二次運(yùn)行客戶端時(shí),將顯示緩存的值。
package com.darylmathison.hazelcastjavaclient;import com.hazelcast.client.HazelcastClient; import com.hazelcast.core.HazelcastInstance; import java.util.Map;/**** @author Daryl*/ public class Main {/*** @param args the command line arguments*/public static void main(String[] args) {HazelcastInstance instance = HazelcastClient.newHazelcastClient();Map<Long, Long> cacheMap = instance.getMap("fibmap");for(long i = 1; i <= 10L; i++) {System.out.println("value is " + fibonacci(i, cacheMap));}instance.shutdown();}private static long fibonacci(long rounds, Map<Long, Long> cacheMap) {Long cached = cacheMap.get(rounds);if(cached != null) {System.out.print("cached ");return cached;}long[] lastTwo = new long[] {1, 1};for(int i = 0; i < rounds; i++) {long last = lastTwo[1];lastTwo[1] = lastTwo[0] + lastTwo[1];lastTwo[0] = last;}cacheMap.put(rounds, lastTwo[1]);return lastTwo[1];}}結(jié)論
在本文中,我討論了使用Hazelcast的本機(jī)Java客戶端的原因。 我還展示了一個(gè)簡(jiǎn)單的示例。 該代碼可以在這里找到。
參考
當(dāng)涉及到《 Hazelcast入門指南》時(shí)。 我一直在瀏覽www.hazelcast.com和www.hazelcast.org 。
翻譯自: https://www.javacodegeeks.com/2015/02/beginners-guide-hazelcast-part-6.html
hazelcast入門教程
總結(jié)
以上是生活随笔為你收集整理的hazelcast入门教程_Hazelcast入门指南第6部分的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: rx 异步执行耗时_使用rx-java的
- 下一篇: 主要矛盾和次要矛盾_次要GC,主要GC与