Spring集成hazelcast实现分布式缓存

1.Hazelcast介绍

Hazelcast是Hazelcast公司开源的一款分布式内存数据库产品,提供弹性可扩展、高性能的分布式内存计算。并通过提供诸如Map,Queue,ExecutorService,Lock和JCache等Java的许多开发人员友好的分布式实现。

Hazelcast优势

  • Hazelcast提供开源版本。

  • Hazelcast无需安装,只是个极小jar包。

  • Hazelcast提供开箱即用的分布式数据结构,如Map,Queue,MultiMap,Topic,Lock和Executor。

  • Hazelcast集群非传统主从关系,避免了单点故障;集群中所有成员共同分担集群功能。

  • Hazelcast集群提供弹性扩展,新成员在内存不足或负载过高时能动态加入集群。

  • Hazelcast集群中成员分担数据缓存的同时互相冗余备份其他成员数据,防止某成员离线后数据丢失。

  • Hazelcast提供SPI接口支持用户自定义分布式数据结构。

Hazelcast适用场景

  • 频繁读写数据

  • 需要高可用分布式缓存

  • 内存行NoSql存储

  • 分布式环境中弹性扩展


2.代码工程

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">    <parent>        <artifactId>springboot-demo</artifactId>        <groupId>com.et</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>
<artifactId>hazelcast</artifactId>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast-all</artifactId> <version>4.0.2</version> </dependency> </dependencies>

</project>

hazelcast.xml

<hazelcast        xsi:schemaLocation="http://www.hazelcast.com/schema/config   http://www.hazelcast.com/schema/config/hazelcast-config-3.12.12.xsd"        xmlns="http://www.hazelcast.com/schema/config"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    <instance-name>XML_Hazelcast_Instance</instance-name>    <network>        <join>            <multicast enabled="false">            </multicast>            <tcp-ip enabled="true">                <member>10.11.68.77</member>            </tcp-ip>        </join>    </network></hazelcast>

application.yaml

    
server:  port: 8090

entity

    
package com.et.hazelcast.entity;import java.io.Serializable;public class Employee implements Serializable{   private static final long serialVersionUID = 1L;   private int empId;   private String name;   private String department;   public Employee(Integer id, String name, String department) {      super();      this.empId = id;      this.name = name;      this.department = department;   }   public int getEmpId() {      return empId;   }   public void setEmpId(int empId) {      this.empId = empId;   }   public String getName() {      return name;   }   public void setName(String name) {      this.name = name;   }   public String getDepartment() {      return department;   }   public void setDepartment(String department) {      this.department = department;   }   @Override   public String toString() {      return "Employee [empId=" + empId + ", name=" + name + ", department=" + department + "]";   }}

controller

    
package com.et.hazelcast.controller;
import com.et.hazelcast.entity.Employee;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;import java.util.Map;
@Controllerpublic class HelloWorldController { @RequestMapping("/hello") @ResponseBody public Map<String, Object> showHelloWorld(){ Map<String, Object> map = new HashMap<>(); map.put("msg", "HelloWorld"); return map; } @Cacheable(value = "employee") @GetMapping("employee/{id}") @ResponseBody public Employee getSubscriber(@PathVariable("id") int id) throws InterruptedException { System.out.println("Finding employee information with id " + id + " ..."); return new Employee(id, "John Smith", "CS"); }}

准备三个启动类

    
package com.et.hazelcast;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;
@EnableCaching@SpringBootApplicationpublic class HazelcastNode1Starter {
public static void main(String[] args) { SpringApplication.run(HazelcastNode1Starter.class, args); }
}
    
package com.et.hazelcast;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@EnableCaching@SpringBootApplicationpublic class HazelcastNode2Starter { public static void main(String[] args) { SpringApplication.run(HazelcastNode2Starter.class, args); }}
    
package com.et.hazelcast;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@EnableCaching@SpringBootApplicationpublic class HazelcastNode3Starter { public static void main(String[] args) { SpringApplication.run(HazelcastNode3Starter.class, args); }}

java代码客户端

这个代码中最关键的参数是需要设置之前定义的cluster-name “hazelcast-cluster”。 这样就实现了对hazelcast集群中map的调用。上述过程中,如果关闭任意一个hazelcast节点,上述缓存中的数据都可用。很好的实现了分布式。

    
package com.et.hazelcast;
import com.hazelcast.client.HazelcastClient;import com.hazelcast.client.config.ClientConfig;import com.hazelcast.core.HazelcastInstance;import lombok.extern.slf4j.Slf4j;
import java.util.Map;
@Slf4jpublic class HazelcastGetStartClient { public static void main(String[] args) { ClientConfig clientConfig = new ClientConfig(); clientConfig.setClusterName("hazelcast-cluster"); HazelcastInstance instance = HazelcastClient.newHazelcastClient(clientConfig); Map<Integer, String> clusterMap = instance.getMap("map"); }
}

代码仓库

  • https://github.com/Harries/springboot-demo

3.测试

  • 端口修改8088,启动HazelcastNode1Starter

  • 端口修改8089,启动HazelcastNode2Starter

  • 端口修改8090,启动HazelcastNode3Starter

    
Members {size:3, ver:5} [ Member [10.11.68.77]:5701 - 2faf3b2d-76f3-493c-be48-d19d25aeb581 this Member [10.11.68.77]:5702 - 63caca7f-f8ba-4b0a-989a-6e86a199fb72 Member [10.11.68.77]:5703 - 3e9fa03c-72f4-4866-8904-73b908c4005d]

浏览器输入http://localhost:8088/employee/6,存入数据到hazelcast里面,控制台输出

    
Finding employee information with id 6 ...

浏览器输入http://localhost:8089/employee/6,可以直接获取缓存数据,控制台不会打印日志出来 

浏览器输入http://localhost:8090/employee/6,可以直接获取缓存数据,控制台不会打印日志出来

4.引用

  • http://element-ui.cn/news/show-552564.html

  • http://www.liuhaihua.cn/archives/710310.html


相关推荐

  • 今日代码大赏 | 快速排序
  • CPU 被干爆了。。还找不到原因?
  • Redis 八种常用数据类型常用命令和应用场景
  • 数学问题难解?新研究提出MathScale方法,让AI更懂数学推理
  • 今日arXiv最热大模型论文:清华把大模型用于城市规划,回龙观和大红门地区成研究对象
  • GPT-4.5 疑似面世,OpenAI 官网网页被索引,最快明天发布?
  • 万维网之父罕见发声:某家巨头公司将被分拆、人无需上网,下一代互联网将由AI代劳,数据不再归平台所控制
  • PPT 下载|【数据湖峰会】22份资料
  • 大模型在携程的探索与实践
  • AI风暴来袭:2024年数据平台的演进、挑战与机遇
  • AI图片橡皮擦来了,清华&阿里合作推出「概念半透膜」模型,还能改头换面
  • 专为训练Llama 3,Meta 4.9万张H100集群细节公布
  • GPT-4.5 Turbo意外曝光,官方网页被扒出,网传明天就上线
  • 首个AI软件工程师上线!已通过公司面试抢程序员饭碗,华人创始团队手握10块IOI金牌
  • 19年创业失败,23年利用AI在代写行业杀出一条全新业务线,从0到100w全程分享。
  • 350亿参数、开放权重,Transformer作者创业后推出新大模型
  • 2024苹果学者名单公布,华人占一半!宾大博士生曾和Jim Fan搞出英伟达最火机器人
  • 【Python】详解Python文件: .py、.ipynb、.pyi、.pyc、​.pyd !
  • 35岁青基挂了!“997”的学术圈太卷了
  • Llama-3背后基础训练设施全揭秘:两个24KGPU集群,共4.9万个H100