• 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

     
     
    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>springboot-demoartifactId>
    7. <groupId>com.etgroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>hazelcastartifactId>
    12. <properties>
    13. <maven.compiler.source>8maven.compiler.source>
    14. <maven.compiler.target>8maven.compiler.target>
    15. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.bootgroupId>
    19. <artifactId>spring-boot-starter-webartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframework.bootgroupId>
    23. <artifactId>spring-boot-autoconfigureartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.bootgroupId>
    27. <artifactId>spring-boot-starter-testartifactId>
    28. <scope>testscope>
    29. dependency>
    30. <dependency>
    31. <groupId>org.springframework.bootgroupId>
    32. <artifactId>spring-boot-starter-cacheartifactId>
    33. dependency>
    34. <dependency>
    35. <groupId>com.hazelcastgroupId>
    36. <artifactId>hazelcast-allartifactId>
    37. <version>4.0.2version>
    38. dependency>
    39. dependencies>
    40. project>

    hazelcast.xml

    1. <hazelcast
    2. xsi:schemaLocation="http://www.hazelcast.com/schema/config
    3. http://www.hazelcast.com/schema/config/hazelcast-config-3.12.12.xsd"
    4. xmlns="http://www.hazelcast.com/schema/config"
    5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    6. <instance-name>XML_Hazelcast_Instanceinstance-name>
    7. <network>
    8. <join>
    9. <multicast enabled="false">
    10. multicast>
    11. <tcp-ip enabled="true">
    12. <member>10.11.68.77member>
    13. tcp-ip>
    14. join>
    15. network>
    16. hazelcast>

    application.yaml

     
     
    1. server:
    2. port: 8090

    entity

     
     
    1. package com.et.hazelcast.entity;
    2. import java.io.Serializable;
    3. public class Employee implements Serializable{
    4. private static final long serialVersionUID = 1L;
    5. private int empId;
    6. private String name;
    7. private String department;
    8. public Employee(Integer id, String name, String department) {
    9. super();
    10. this.empId = id;
    11. this.name = name;
    12. this.department = department;
    13. }
    14. public int getEmpId() {
    15. return empId;
    16. }
    17. public void setEmpId(int empId) {
    18. this.empId = empId;
    19. }
    20. public String getName() {
    21. return name;
    22. }
    23. public void setName(String name) {
    24. this.name = name;
    25. }
    26. public String getDepartment() {
    27. return department;
    28. }
    29. public void setDepartment(String department) {
    30. this.department = department;
    31. }
    32. @Override
    33. public String toString() {
    34. return "Employee [empId=" + empId + ", name=" + name + ", department=" + department + "]";
    35. }
    36. }

    controller

     
     
    1. package com.et.hazelcast.controller;
    2. import com.et.hazelcast.entity.Employee;
    3. import org.springframework.cache.annotation.Cacheable;
    4. import org.springframework.stereotype.Controller;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.PathVariable;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.ResponseBody;
    9. import java.util.HashMap;
    10. import java.util.Map;
    11. @Controller
    12. public class HelloWorldController {
    13. @RequestMapping("/hello")
    14. @ResponseBody
    15. public Map showHelloWorld(){
    16. Map map = new HashMap<>();
    17. map.put("msg", "HelloWorld");
    18. return map;
    19. }
    20. @Cacheable(value = "employee")
    21. @GetMapping("employee/{id}")
    22. @ResponseBody
    23. public Employee getSubscriber(@PathVariable("id") int id) throws
    24. InterruptedException {
    25. System.out.println("Finding employee information with id " + id + " ...");
    26. return new Employee(id, "John Smith", "CS");
    27. }
    28. }

    准备三个启动类

     
     
    1. package com.et.hazelcast;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cache.annotation.EnableCaching;
    5. @EnableCaching
    6. @SpringBootApplication
    7. public class HazelcastNode1Starter {
    8. public static void main(String[] args) {
    9. SpringApplication.run(HazelcastNode1Starter.class, args);
    10. }
    11. }
     
     
    1. package com.et.hazelcast;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cache.annotation.EnableCaching;
    5. @EnableCaching
    6. @SpringBootApplication
    7. public class HazelcastNode2Starter {
    8. public static void main(String[] args) {
    9. SpringApplication.run(HazelcastNode2Starter.class, args);
    10. }
    11. }
     
     
    1. package com.et.hazelcast;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cache.annotation.EnableCaching;
    5. @EnableCaching
    6. @SpringBootApplication
    7. public class HazelcastNode3Starter {
    8. public static void main(String[] args) {
    9. SpringApplication.run(HazelcastNode3Starter.class, args);
    10. }
    11. }

    java代码客户端

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

     
     
    1. package com.et.hazelcast;
    2. import com.hazelcast.client.HazelcastClient;
    3. import com.hazelcast.client.config.ClientConfig;
    4. import com.hazelcast.core.HazelcastInstance;
    5. import lombok.extern.slf4j.Slf4j;
    6. import java.util.Map;
    7. @Slf4j
    8. public class HazelcastGetStartClient {
    9. public static void main(String[] args) {
    10. ClientConfig clientConfig = new ClientConfig();
    11. clientConfig.setClusterName("hazelcast-cluster");
    12. HazelcastInstance instance = HazelcastClient.newHazelcastClient(clientConfig);
    13. Map clusterMap = instance.getMap("map");
    14. }
    15. }

    代码仓库

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

    3.测试

    • 端口修改8088,启动HazelcastNode1Starter

    • 端口修改8089,启动HazelcastNode2Starter

    • 端口修改8090,启动HazelcastNode3Starter

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

    浏览器输入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

  • 相关阅读:
    2.5 射频辐射电磁场抗扰度试验【电磁兼容EMC原理、设计与故障排除】
    C++ socket编程(TCP)
    cdh6.x 集成spark-sql
    【微服务~原始真解】Spring Cloud —— 什么是负载均衡?
    1455. 检查单词是否为句中其他单词的前缀
    spring cloud实践
    jetson xiaver NX 安装tensorflow object detection api 遇到的tensorflow-addons 不能安装问题
    JS学习762~780(注册事件+删除事件+DOM事件流+事件对象+阻止事件冒泡+事件委托鼠标事件+键盘事件)
    [React] 性能优化相关 (二)
    CentOS 7 安装和配置java环境
  • 原文地址:https://blog.csdn.net/dot_life/article/details/136696748