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 复制代码
<?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

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

yaml 复制代码
server:
  port: 8090

controller

kotlin 复制代码
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;

@Controller
public 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");
    }
}

准备三个启动类

typescript 复制代码
package com.et.hazelcast;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@EnableCaching
@SpringBootApplication
public 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
@SpringBootApplication
public 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
@SpringBootApplication
public class HazelcastNode3Starter {
    public static void main(String[] args) {
        SpringApplication.run(HazelcastNode3Starter.class, args);
    }
}

java代码客户端

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

java 复制代码
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;

@Slf4j
public 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");
    }

}

代码仓库

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里面,控制台输出

python 复制代码
Finding employee information with id 6 ...

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

4.引用

相关推荐
KiddoStone7 分钟前
多实例schedule job同步数据流的数据一致性设计和实现方案
java
岁忧29 分钟前
(LeetCode 每日一题) 1865. 找出和为指定值的下标对 (哈希表)
java·c++·算法·leetcode·go·散列表
YuTaoShao32 分钟前
【LeetCode 热题 100】240. 搜索二维矩阵 II——排除法
java·算法·leetcode
考虑考虑1 小时前
JDK9中的dropWhile
java·后端·java ee
想躺平的咸鱼干2 小时前
Volatile解决指令重排和单例模式
java·开发语言·单例模式·线程·并发编程
hqxstudying2 小时前
java依赖注入方法
java·spring·log4j·ioc·依赖
·云扬·2 小时前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
Bug退退退1233 小时前
RabbitMQ 高级特性之重试机制
java·分布式·spring·rabbitmq
小皮侠3 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github
Zz_waiting.3 小时前
Javaweb - 10.4 ServletConfig 和 ServletContext
java·开发语言·前端·servlet·servletconfig·servletcontext·域对象