一、简介
官方文档:https://docs.hazelcast.com/hazelcast/latest/clients/java Github地址:https://github.com/hazelcast/hazelcast management-center:https://hazelcast.com/open-source-projects/downloads/archives/#management-center
二、安装
2.1 使用Docker形式部署
使用Docker形式进行部署:
docker pull hazelcast/management-center
docker run -d -p 8200:8080 hazelcast/management-center:5.1.3
docker pull hazelcast/hazelcast:5.1.3
docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=192.168.153.131:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -p 5701:5701 hazelcast/hazelcast:5.1.3
docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=192.168.153.129:5701 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://192.168.153.129:8200/hazelcast-mancenter" -p 5701:5701 hazelcast/hazelcast:5.1.3
docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=192.168.153.129:5702 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://192.168.153.129:8200/hazelcast-mancenter" -p 5702:5701 hazelcast/hazelcast:5.1.3
docker run -d -e JAVA_OPTS="-Dhazelcast.local.publicAddress=192.168.153.129:5703 -Dhazelcast.rest.enabled=true -Xms128M -Xmx256M" -e MANCENTER_URL="http://192.168.153.129:8200/hazelcast-mancenter" -p 5703:5701 hazelcast/hazelcast:5.1.3
2.2 客户端命令行
hz-cli console
ns my-distributed-map
m.put "1" "John"
m.put "2" "Mary"
m.put "3" "Jane"
m.keys
m.values
m.get "1"
m.get "2"
m.get "3"
2.3 Web管理页面
启动后打开浏览器访问localhost:8200端口,连接集群: 单节点:
多节点:
三、与SpringBoot项目整合
3.1 pom依赖
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<version>5.1.3</version>
</dependency>
3.2 配置类
这种方式是连接远程hazelcast集群,也可以直接当做本地缓存来使用(不需要单独部署hazelcast,直接引入jar包)
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientNetworkConfig;
import com.hazelcast.core.HazelcastInstance;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "hazelcast")
@Data
@Configuration
public class HazelcastConfiguration {
public String address;
@Bean
public ClientConfig hazelCastConfig() {
ClientConfig clientConfig = new ClientConfig();
ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
networkConfig.addAddress("192.168.153.131:5701")
.setSmartRouting(true)
.addOutboundPortDefinition("34700-34710")
.setRedoOperation(true)
.setConnectionTimeout(5000);
return clientConfig;
}
@Bean("hazelcastClient")
public HazelcastInstance hazelcastInstance(ClientConfig config) {
HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient(config);
return hazelcastInstance;
}
}
3.3 使用方式
@SpringBootTest
@RunWith(SpringRunner.class)
public class HazelcastTest {
@Autowired
HazelcastInstance hazelcastClient;
@Test
public void test(){
Map<String, Object> cache = hazelcastClient.getMap("instruments");
cache.put("date", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
System.out.println(cache.get("date"));
}
}
|