From f599fc2cb3ff2363834ad58868a6db77beb6f885 Mon Sep 17 00:00:00 2001 From: yizzuide Date: Tue, 21 Apr 2020 15:14:13 +0800 Subject: [PATCH] refactor(come|particle): Key template support extract value with `$params.name`, `$header.name`, `$cookie.name`. --- Milkomeda/pom.xml | 4 +- .../comet/logger/CometLoggerProperties.java | 6 +- .../particle/ParticleProperties.java | 6 +- .../parser/url/URLPlaceholderParser.java | 68 +++++++++++++------ MilkomedaDemo/pom.xml | 2 +- .../src/main/resources/application.yml | 2 +- 6 files changed, 57 insertions(+), 31 deletions(-) diff --git a/Milkomeda/pom.xml b/Milkomeda/pom.xml index 62660032..2782e192 100644 --- a/Milkomeda/pom.xml +++ b/Milkomeda/pom.xml @@ -11,7 +11,7 @@ 1.8 - 3.0.10-SNAPSHOT + 3.1.0-SNAPSHOT 2.2.4 Hoxton.RELEASE 2.1.1 @@ -65,7 +65,7 @@ sonatype-oss-release - 3.0.10 + 3.1.0 diff --git a/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/comet/logger/CometLoggerProperties.java b/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/comet/logger/CometLoggerProperties.java index 5f17eb11..54b77bdc 100644 --- a/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/comet/logger/CometLoggerProperties.java +++ b/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/comet/logger/CometLoggerProperties.java @@ -12,7 +12,7 @@ * * @author yizzuide * @since 3.0.0 - * @since 3.0.3 + * @since 3.1.0 * Create at 2020/04/05 18:47 */ @Data @@ -46,9 +46,9 @@ public static class Strategy { */ private List paths = Collections.singletonList("/**"); /** - * 策略模板,默认能识别的占位符:uri、method、params(参数可配置占位符,请求头固定使用[]) + * 策略模板(固定占位符:uri、method、params;请求参数域:$params.name;请求头域:$header.name;cookie域:$cookie.name) */ - private String tpl = "{\"uri\":\"{uri}\", \"method\": \"{method}\", \"params\": \"{params}\", \"token\": \"(token)\"}"; + private String tpl = "{\"uri\":\"{uri}\", \"method\": \"{method}\", \"params\": \"{params}\", \"token\": \"{$header.token}\"}"; /** * 缓存占位符(模块内部使用) */ diff --git a/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/particle/ParticleProperties.java b/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/particle/ParticleProperties.java index 0a08166c..340cdcd4 100644 --- a/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/particle/ParticleProperties.java +++ b/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/particle/ParticleProperties.java @@ -14,7 +14,7 @@ * * @author yizzuide * @since 3.0.0 - * @version 3.0.4 + * @version 3.1.0 * Create at 2020/04/08 11:12 */ @Data @@ -63,9 +63,9 @@ public static class Limiter { private Map props; /** - * 分布式key模板 + * 分布式key模板(固定占位符:uri、method、params;请求参数域:$params.name;请求头域:$header.name;cookie域:$cookie.name) */ - private String keyTpl = "limit_{method}_{uri}_(token)"; + private String keyTpl = "limit_{method}_{uri}_{$header.token}"; /** * 分布式key过期时间 diff --git a/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/universe/parser/url/URLPlaceholderParser.java b/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/universe/parser/url/URLPlaceholderParser.java index 3af11fc4..faa631ec 100644 --- a/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/universe/parser/url/URLPlaceholderParser.java +++ b/Milkomeda/src/main/java/com/github/yizzuide/milkomeda/universe/parser/url/URLPlaceholderParser.java @@ -5,14 +5,15 @@ import com.github.yizzuide.milkomeda.util.JSONUtil; import com.github.yizzuide.milkomeda.util.PlaceholderResolver; import lombok.Data; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * URLPlaceholderParser @@ -20,24 +21,32 @@ * * @author yizzuide * @since 3.0.0 - * @version 3.0.3 + * @version 3.1.0 * Create at 2020/04/09 15:07 */ @Data public class URLPlaceholderParser { + // 请求参数解析 - private PlaceholderResolver paramsPlaceholderResolver; - // 请求头解析 - private static PlaceholderResolver headerPlaceholderResolver = PlaceholderResolver.getResolver("(", ")"); - // 忽略的固定参数 - private static List ignorePlaceHolders = Arrays.asList("uri", "method", "params"); + private PlaceholderResolver placeholderResolver; + private String paramsPrefix; + private String paramsSuffix; /** * 自定义请求参数解析器 */ private URLPlaceholderResolver customURLPlaceholderResolver; + // 固定参数 + private static List ignorePlaceHolders = Arrays.asList("uri", "method", "params"); + + // 请求数据域 + private static final String headerStartToken = "$header."; + private static final String cookieStartToken = "$cookie."; + private static final String paramsStartToken = "$params."; + public static final String KEY_HEAD = "header"; + public static final String KEY_COOKIE = "cookie"; public static final String KEY_PARAMS = "params"; public URLPlaceholderParser() { @@ -50,7 +59,9 @@ public URLPlaceholderParser() { * @param paramsSuffix 参数占位符后缀 */ public URLPlaceholderParser(String paramsPrefix, String paramsSuffix) { - paramsPlaceholderResolver = PlaceholderResolver.getResolver(paramsPrefix, paramsSuffix); + this.paramsPrefix = paramsPrefix; + this.paramsSuffix = paramsSuffix; + placeholderResolver = PlaceholderResolver.getResolver(paramsPrefix, paramsSuffix); } /** @@ -59,9 +70,11 @@ public URLPlaceholderParser(String paramsPrefix, String paramsSuffix) { * @return Map */ public Map> grabPlaceHolders(String tpl) { - Map> keyMap = new HashMap<>(4); - keyMap.put(KEY_HEAD, headerPlaceholderResolver.getPlaceHolders(tpl)); - keyMap.put(KEY_PARAMS, paramsPlaceholderResolver.getPlaceHolders(tpl)); + List placeHolders = placeholderResolver.getPlaceHolders(tpl); + Map> keyMap = new HashMap<>(6); + keyMap.put(KEY_HEAD, placeHolders.stream().filter(s -> s.startsWith(headerStartToken)).collect(Collectors.toList())); + keyMap.put(KEY_COOKIE, placeHolders.stream().filter(s -> s.startsWith(cookieStartToken)).collect(Collectors.toList())); + keyMap.put(KEY_PARAMS, placeHolders.stream().filter(s -> s.startsWith(paramsStartToken)).collect(Collectors.toList())); return keyMap; } @@ -90,22 +103,35 @@ public String parse(String tpl, HttpServletRequest request, String params, MapUTF-8 1.8 Hoxton.RELEASE - 3.0.10-SNAPSHOT + 3.1.0-SNAPSHOT 2.1.1 diff --git a/MilkomedaDemo/src/main/resources/application.yml b/MilkomedaDemo/src/main/resources/application.yml index 8140dd5b..2f06395b 100644 --- a/MilkomedaDemo/src/main/resources/application.yml +++ b/MilkomedaDemo/src/main/resources/application.yml @@ -73,7 +73,7 @@ milkomeda: # 根据路径匹配策略 strategy: - paths: ["/**"] - tpl: url->{uri},userId:{uid},token:(token),method->{method},parameters->{params},dataId->{data.id} + tpl: url->{uri},userId:{uid},token:{$header.token},method->{method},parameters->{params},dataId->{$params.data.id} - paths: ["/error/**"] tpl: url->{uri},method->{method} # 日志收集器