首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Hazelcast配置Jcache

用Hazelcast配置Jcache
EN

Stack Overflow用户
提问于 2015-03-25 17:22:02
回答 1查看 4.3K关注 0票数 0

我有两个节点Hazelcast (3.4.1)集群正在运行。我已经将它配置为使用本机内存。(基本上试着使用高密度内存存储)。已经编写了一个客户端代码来连接到那些集群来执行写操作。

我试图用Hazelcast配置Jcache,以便使用缓存存储。Jcache提供程序类型: client。另外,我希望在下面使用声明式配置,这是我的客户端代码& configuration。

代码语言:javascript
复制
public class Test {

  public static void main(String[] args) {

    try {
        System.setProperty("hazelcast.config",
                "classpath:conf/hazelcast.xml");
        System.setProperty("hazelcast.jcache.provider.type", "client");
        CachingProvider cachingProvider = Caching.getCachingProvider();

        CacheManager cacheManager = cachingProvider.getCacheManager();
        ICache<String, Object> cache = cacheManager.getCache("Classroom",String.class, Object.class).unwrap(ICache.class);
        CacheConfig cacheConfig = cache.getConfiguration(CacheConfig.class);

        System.out.println("key type: " + cacheConfig.getKeyType());
        System.out.println("value type: " + cacheConfig.getValueType());
        cache.put("1", "value1");
        System.out.println("-->" + cache.get("1"));

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

我在客户端使用的hazelcast.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast
xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-     
 3.4.xsd"
xmlns="http://www.hazelcast.com/schema/config"    
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<group>
    <name>dev</name>
    <password>dev-pass</password>
</group>
<management-center enabled="true">http://ipaddr:8080/mancenter
</management-center>
<network>
    <port auto-increment="true" port-count="100">5701</port>
    <outbound-ports>
        <!-- Allowed port range when connecting to other nodes. 0 or * means use 
            system provided port. -->
        <ports>0</ports>
    </outbound-ports>
    <join>
        <multicast enabled="false">
            <multicast-group>224.2.2.3</multicast-group>
            <multicast-port>54327</multicast-port>
        </multicast>
        <tcp-ip enabled="true">
            <member>my domain addr 1 </member>
            <member>my domain addr 2</member>

            <connection-timeout-seconds>20</connection-timeout-seconds>
        </tcp-ip>
        <aws enabled="false">
            <access-key>my-access-key</access-key>
            <secret-key>my-secret-key</secret-key>
            <!--optional, default is us-east-1 -->
            <region>us-west-1</region>
            <!--optional, default is ec2.amazonaws.com. If set, region shouldn't 
                be set as it will override this property -->
            <host-header>ec2.amazonaws.com</host-header>
            <!-- optional, only instances belonging to this group will be discovered, 
                default will try all running instances -->
            <security-group-name>hazelcast-sg</security-group-name>
            <tag-key>type</tag-key>
            <tag-value>hz-nodes</tag-value>
        </aws>
    </join>
    <interfaces enabled="false">
        <interface>10.*.*.*</interface>
    </interfaces>


    <ssl enabled="false" />
    <socket-interceptor enabled="false" />
    <symmetric-encryption enabled="false">
        <!-- encryption algorithm such as DES/ECB/PKCS5Padding, PBEWithMD5AndDES, 
            AES/CBC/PKCS5Padding, Blowfish, DESede -->
        <algorithm>PBEWithMD5AndDES</algorithm>
        <!-- salt value to use when generating the secret key -->
        <salt>thesalt</salt>
        <!-- pass phrase to use when generating the secret key -->
        <password>thepass</password>
        <!-- iteration count to use when generating the secret key -->
        <iteration-count>19</iteration-count>
    </symmetric-encryption>
</network>
<partition-group enabled="false" />
<executor-service name="default">
    <pool-size>16</pool-size>
    <!--Queue capacity. 0 means Integer.MAX_VALUE. -->
    <queue-capacity>0</queue-capacity>
</executor-service>
<queue name="default">
    <!-- Maximum size of the queue. When a JVM's local queue size reaches the 
        maximum, all put/offer operations will get blocked until the queue size of 
        the JVM goes down below the maximum. Any integer between 0 and Integer.MAX_VALUE. 
        0 means Integer.MAX_VALUE. Default is 0. -->
    <max-size>0</max-size>
    <!-- Number of backups. If 1 is set as the backup-count for example, then 
        all entries of the map will be copied to another JVM for fail-safety. 0 means 
        no backup. -->
    <backup-count>0</backup-count>

    <!-- Number of async backups. 0 means no backup. -->
    <async-backup-count>0</async-backup-count>

    <empty-queue-ttl>-1</empty-queue-ttl>
</queue>
<map name="default">
    <!-- Data type that will be used for storing recordMap. Possible values: 
        BINARY (default): keys and values will be stored as binary data OBJECT : 
        values will be stored in their object forms NATIVE : values will be stored 
        in non-heap region of JVM -->
    <in-memory-format>NATIVE</in-memory-format>

    <!-- Number of backups. If 1 is set as the backup-count for example, then 
        all entries of the map will be copied to another JVM for fail-safety. 0 means 
        no backup. -->
    <backup-count>0</backup-count>
    <!-- Number of async backups. 0 means no backup. -->
    <async-backup-count>0</async-backup-count>
    <!-- Maximum number of seconds for each entry to stay in the map. Entries 
        that are older than <time-to-live-seconds> and not updated for <time-to-live-seconds> 
        will get automatically evicted from the map. Any integer between 0 and Integer.MAX_VALUE. 
        0 means infinite. Default is 0. -->
    <time-to-live-seconds>0</time-to-live-seconds>
    <!-- Maximum number of seconds for each entry to stay idle in the map. 
        Entries that are idle(not touched) for more than <max-idle-seconds> will 
        get automatically evicted from the map. Entry is touched if get, put or containsKey 
        is called. Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. 
        Default is 0. -->
    <max-idle-seconds>0</max-idle-seconds>
    <!-- Valid values are: NONE (no eviction), LRU (Least Recently Used), LFU 
        (Least Frequently Used). NONE is the default. -->
    <eviction-policy>NONE</eviction-policy>
    <!-- Maximum size of the map. When max size is reached, map is evicted 
        based on the policy defined. Any integer between 0 and Integer.MAX_VALUE. 
        0 means Integer.MAX_VALUE. Default is 0. -->
    <max-size policy="PER_NODE">0</max-size>
    <!-- When max. size is reached, specified percentage of the map will be 
        evicted. Any integer between 0 and 100. If 25 is set for example, 25% of 
        the entries will get evicted. -->
    <eviction-percentage>25</eviction-percentage>
    <!-- Minimum time in milliseconds which should pass before checking if 
        a partition of this map is evictable or not. Default value is 100 millis. -->
    <min-eviction-check-millis>100</min-eviction-check-millis>
    <!-- While recovering from split-brain (network partitioning), map entries 
        in the small cluster will merge into the bigger cluster based on the policy 
        set here. When an entry merge into the cluster, there might an existing entry 
        with the same key already. Values of these entries might be different for 
        that same key. Which value should be set for the key? Conflict is resolved 
        by the policy set here. Default policy is PutIfAbsentMapMergePolicy There 
        are built-in merge policies such as com.hazelcast.map.merge.PassThroughMergePolicy; 
        entry will be overwritten if merging entry exists for the key. com.hazelcast.map.merge.PutIfAbsentMapMergePolicy 
        ; entry will be added if the merging entry doesn't exist in the cluster. 
        com.hazelcast.map.merge.HigherHitsMapMergePolicy ; entry with the higher 
        hits wins. com.hazelcast.map.merge.LatestUpdateMapMergePolicy ; entry with 
        the latest update wins. -->
    <merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy
    </merge-policy>

</map>

<multimap name="default">
    <backup-count>0</backup-count>
    <value-collection-type>SET</value-collection-type>
</multimap>

<multimap name="default">
    <backup-count>0</backup-count>
    <value-collection-type>SET</value-collection-type>
</multimap>

<list name="default">
    <backup-count>0</backup-count>
</list>

<set name="default">
    <backup-count>0</backup-count>
</set>

<jobtracker name="default">
    <max-thread-size>0</max-thread-size>
    <!-- Queue size 0 means number of partitions * 2 -->
    <queue-size>0</queue-size>
    <retry-count>0</retry-count>
    <chunk-size>1000</chunk-size>
    <communicate-stats>true</communicate-stats>
    <topology-changed-strategy>CANCEL_RUNNING_OPERATION
    </topology-changed-strategy>
</jobtracker>

<semaphore name="default">
    <initial-permits>0</initial-permits>
    <backup-count>1</backup-count>
    <async-backup-count>0</async-backup-count>
</semaphore>

<serialization>
    <portable-version>0</portable-version>
</serialization>

<services enable-defaults="true" />

<cache name="Classroom">

    <key-type class-name="java.lang.String" />
    <value-type class-name="java.lang.Object" />
    <statistics-enabled>false</statistics-enabled>
    <management-enabled>false</management-enabled>
</cache>

<properties>
    <property name="hazelcast.logging.type">jdk</property>
    <property name="hazelcast.jcache.provider.type">client</property>
</properties>

</hazelcast>

当执行客户端代码时。我得到了以下例外。

代码语言:javascript
复制
            Mar 25, 2015 10:05:19 PM com.hazelcast.client.config.XmlClientConfigLocator
            INFO: Loading 'hazelcast-client-default.xml' from classpath.
            Mar 25, 2015 10:05:19 PM com.hazelcast.core.LifecycleService
            INFO: HazelcastClient[hz.client_0_dev][3.4.1] is STARTING
            Mar 25, 2015 10:05:20 PM com.hazelcast.core.LifecycleService
            INFO: HazelcastClient[hz.client_0_dev][3.4.1] is STARTED
            Mar 25, 2015 10:05:22 PM com.hazelcast.core.LifecycleService
            INFO: HazelcastClient[hz.client_0_dev][3.4.1] is CLIENT_CONNECTED
            Mar 25, 2015 10:05:22 PM com.hazelcast.client.spi.impl.ClusterListenerThread
            INFO: 

            Members [1] {
                Member [mylocalipaddress]:5701
            }

            javax.cache.CacheException: Error opening URI [hazelcast]
                at com.hazelcast.cache.impl.AbstractHazelcastCachingProvider.getCacheManager(AbstractHazelcastCachingProvider.java:89)
                at com.hazelcast.cache.impl.AbstractHazelcastCachingProvider.getCacheManager(AbstractHazelcastCachingProvider.java:118)
                at com.hazelcast.cache.HazelcastCachingProvider.getCacheManager(HazelcastCachingProvider.java:158)
                at com.apollo.cache.manager.Test.main(Test.java:61)
            Caused by: java.lang.IllegalArgumentException: No service registered with name: hz:impl:cacheService
                at com.hazelcast.client.impl.ClientEngineImpl$ClientPacketProcessor.initService(ClientEngineImpl.java:477)
                at com.hazelcast.client.impl.ClientEngineImpl$ClientPacketProcessor.processRequest(ClientEngineImpl.java:427)
                at com.hazelcast.client.impl.ClientEngineImpl$ClientPacketProcessor.run(ClientEngineImpl.java:353)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
                at java.lang.Thread.run(Thread.java:744)
                at com.hazelcast.util.executor.HazelcastManagedThread.executeRun(HazelcastManagedThread.java:76)
                at com.hazelcast.util.executor.HazelcastManagedThread.run(HazelcastManagedThread.java:92)
                at ------ End remote and begin local stack-trace ------.(Unknown Source)
                at com.hazelcast.client.spi.impl.ClientCallFuture.resolveResponse(ClientCallFuture.java:201)
                at com.hazelcast.client.spi.impl.ClientCallFuture.get(ClientCallFuture.java:142)
                at com.hazelcast.client.spi.impl.ClientCallFuture.get(ClientCallFuture.java:118)
                at com.hazelcast.client.spi.ProxyManager.initialize(ProxyManager.java:200)
                at com.hazelcast.client.spi.ProxyManager.getOrCreateProxy(ProxyManager.java:183)
                at com.hazelcast.client.impl.HazelcastClientInstanceImpl.getDistributedObject(HazelcastClientInstanceImpl.java:391)
                at com.hazelcast.client.impl.HazelcastClientProxy.getDistributedObject(HazelcastClientProxy.java:234)
                at com.hazelcast.client.cache.impl.HazelcastClientCacheManager.<init>(HazelcastClientCacheManager.java:62)
                at com.hazelcast.client.cache.impl.HazelcastClientCachingProvider.createHazelcastCacheManager(HazelcastClientCachingProvider.java:75)
                at com.hazelcast.client.cache.impl.HazelcastClientCachingProvider.createHazelcastCacheManager(HazelcastClientCachingProvider.java:38)
                at com.hazelcast.cache.impl.AbstractHazelcastCachingProvider.getCacheManager(AbstractHazelcastCachingProvider.java:86)

我甚至根据为jcache提供的示例进行了尝试。没能成功。不知道我哪里出了问题。另外,我不确定是否配置了jcache。正如文档中提到的,拥有所有依赖的jars。

Usecase:启用了本机缓存(高密度内存存储)的Hazelcast集群。运行在另一个节点上的Hazelcast Java客户端(jcache配置)应该连接到这个集群,并在缓存中执行读/写操作。

EN

回答 1

Stack Overflow用户

发布于 2015-03-25 19:44:51

将jcache api JAR添加到类路径中。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29262341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档