Apache Ignite 复制缓存在客户端不可用

Apache Ignite replicated cache not available across clients

我有两个客户端和一个服务器。

[14:02:16] Topology snapshot [ver=8, servers=1, clients=2, CPUs=8, heap=7.8GB]

我创建了一个复制的缓存,我希望两个实例都可以访问相同的令牌缓存,即一个客户端添加的令牌可以被另一个客户端读取,反之亦然。

private IgniteCache<Long, CacheToken> getOrCreateCache() {
    CacheConfiguration<Long, CacheToken> tokenCacheConfig = new CacheConfiguration<>(REVOCATION_CACHE);

    tokenCacheConfig.setCacheMode(CacheMode.REPLICATED);
    tokenCacheConfig.setIndexedTypes(Long.class, CacheToken.class);
    tokenCacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.THIRTY_MINUTES));

    return ignite.getOrCreateCache(tokenCacheConfig);
}

当我查看缓存时,我实际看到的是正在创建两个独立的同名缓存,而不是复制它们。

Nodes for: TOKEN_REVOCATION_LIST(@c2)
+==============================================================================================+
|      Node ID8(@), IP       | CPUs | Heap Used | CPU Load |   Up Time    | Size | Hi/Mi/Rd/Wr |
+==============================================================================================+
| 0794F94D(@n2), xx.xx.xx.xx | 8    | 22.79 %   | 0.03 %   | 00:54:44:853 | 14   | Hi: 0       |
|                            |      |           |          |              |      | Mi: 0       |
|                            |      |           |          |              |      | Rd: 0       |
|                            |      |           |          |              |      | Wr: 0       |
+----------------------------+------+-----------+----------+--------------+------+-------------+
| 7B9F7E25(@n1), xx.xx.xx.xx | 8    | 23.12 %   | 0.13 %   | 00:55:00:366 | 0    | Hi: 0       |
|                            |      |           |          |              |      | Mi: 0       |
|                            |      |           |          |              |      | Rd: 0       |
|                            |      |           |          |              |      | Wr: 0       |
+----------------------------+------+-----------+----------+--------------+------+-------------+
| E7321C6D(@n0), xx.xx.xx.xx | 8    | 6.90 %    | 0.03 %   | 00:55:08:795 | 0    | Hi: 0       |
|                            |      |           |          |              |      | Mi: 0       |
|                            |      |           |          |              |      | Rd: 0       |
|                            |      |           |          |              |      | Wr: 0       |
+----------------------------------------------------------------------------------------------+

我希望看到的行为是一个共享缓存,或者至少是跨节点复制令牌。从每个节点内,我可以读取节点写入的值,但不能跨节点读取。

我应该对配置进行哪些更改才能实现共享的复制缓存?有这样的例子吗?

@Configuration
public class IgniteConfig {

@Bean
public Ignite ignite() {
    return Ignition.start(igniteConfiguration());
}

@Bean
public JdbcDataSource h2ExampleDb() {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:tcp://localhost/mem:ExampleDb");
    ds.setUser("sa");
    return ds;
}

@Bean
public IgniteConfiguration igniteConfiguration() {
    IgniteConfiguration ic = new IgniteConfiguration();
    ic.setClientMode(true);
    ic.setPeerClassLoadingEnabled(true);
    ic.setIncludeEventTypes(getEventTypes());
    ic.setDiscoverySpi(discoverySpi());
    ic.setCacheConfiguration(cacheConfiguration());
    return ic;
}

public CacheConfiguration cacheConfiguration() {
    CacheConfiguration cc = new CacheConfiguration();
    cc.setCacheMode(CacheMode.REPLICATED);
    return cc;
}

public int[] getEventTypes() {
    int[] eventTypes = {
            EventType.EVT_TASK_STARTED,
            EventType.EVT_TASK_FINISHED,
            EventType.EVT_TASK_FAILED,
            EventType.EVT_TASK_TIMEDOUT,
            EventType.EVT_TASK_SESSION_ATTR_SET,
            EventType.EVT_TASK_REDUCED,
            EventType.EVT_CACHE_OBJECT_PUT,
            EventType.EVT_CACHE_OBJECT_READ,
            EventType.EVT_CACHE_OBJECT_REMOVED,
            EventType.EVT_CLIENT_NODE_RECONNECTED,
            EventType.EVT_CACHE_OBJECT_EXPIRED
    };

    return eventTypes;
}

@Bean
public TcpDiscoverySpi discoverySpi() {
    TcpDiscoverySpi spi = new TcpDiscoverySpi();
    spi.setIpFinder(tdif());
    return spi;
}

@Bean
public TcpDiscoveryMulticastIpFinder tdif() {
    TcpDiscoveryMulticastIpFinder finder = new TcpDiscoveryMulticastIpFinder();
    List<String> addresses = new ArrayList<>();
    addresses.add("127.0.0.1:47500..47509");
    finder.setAddresses(addresses);
    return finder;
}
}

客户端节点不存储分布式缓存(复制或分区)的数据。您只需要一个副本,因为您只有一个服务器节点。只需启动更多服务器节点,您就会看到缓存已复制到所有服务器节点。