原创

Spring Boot集成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;

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

准备三个启动类

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);
    }
}
这个代码中最关键的参数是需要设置之前定义的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;

@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里面,控制台输出
Finding employee information with id 6 ...
  • 浏览器输入http://localhost:8089/employee/6,可以直接获取缓存数据,控制台不会打印日志出来
  • 浏览器输入http://localhost:8090/employee/6,可以直接获取缓存数据,控制台不会打印日志出来

4.引用

正文到此结束
Loading...