尝试配置接口并连接到集群时出现问题

Problem trying to configure an interface and connect to the cluster

大家好,大家好,我正在尝试构建一个界面,该界面将是一个外部客户端,我需要从我的 linux 控制台获得访问权限并执行命令,这是界面

public class HazelcastCacheImpl implements HazelcastCache {

    private HazelcastInstance clientInstance;

    public Boolean initCache() {

        HazelcastInstance status = Hazelcast.newHazelcastInstance();

        if (status.getLifecycleService().isRunning()) {
            return true;
        } else {
            System.out.println("There was a problem trying to run the cluster node");
            return false;
        }

    }

    public Boolean getEntry(String CacheName, String claveReq) {
        IMap<String, ResponseSerializable> map = clientInstance.getMap(CacheName);

        // Predicate<String,ResponseSerializable> query = Predicates.equal("claveReq", claveReq);

        if (!map.isEmpty()) {
            map.values(query);
        } else {
            return false;
        }
        return false;

    }

    public Boolean putEntry(String CacheName, Object key, Object value) {
        KeySerializable keyAux = null;
        ResponseSerializable responseAux = null;

        IMap<String, ResponseSerializable> map = clientInstance.getMap(CacheName);

        if (key.getClass().equals(KeySerializable.class) && value.getClass().equals(ResponseSerializable.class)) {
            keyAux = (KeySerializable) key;
            responseAux = (ResponseSerializable) value;

            String keyOR = keyAux.getClaveReq();

            map.put(keyOR, responseAux);

            return true;

        } else {
            System.out.println("the map doesn't exist or wrong data format");
            return false;
        }

    }

    public int removeEntry(String CacheName, String claveReq, int id_interno_pe, String cod_nrbe_en, int num_sec_ac) {
        IMap<String, ResponseSerializable> map = clientInstance.getMap(CacheName);

        Predicate<String, ResponseSerializable> query = Predicates.equal("claveReq", claveReq);

        if (!map.isEmpty()) {
            map.values(query);
            map.remove(claveReq);

            return 0;
        } else {
            return 0;

        }

    }

    public Boolean removeCache(String CacheName) {

        IMap<String, ResponseSerializable> map = clientInstance.getMap(CacheName);

        if (!map.isEmpty()) {
            map.destroy();
            System.out.println("Cache removed");
            return true;
        } else {
            System.out.println("cannot be deleted because the map doesn't exist, try with the right map name");
            return false;
        }
    }
}

所以...我需要方法“initCache”做的是...连接到一个外部集群,这样它将成为一个 HazelcastClient 然后我需要检查一些我不知道如何去做的事情,如果客户端是 alive/running,如果客户端存在,则删除损坏的客户端并 运行 一个新客户端,如果没有客户端,则 运行

主要问题是连接丢失或连接问题,如果连接丢失,我不知道如何尝试新连接

¿有什么提示朋友吗?

最好的, 路易斯.

我想你在某个地方有一个 Hazelcast 集群 运行,你只想从你的“界面”访问它。您不想管理集群,但您只关心 interface/cache.

中客户端的活跃度

使用 Hazelcast 客户端配置选项处理操作重试 and/or 连接失败。

参见Java client documentation (ver 5.1)即以下部分:

然后你可以使用类似下面的代码来初始化你的 clientInstance:

    public Boolean initCache() {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.getNetworkConfig()
            // TODO add address(es) of your cluster member(s)
            .addAddress("10.0.0.1:5701")
            // TODO decide if the client should talk to all cluster members (smart) or just one (unisocket)
            .setSmartRouting(false)
            // Retry operation when it's possible
            .setRedoOperation(true);

        ClientConnectionStrategyConfig connectionStrategyConfig = clientConfig.getConnectionStrategyConfig();
        connectionStrategyConfig
            .setAsyncStart(true)
            .setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
        ConnectionRetryConfig connectionRetryConfig = connectionStrategyConfig.getConnectionRetryConfig();
        connectionRetryConfig
            .setInitialBackoffMillis(1000)
            .setMaxBackoffMillis(60000)
            .setMultiplier(2)
            .setClusterConnectTimeoutMillis(50000)
            .setJitter(0.2);

        clientInstance = HazelcastClient.newHazelcastClient(clientConfig);
    }