mica 1.1.8 发布,添加分布式限流组件

栏目: 软件资讯 · 发布时间: 4年前

内容简介:mica(云母) mica 云母,寓意为云服务的核心,增强 Spring cloud 功能,使得 Spring cloud 服务开发更加方便快捷。 mica 核心依赖 mica 基于 java 8,没有历史包袱,支持传统 Servlet 和 Reactive(webflux)。采...

mica(云母)

mica 云母,寓意为云服务的核心,增强 Spring cloud 功能,使得 Spring cloud 服务开发更加方便快捷。

mica 核心依赖

mica 基于 java 8,没有历史包袱,支持传统 Servlet 和 Reactive(webflux)。采用 mica-auto 自动生成 spring.factories 和 spring-devtools.properties 配置,仅依赖 Spring boot、Spring cloud 全家桶,无第三方依赖。市面上鲜有的微服务核心组件。

依赖 版本
Spring 5.x
Spring Boot 2.1.x
Spring Cloud Greenwich 版

更新说明

  • ⚡️ mica-plus-redis 添加 redis 限流组件.

  • ⚡️ mica-http Response asDocument 方法迁移到 DomMapper,不强制依赖 jsoup.

  • ⚡️ mica-http CssQuery 添加正则取值处理.

  • ⚡️ mica-http 优化 DomMapper 添加更多方法.

  • ⚡️ mica-http proxy 改用 MethodInterceptor.

  • ???? mica-cloud Fixing Feign feignContract mvcConversionService.

  • ⚡️ mica-core 优化 Exceptions.unchecked 方法使异常抛出更准确.

  • ⚡️ mica-core 拆分 lambda Try 为 Unchecked.

  • ???? 优化 gradle 配置,自动发布 snapshots 版本.

  • ???? 迁移 spring-cloud-alibaba 依赖到新版。

  • ⬆️ Spring boot 升级到 2.1.7.RELEASE.

一、添加分布式限流

mica-plus-redis 组件采用 lua 脚本和 aop 使用起来更加方便。

1.1 开启限流组件

mica:
  redis:
    rate-limiter:
      enable: true

1.2 使用注解

@RateLimiter

注解变量:

/**
 * 限流的 key 支持,必须:请保持唯一性
 *
 * @return key
 */
String value();
​
/**
 * 限流的参数,可选,支持 spring el # 读取方法参数和 @ 读取 spring bean
 *
 * @return param
 */
String param() default "";
​
/**
 * 支持的最大请求,默认: 2500
 *
 * @return 请求数
 */
long max() default 2500L;
​
/**
 * 持续时间,默认: 3600
 *
 * @return 持续时间
 */
long ttl() default 3600L;
​
/**
 * 时间单位,默认为秒
 *
 * @return TimeUnit
 */
TimeUnit timeUnit() default TimeUnit.SECONDS;

1.3 使用 Client

@Autowired
private RateLimiterClient rateLimiterClient;

方法:

/**
 * 服务是否被限流
 *
 * @param key 自定义的key,请保证唯一
 * @param max 支持的最大请求
 * @param ttl 时间,单位默认为秒(seconds)
 * @return 是否允许
 */
boolean isAllowed(String key, long max, long ttl);
​
/**
 * 服务是否被限流
 *
 * @param key      自定义的key,请保证唯一
 * @param max      支持的最大请求
 * @param ttl      时间
 * @param timeUnit 时间单位
 * @return 是否允许
 */
boolean isAllowed(String key, long max, long ttl, TimeUnit timeUnit);
​
/**
 * 服务限流,被限制时抛出 RateLimiterException 异常,需要自行处理异常
 *
 * @param key      自定义的key,请保证唯一
 * @param max      支持的最大请求
 * @param ttl      时间
 * @param supplier Supplier 函数式
 * @return 函数执行结果
 */
<T> T allow(String key, long max, long ttl, CheckedSupplier<T> supplier);
​
/**
 * 服务限流,被限制时抛出 RateLimiterException 异常,需要自行处理异常
 *
 * @param key      自定义的key,请保证唯一
 * @param max      支持的最大请求
 * @param ttl      时间
 * @param supplier Supplier 函数式
 * @return 函数执行结果
 */
<T> T allow(String key, long max, long ttl, TimeUnit timeUnit, CheckedSupplier<T> supplier);

1.4 使用示例

@GetMapping("test1")
@RateLimiter(value = "test1", param = "#name", max = 2)
@ResponseBody
public String test1(String name) {
    return "hello:" + name;
}
@Autowired
private RateLimiterClient rateLimiterClient;
​
@GetMapping("test2")
@ResponseBody
public String test2(String name) {
    return rateLimiterClient.allow("test2", 2, 3, () -> testService.test(name));
}

二、强化 mica-http 爬虫功能

2.1 爬取开源中国首页

// 同步,异常返回 null
Oschina oschina = HttpRequest.get("https://www.oschina.net")
    .execute()
    .onSuccess(responseSpec -> responseSpec.asDomValue(Oschina.class));
if (oschina == null) {
    return;
}
System.out.println(oschina.getTitle());
​
System.out.println("热门新闻");
​
List<VNews> vNews = oschina.getVNews();
for (VNews vNew : vNews) {
    System.out.println("title:\t" + vNew.getTitle());
    System.out.println("href:\t" + vNew.getHref());
    System.out.println("时间:\t" + vNew.getDate());
}
​
System.out.println("热门博客");
List<VBlog> vBlogList = oschina.getVBlogList();
for (VBlog vBlog : vBlogList) {
    System.out.println("title:\t" + vBlog.getTitle());
    System.out.println("href:\t" + vBlog.getHref());
    System.out.println("阅读数:\t" + vBlog.getRead());
    System.out.println("评价数:\t" + vBlog.getPing());
    System.out.println("点赞数:\t" + vBlog.getZhan());
}

2.2 模型1

@Getter
@Setter
public class Oschina {
​
    @CssQuery(value = "head > title", attr = "text")
    private String title;
​
    @CssQuery(value = "#v_news .page .news", inner = true) // 标记为嵌套模型
    private List<VNews> vNews;
​
    @CssQuery(value = ".blog-container .blog-list div", inner = true) // 标记为嵌套模型
    private List<VBlog> vBlogList;
​
}

2.2 模型2

@Setter
@Getter
public class VNews {
​
    @CssQuery(value = "a", attr = "title")
    private String title;
​
    @CssQuery(value = "a", attr = "href")
    private String href;
​
    @CssQuery(value = ".news-date", attr = "text")
    @DateTimeFormat(pattern = "MM/dd")
    private Date date;
​
}

2.3 模型3

@Getter
@Setter
public class VBlog {
​
    @CssQuery(value = "a", attr = "title")
    private String title;
​
    @CssQuery(value = "a", attr = "href")
    private String href;
​
    //1341阅/9评/4赞
    @CssQuery(value = "span", attr = "text", regex = "^\\d+")
    private Integer read;
​
    @CssQuery(value = "span", attr = "text", regex = "(\\d*).*/(\\d*).*/(\\d*).*", regexGroup = 2)
    private Integer ping;
​
    @CssQuery(value = "span", attr = "text", regex = "(\\d*).*/(\\d*).*/(\\d*).*", regexGroup = 3)
    private Integer zhan;
​
}

文档


以上所述就是小编给大家介绍的《mica 1.1.8 发布,添加分布式限流组件》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

How to Think About Algorithms

How to Think About Algorithms

Jeff Edmonds / Cambridge University Press / 2008-05-19 / USD 38.99

HOW TO THINK ABOUT ALGORITHMS There are many algorithm texts that provide lots of well-polished code and proofs of correctness. Instead, this one presents insights, notations, and analogies t......一起来看看 《How to Think About Algorithms》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

MD5 加密
MD5 加密

MD5 加密工具