旧东西 HazelcastInstance hzClient = HazelcastClient.newHazelcastClient(); 的新 API 是什么?

What are new API for old thing HazelcastInstance hzClient = HazelcastClient.newHazelcastClient();?

我按照 https://www.youtube.com/watch?v=AjpSdbdvIHw

上的教程进行操作

我的代码 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.anil.hz</groupId>
    <artifactId>H</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast</artifactId>
            <version>3.11</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

文件Server.java

package com.anil.server;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class Server {

    public static void main(String[] args) {
        HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance();
        IMap<Long, String> map = hzInstance.getMap("testMap");
        map.put(1L, "one");
        map.put(2L, "two");
    }
}

文件Student.java

package com.anil.server;

public class Student {

    private Long id;
    private String sName;
    private String city;

    public Student(Long id, String sName, String city) {
        this.id = id;
        this.sName = sName;
        this.city = city;
    }

}

文件Server.java

package com.anil.server;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class Server {

    public static void main(String[] args) {
        HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance();
        IMap<Long, String> map = hzInstance.getMap("testMap");
        map.put(1L, "one");
        map.put(2L, "two");
    }
}

文件HZConfig.java

package com.anil.server;

import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;

public class HZConfig {

    public static Config getConfig(){
        Config config = new Config();
        NetworkConfig network = config.getNetworkConfig();
        network.setPort(5710).setPortCount(20);
        network.setPortAutoIncrement(true);
        JoinConfig join = network.getJoin();
        join.getMulticastConfig().setEnabled(false);
        join.getTcpIpConfig().addMember("localhost").setEnabled(true);
        config.getManagementCenterConfig().setEnabled(true);
        config.getManagementCenterConfig().setUrl("http://localhost:8081/mancenter");
        return config;
    }

}

文件Client.java

package com.anil.server;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class Client {

    public static void main(String[] args) {
        HazelcastInstance hzClient = null;
        try {
            hzClient = HazelcastClient.newHazelcastClient();
            IMap<Long, String> testMap = hzClient.getMap("testMap");
            System.out.println(testMap.get(1L));
        } finally {
            hzClient.shutdown();
        }
    }

}

我在

旧东西HazelcastInstance hzClient = HazelcastClient.newHazelcastClient();有什么新东西API?

您正在使用以下依赖项

    <dependency>
        <groupId>com.hazelcast</groupId>
        <artifactId>hazelcast</artifactId>
        <version>3.11</version>
    </dependency>

这不包含客户端。本教程将该依赖项更改为 3:45 处的 hazelcast-all,其中包含客户端。您还可以使用 hazelcast-client artifactId.

单独包含客户端

但是 3.11 是一个非常旧的版本,不再受支持,您应该使用最新版本 - 当前的 5.1。在此版本中,不再有 hazelcast-all jar,客户端包含在主 jar 中。请注意 3.x 和 4.x/5.x 之间有一些重大变化,但主要只是包名称。

根据 的回答,我添加了更多细节

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.anil.hz</groupId>
    <artifactId>H</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast</artifactId>
            <version>5.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

文件Client.java

package com.anil.server;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;

public class Client {

    public static void main(String[] args) {
        HazelcastInstance hzClient = null;
        try {
            hzClient = HazelcastClient.newHazelcastClient();
            IMap<Long, String> testMap = hzClient.getMap("testMap");
            System.out.println(testMap.get(1L));
        } finally {
            hzClient.shutdown();
        }
    }

}

文件Server.java

package com.anil.server;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;

public class Server {

    public static void main(String[] args) {
        HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance();
        IMap<Long, String> map = hzInstance.getMap("testMap");
        map.put(1L, "one");
        map.put(2L, "two");
    }
}

文件HZConfig.java

package com.anil.server;

import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;

public class HZConfig {

    public static Config getConfig(){
        Config config = new Config();
        NetworkConfig network = config.getNetworkConfig();
        network.setPort(5710).setPortCount(20);
        network.setPortAutoIncrement(true);
        JoinConfig join = network.getJoin();
        join.getMulticastConfig().setEnabled(false);
        join.getTcpIpConfig().addMember("localhost").setEnabled(true);
        config.getManagementCenterConfig().setConsoleEnabled(true);
        config.getManagementCenterConfig().setDataAccessEnabled(true);
        config.getManagementCenterConfig().setScriptingEnabled(true);
                       
        // config.getManagementCenterConfig().setUrl("http://localhost:8081/mancenter"); // ?
        return config;
    }

}