热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

id本地缓存策略

篇首语:本文由编程笔记#小编为大家整理,主要介绍了id本地缓存策略相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了id本地缓存策略相关的知识,希望对你有一定的参考价值。






思路是每次去生成id都要去获取比较慢,可以在启动的时候先获取部分id到缓存中,然后直接在缓存里获取id,当id数量下降到一定程度的时候再去补充id到缓存中


上配置类

package com.felix.spring_cloud_one.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(
prefix = "idsetting"
)
public class IdGenerateProperties
private IdGenerateProperties.IdentTooker identTooker = new IdGenerateProperties.IdentTooker();
public void setIdentTooker(IdentTooker identTooker)
this.identTooker = identTooker;

public IdentTooker getIdentTooker()
return identTooker;

public static class IdentTooker
private String enable = "false";
private Integer limitAllowExistNumber = 30;
private Integer maxKeepNumber = 200;
private Integer perRequestStep = 100;
public IdentTooker()

public String getEnable()
return this.enable;

public void setEnable(String enable)
this.enable = enable;

public Integer getLimitAllowExistNumber()
return this.limitAllowExistNumber;

public void setLimitAllowExistNumber(Integer limitAllowExistNumber)
this.limitAllowExistNumber = limitAllowExistNumber;

public Integer getMaxKeepNumber()
return this.maxKeepNumber;

public void setMaxKeepNumber(Integer maxKeepNumber)
this.maxKeepNumber = maxKeepNumber;

public Integer getPerRequestStep()
return this.perRequestStep;

public void setPerRequestStep(Integer perRequestStep)
this.perRequestStep = perRequestStep;




id获取及补充类

package com.felix.spring_cloud_one.util;
import com.felix.spring_cloud_one.config.ClientResult;
import com.felix.spring_cloud_one.config.IdGenerateProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Configuration
public class IdTook
private static final Logger log = LoggerFactory.getLogger(IdTook.class);
private static IdGenerateProperties idGenerateProperties;
private static IdentGenerateClient identGenerateClient;
private static final LinkedBlockingQueue<Long> IDENT_POOL &#61; new LinkedBlockingQueue<>();
//private static final LinkedBlockingQueue IDENT_POOL_LISTEN &#61; new LinkedBlockingQueue<>();
private static final ExecutorService SINGLE_THREAD_EXECUTOR;
private static IdTook.ListenRunnable listenRunnable;
static
SINGLE_THREAD_EXECUTOR &#61; new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), (r) ->
return new Thread(r, "id-took-thread");
, new ThreadPoolExecutor.DiscardOldestPolicy());
listenRunnable &#61; new IdTook.ListenRunnable();

public IdTook()

&#64;Autowired
public void setIdentFeignClient(IdentGenerateClient identGenerateClient)
//从id生成服务获取id
IdTook.identGenerateClient &#61; identGenerateClient;

&#64;Autowired
public void setDrawerProperties(IdGenerateProperties idGenerateProperties)
IdTook.idGenerateProperties &#61; idGenerateProperties;

&#64;PostConstruct
public void initIdentPool()
SINGLE_THREAD_EXECUTOR.execute(listenRunnable);

public static Long take()
log.info("当前取号池剩余:个号码", IDENT_POOL.size());
try
if (0 &#61;&#61; IDENT_POOL.size())
log.warn("取号线程异常中断&#xff0c;当前系统直接从生成器获取id");
ClientResult<Long> clientResult &#61; identGenerateClient.make();
if (1 &#61;&#61; clientResult.getCode())
return (Long)clientResult.getData();


Long id &#61; (Long)IDENT_POOL.poll(10L, TimeUnit.SECONDS);
if (IDENT_POOL.size() < idGenerateProperties.getIdentTooker().getLimitAllowExistNumber())
synchronized(listenRunnable)
listenRunnable.notify();


return id;
catch (InterruptedException var4)
log.error(var4.getMessage(), var4);
throw new RuntimeException("取号超时,请确保已启用id取号器功能");


private static void offer(Long... ids)
Long[] var1 &#61; ids;
int var2 &#61; ids.length;
for(int var3 &#61; 0; var3 < var2; &#43;&#43;var3)
Long id &#61; var1[var3];
try
IDENT_POOL.offer(id, 1L, TimeUnit.SECONDS);
catch (InterruptedException var6)
log.warn("插入新号码失败");



private static class ListenRunnable implements Runnable
private ListenRunnable()

public void run()
while(true)
synchronized(IdTook.listenRunnable)
if (IdTook.IDENT_POOL.size() > IdTook.idGenerateProperties.getIdentTooker().getLimitAllowExistNumber())
try
IdTook.listenRunnable.wait();
catch (InterruptedException var8)
var8.printStackTrace();

else
IdTook.log.info("取号池号码过少&#xff0c;获取新号码");
Integer step &#61; IdTook.idGenerateProperties.getIdentTooker().getPerRequestStep();
step &#61; step > 100 ? 100 : step;
int count &#61; (int)Math.ceil((double)(IdTook.idGenerateProperties.getIdentTooker().getMaxKeepNumber() / step));
count &#61; 0 &#61;&#61; count ? 1 : count;
for(int i &#61; 0; i < count; &#43;&#43;i)
ClientResult<Long []> clientResult &#61; IdTook.identGenerateClient.make(step);
if (1 &#61;&#61; clientResult.getCode())
Long[] data &#61; clientResult.getData();
IdTook.offer(data);








yml配置文件

idsetting:
identTooker:
limitAllowExistNumber: 5
maxKeepNumber: 5
perRequestStep: 5






推荐阅读
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 深入解析 Apache Shiro 安全框架架构
    本文详细介绍了 Apache Shiro,一个强大且灵活的开源安全框架。Shiro 专注于简化身份验证、授权、会话管理和加密等复杂的安全操作,使开发者能够更轻松地保护应用程序。其核心目标是提供易于使用和理解的API,同时确保高度的安全性和灵活性。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了如何使用Spring Boot进行高效开发,涵盖了配置、实例化容器以及核心注解的使用方法。 ... [详细]
  • 本文详细介绍了如何在 Spring Boot 应用中通过 @PropertySource 注解读取非默认配置文件,包括配置文件的创建、映射类的设计以及确保 Spring 容器能够正确加载这些配置的方法。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • andr ... [详细]
  • MySQL 数据库迁移指南:从本地到远程及磁盘间迁移
    本文详细介绍了如何在不同场景下进行 MySQL 数据库的迁移,包括从一个硬盘迁移到另一个硬盘、从一台计算机迁移到另一台计算机,以及解决迁移过程中可能遇到的问题。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 本文作者分享了在阿里巴巴获得实习offer的经历,包括五轮面试的详细内容和经验总结。其中四轮为技术面试,一轮为HR面试,涵盖了大量的Java技术和项目实践经验。 ... [详细]
author-avatar
263企业邮箱温州授权合作伙伴
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有