热门标签 | 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






推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • Java中包装类的设计原因以及操作方法
    本文主要介绍了Java中设计包装类的原因以及操作方法。在Java中,除了对象类型,还有八大基本类型,为了将基本类型转换成对象,Java引入了包装类。文章通过介绍包装类的定义和实现,解答了为什么需要包装类的问题,并提供了简单易用的操作方法。通过本文的学习,读者可以更好地理解和应用Java中的包装类。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
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社区 版权所有