栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA 本地缓存使用-分布式时要使用路由

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 本地缓存使用-分布式时要使用路由

在redis盛行的今天很少有人还会选择本地缓存,但是在一次面试中有人问了,我就学习一下,为防止以后忘记,写篇博客记录一下。

使用本地缓存,需要使用一个路由策略,保证同一个单号的数据永远路由到同一台机器,这个可以使用Hash算法生成一个hashcode,然后和集群数量做取模运算。 直接记录代码,这个代码是借鉴的别人的。

import org.apache.commons.lang3.StringUtils;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;

public class ConsistentHashWithVirtualNode {
    
    private static SortedMap virtualNodeMap = new TreeMap<>();
    //单机虚拟节点
    private static final int VIRTUAL_NODE_NUM = 5;
    //服务器配置信息(可配置)
    private static String[] servers = {"192.168.56.120:6379",
            "192.168.56.121:6379",
            "192.168.56.122:6379",
            "192.168.56.123:6379",
            "192.168.56.124:6379"};

    
    static{
        for(int i=0; i< servers.length; i++){
            for(int j=0; j subMap = virtualNodeMap.tailMap(hash);
        if(subMap.isEmpty()){
            int index = virtualNodeMap.firstKey();
            System.out.printf("%s被路由到虚拟节点[%s]真实节点[%s]n", key, virtualNodeMap.get(index),
                    virtualNodeMap.get(index).substring(0, virtualNodeMap.get(index).indexOf("&")));
            return virtualNodeMap.get(index).substring(0, virtualNodeMap.get(index).indexOf("&"));
        }else{
            int index = subMap.firstKey();
            System.out.printf("%s被路由到虚拟节点[%s]真实节点[%s]n", key, virtualNodeMap.get(index),
                    virtualNodeMap.get(index).substring(0, virtualNodeMap.get(index).indexOf("&")));
            return virtualNodeMap.get(index).substring(0, virtualNodeMap.get(index).indexOf("&"));
        }
    }

    
    public static void main(String[] args) {
//        for(int i=0; i<20; i++){
//            String str = UUID.randomUUID().toString();
//            getServer(str);
//        }


        String user = new String("Java4ye");
        ReferenceQueue userReferenceQueue = new ReferenceQueue<>();
// 创建User对象的虚引用
        PhantomReference phantomReference = new PhantomReference<>(user, userReferenceQueue);
        System.out.println(userReferenceQueue.poll().get());
// 去掉强引用
        user = null;
        System.out.println(phantomReference.get());

        System.gc();
        System.out.println("GC: " + phantomReference.get());
        Reference reference = null;
        try {
            reference = userReferenceQueue.remove(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (reference != null) {
            System.out.println("对象User被回收了:");
        }
    }

}

本地缓存框架有很多 如ehcache、GuavaCache、Caffeine等,因为这些框架使用都比较简单,网上有很多例子,我就不写了

Java高性能本地缓存框架Caffeine_C.-CSDN博客_java本地缓存框架

中介绍了Caffeine 的使用

java中使用Ehcache缓存数据 - shuaiflying - 博客园

中介绍了ehcache 的使用

Java Guava Cache 使用_编程战五渣-CSDN博客

中介绍了GuavaCache的使用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号