您现在的位置是:首页 > 文章详情

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

日期:2019-08-27点击:602

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; ​ }

文档

原文链接:https://www.oschina.net/news/109399/mica-1-1-8-released
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章