【问题标题】:Configuring Jcache with Hazelcast使用 Hazelcast 配置 Jcache
【发布时间】:2015-05-29 12:25:47
【问题描述】:

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

我尝试使用 Hazelcast 配置 Jcache 以使用缓存存储。 Jcache 提供程序类型:客户端。我也想使用声明性配置下面是我的客户端代码和配置 XML。

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

<?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>

客户端代码执行时。我收到以下异常。

            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 是否已配置。如文档中所述,拥有所有依赖的 jar。

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

【问题讨论】:

    标签: hazelcast


    【解决方案1】:

    将 jcache api JAR 添加到类路径

    【讨论】:

    • 我的 pom.xml 中有缓存 api jar
    • 您可能在谈论客户端。你确定你在集群端的运行时也有它吗?缓存服务是处理客户端请求的集群端服务,仅当 JCache JAR 在服务器端运行时可用时才注册。
    • 谢谢诺克塔瑞斯。我的服务器中也会有 jcache。但我不确定这将如何帮助解决上述异常。如果您看到我尝试传递我的自定义配置 xml 的异常,其中包含我的 hazelcast 集群详细信息,而不是选择此 xml,它会加载默认 xml。
    • 客户端xml文件需要调用hazelcast-client.xml并且必须使用hazelcast-client-config.xsd。如果要配置服务器端,必须使用 hazelcast.xml 来配置服务器端。您原来的例外是:“java.lang.IllegalArgumentException: No service registered with name: hz:impl:cacheService”这表示服务器尚未启动 JCache 服务。总而言之,如果在客户端使用 hazelcast.xml,您的应用程序中会出现多个错误。
    • @noctarius 的建议帮助了我。 here 也解决了同样的问题。此外,需要确保将所需的 jar 正确包含到节点的类路径中。检查设置了 CLASSPATH 的 server.sh。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多