Guava

Sentinel中的冷启动限流算法

泄露秘密 提交于 2021-01-17 03:44:02
关注 “Java艺术” 我们一起成长! -- 转载请声明来源和作者信息 -- 冷启动算法基于令牌桶算法实现。 令牌桶算法的原理是:按一定的速率往令牌桶中放入令牌,当接收到请求时,从令牌桶申请令牌,只有拿到令牌的请求才能通过。当令牌桶放满时,多余的令牌就会被丢弃;当令牌桶为空时,请求拿不到令牌就拒绝请求。 例如,想要使用令牌桶算法限制接口的最大QPS为200,那么就要每5毫秒就要生产一个令牌放入令牌桶,且生产令牌放入的速度不变。 冷启动算法 用于控制令牌桶的令牌生产速率,即控制每个令牌生产的时间间隔。 假设冷启动时长为10秒,初始状态为冷启动状态,限流阈值为200QPS,正常情况下生产令牌的速率应该为5毫秒/个,而在冷启动阶段,速率会从最小值上升至 5毫秒/个,最小速率与冷启动系数有关,与冷启动周期时长有关 。 Sentinel与Guava的实现不同,Sentinel可能是出于对性能的考虑,并不控制每个请求的通过时间间隔,只控制每秒钟能通过的请求数。 通过下面这张图来理解冷启动算法。 坐标轴: 横坐标storedPermits代表存储桶中的令牌数量; 纵坐标代表获取一个令牌需要的时间,即请求通过的时间间隔; stableInterval:稳定产生令牌的时间间隔,假设限流阈值QPS为200,stableInterval的值为5毫秒。 coldInterval

Guava并发之ListenableFuture使用介绍

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-14 00:05:28
1. 引子 ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强。我们知道Future表示一个异步计算任务,当任务完成时可以得到计算结果。如果我们希望一旦计算完成就拿到结果展示给用户或者做另外的计算,就必须使用另一个线程不断的查询计算状态。这样做,代码复杂,而且效率低下。使用ListenableFuture Guava帮我们检测Future是否完成了,如果完成就自动调用回调函数,这样可以减少并发程序的复杂度。 ListenableFuture是一个接口,它从jdk的Future接口继承,添加了void addListener(Runnable listener, Executor executor)方法。 我们看下如何使用ListenableFuture。首先需要定义ListenableFuture的实例。 ListeningExecutorService executorService = MoreExecutors . listeningDecorator ( Executors . newCachedThreadPool ( ) ) ; final ListenableFuture < Integer > listenableFuture = executorService . submit ( new Callable

Maven--jar包冲突原理与解决办法

假如想象 提交于 2021-01-05 08:05:20
Maven中jar包冲突是开发过程中比较常见而又令人头疼的问题,我们需要知道 jar包冲突的原理,才能更好的去解决jar包冲突的问题。本文将从jar包冲突的原理和解决两个方面阐述Maven中jar包冲突的解决办法。 一、Maven中jar包冲突产生原因 MAVEN项目运行中如果报如下错误: Caused by:java.lang.NoSuchMethodError Caused by: java.lang.ClassNotFoundException 十有八九是Maven jar包冲突造成的。那么jar包冲突是如何产生的? 首先我们需要了解jar包依赖的传递性。 1、依赖传递 当我们需要A的依赖的时候,就会在pom.xml中引入A的jar包;而引入的A的jar包中可能又依赖B的jar包,这样Maven在解析pom.xml的时候,会依次将A、B 的jar包全部都引入进来。 举个例子: 在Spring Boot应用中导入Hystrix和原生Guava的jar包: <!-- 原生Guava API --> < dependency > < groupId > com.google.guava </ groupId > < artifactId > guava </ artifactId > < version > 20.0 </ version > </ dependency > <!-

Can Guava’s TypeToken get the specific type of a generic field?

巧了我就是萌 提交于 2020-12-29 11:59:33
问题 I wrote a parser for a file format called ASN.1 that uses Guice’s TypeLiteral.getFieldType(Field) to convert generic fields into specific Java types so I can construct the correct type (similar to Jackson or GSON databinding). But since I already depend on Guava and it seems to have a new TypeLiteral replacement, I’d like to use TypeToken instead. According to the Guave TypeToken documentation: TypeToken is similar to Guice's TypeLiteral class, but with one important difference: it supports

Can Guava’s TypeToken get the specific type of a generic field?

落爺英雄遲暮 提交于 2020-12-29 11:59:29
问题 I wrote a parser for a file format called ASN.1 that uses Guice’s TypeLiteral.getFieldType(Field) to convert generic fields into specific Java types so I can construct the correct type (similar to Jackson or GSON databinding). But since I already depend on Guava and it seems to have a new TypeLiteral replacement, I’d like to use TypeToken instead. According to the Guave TypeToken documentation: TypeToken is similar to Guice's TypeLiteral class, but with one important difference: it supports

implementing a lazy Supplier in java

夙愿已清 提交于 2020-12-29 09:06:20
问题 What is the right paradigm or utility class (can't seem to find a preexisting class) to implement a lazy supplier in Java? I want to have something that handles the compute-once/cache-later behavior and allows me to specify the computation behavior independently. I know this probably has an error but it has the right semantics: abstract public class LazySupplier<T> implements Supplier<T> { private volatile T t; final private Object lock = new Object(); final public T get() { if (t == null) {

implementing a lazy Supplier in java

送分小仙女□ 提交于 2020-12-29 09:04:08
问题 What is the right paradigm or utility class (can't seem to find a preexisting class) to implement a lazy supplier in Java? I want to have something that handles the compute-once/cache-later behavior and allows me to specify the computation behavior independently. I know this probably has an error but it has the right semantics: abstract public class LazySupplier<T> implements Supplier<T> { private volatile T t; final private Object lock = new Object(); final public T get() { if (t == null) {

How to compare two Collections for “equivalence” based on fields from different Java classes?

时光怂恿深爱的人放手 提交于 2020-12-29 02:59:12
问题 Given any two classes, e.g. ClassA and ClassB below: class ClassA { private int intA; private String strA; private boolean boolA; // Constructor public ClassA (int intA, String strA, boolean boolA) { this.intA = intA; this.strA = strA; this.boolA = boolA; } // Getters and setters etc. below... } class ClassB { private int intB; private String strB; private boolean boolB; // Constructor public ClassB (int intB, String strB, boolean boolB) { this.intB = intB; this.strB = strB; this.boolB =

How to compare two Collections for “equivalence” based on fields from different Java classes?

我怕爱的太早我们不能终老 提交于 2020-12-29 02:58:58
问题 Given any two classes, e.g. ClassA and ClassB below: class ClassA { private int intA; private String strA; private boolean boolA; // Constructor public ClassA (int intA, String strA, boolean boolA) { this.intA = intA; this.strA = strA; this.boolA = boolA; } // Getters and setters etc. below... } class ClassB { private int intB; private String strB; private boolean boolB; // Constructor public ClassB (int intB, String strB, boolean boolB) { this.intB = intB; this.strB = strB; this.boolB =

Binding a guava supplier using guice

一笑奈何 提交于 2020-12-26 11:04:50
问题 I want do a binding like this, bind(Supplier<TestClass>).toProvider(Provider<Supplier<TestClass>>).in(Singleton.class); The provider is returned by an external function so, inside the toProvider() , I call that function and it returns Provider <Supplier<TestClass>> . The supplier is from guava, the reason to do this kind of thing is, there is a file associated with the TestClass and I need to read that file and assign those values to the respective fields of TestClass. And that file change at