基于Guava RateLimiter实现限流
Guava RateLimiter 客户端限流 (单机版本) <dependency> <groupId> com.google.guava </groupId> <artifactId> guava </artifactId> <version> 18.0 </version> </dependency> import com.google.common.util.concurrent.RateLimiter ; import lombok.extern.slf4j. Slf4j ; import org.springframework.web.bind.annotation. GetMapping ; import org.springframework.web.bind.annotation. RestController ; import java.util.concurrent.TimeUnit ; /** * RateLimiter 只能存在单机项目 */ @RestController @Slf4j public class RateLimiterController { //每一秒2个令牌限流 RateLimiter limiter = RateLimiter. create ( 2.0 ) ; /** * 非阻塞限流 * * @param count 每秒消耗令牌