mica 云母,寓意为云服务的核心,增强 Spring cloud 功能,使得 Spring cloud 服务开发更加方便快捷。
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 版 |
:zap:️ mica-plus-redis 添加 redis 限流组件.
:zap:️ mica-http Response asDocument 方法迁移到 DomMapper,不强制依赖 jsoup.
:zap:️ mica-http CssQuery 添加正则取值处理.
:zap:️ mica-http 优化 DomMapper 添加更多方法.
:zap:️ mica-http proxy 改用 MethodInterceptor.
:bug: mica-cloud Fixing Feign feignContract mvcConversionService.
:zap:️ mica-core 优化 Exceptions.unchecked 方法使异常抛出更准确.
:zap:️ mica-core 拆分 lambda Try 为 Unchecked.
:bug: 优化 gradle 配置,自动发布 snapshots 版本.
迁移 spring-cloud-alibaba 依赖到新版。
:arrow_up: Spring boot 升级到 2.1.7.RELEASE.
mica-plus-redis
组件采用 lua
脚本和 aop
使用起来更加方便。
mica:
redis:
rate-limiter:
enable: true
@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;
@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);
@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));
}
// 同步,异常返回 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());
}
@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;
}
@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;
}
@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.dreamlu.net/#/doc/docs
文档地址(语雀-可关注订阅): https://www.yuque.com/dreamlu/mica
示例项目: https://gitee.com/596392912/mica-example
权限管理平台: https://gitee.com/596392912/dream-security