当前位置: 首页 > 知识库问答 >
问题:

使用Hazelcast配置Jcache

卫嘉谊
2023-03-14

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

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

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。

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

共有1个答案

娄鹤轩
2023-03-14

将jcache api JAR添加到类路径

 类似资料:
  • 大家好 我尝试在SpringBoot应用程序中使用Hazelcast作为本地缓存,如下教程:https://hazelcast.com/blog/non-stop-client-with-near-cache/ 黑泽尔铸造分级版: 在我的版本中,我没有看到为连接到集群设置最大超时的方法(setClusterConnectTimeoutMillis()): ..如果没有它,它就会掉下来 com.ha

  • 本文向大家介绍Spring Boot Hazelcast Caching 使用和配置详解,包括了Spring Boot Hazelcast Caching 使用和配置详解的使用技巧和注意事项,需要的朋友参考一下 本文将展示spring boot 结合 Hazelcast 的缓存使用案例。 1. Project Structure 2. Maven Dependencies 3. Hazelcast

  • 我创建了一个Spring、Hibernate、Hazelcast集成应用程序。 Spring配置文件如下所示:- Spring扩展上下文.xml 各种黑泽尔卡斯特教程要求我在黑泽尔卡斯特中复制以下代码行.xml。 但是我没有使用最终在Hazelcast包的bin文件夹中找到的Hazelcast.xml。 相反,我将Hazelcast jars复制到eclipse中的lib文件夹中。 要运行Manc

  • 我定义了“静态”hazelcast配置: 其中“10.0.0.2”是我的localhostip。我只希望将hazelcast的一个实例添加到我的tcpIpConfig成员中。我的朋友坐在同一个网络中,拥有编号为“10.0.0.3”的IP。他懒得从git上共享的属性文件中更改密码和组名,并且正在连接到我的集群。为什么他能够连接到我的集群?我如何防止这种情况?

  • 我将Hazelcast作为缓存服务运行,似乎我可以通过BoringSSL提高性能,这更简单,因为我不需要安装其他软件 阅读他们的文档:https://docs.hazelcast.com/imdg/4.1.2/security/integrating-openssl.html 我知道我只需要两个罐子,但我没有看到任何关于配置设置的提及。我是否只将Java SSL设置用于BasicSSLContex

  • 上面写着 随着动态添加的数据结构配置在所有集群成员之间传播,可能会由于超时和网络分区等情况而发生故障。只要检测到成员资格更改,配置传播机制就会在内部重试添加配置。 这给我的印象是配置会传播。