Giter VIP home page Giter VIP logo

easy-sentinel's Introduction

easy

EasySentinel


996.icuLICENSE 是一款专门为SpringBoot项目设计的限流组件,利用Redis+lua从而来实现高性能和分布式的能力。使用比较简单。通过半嵌入式的开发即可使用分布式注解。引用使用Redis作为注册中心,所以需要添加redis依赖

SpringBoot2版本之后官方强烈建议以此来替换spring-boot-starter-redis

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

提供报表接口【因为个人没有太多时间投入,所以搁置,如果有人感兴趣可以联系我一起开发】

为了有更好的用户体验,控制台的开发已经提上日常。当前项目只有本人维护,欢迎前端或者后端的同学一起来参与

开始引入依赖

<dependency>
 <groupId>com.github.lxchinesszz</groupId>
    <artifactId>easy-sentinel</artifactId>
    <version>1.0.1-SNAPSHOT</version>
</dependency>

使用案例

只用将需要限制的接口加上@EasySentinel即可实现限流能力

通过Qps设置当前接口的qps,fallback指定限流后的备用方法 。

@RestController
public class WebController {

  @EasySentinel(Qps = 10, fallback = "mock", resourceName = "获取用户名")
  @GetMapping(value = "/user/{name}")
  public String getUser(@PathVariable("name") String userName) {
    System.out.println("process:" + userName);
    return userName;
  }

  public String mock(String userName) {
    System.out.println("Mock::" + userName);
    return "Mock::" + userName;
  }
}

备用方法高级案例

前面只通过fallback实现备用方法的执行,但是平时为了让控制层的代码尽量保证精简,EasySentinel也支持通过工具类的方式来,指定blockHandlerClass处理类的方法blockHandler

@RestController
public class WebController {
  @EasySentinel(resourceName = "获取用户名2", Qps = 1, blockHandler = "fallBack",    `		blockHandlerClass = DefaultLimiterBlockHandler.class)
  @GetMapping(value = "/user1/{name}")
  public String getUser2(@PathVariable("name") String userName) {
    System.out.println("process:" + userName);
    return userName;
  }
}
public class DefaultLimiterBlockHandler {
  public Object fallBack(BlockException block, String userName) {
    return "DefaultLimiterBlockHandler::" + userName + block.getMessage();
  }
}

压力测试

ab -n 14 -c 1 http://127.0.0.1:8889/user/lxchinesszz

创作者

lxchinesszz

easy-sentinel's People

Contributors

lxchinesszz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.