springcloud gateway简介 Predicates(二)

5. 内置Predicates断言

5.1 After 路由断言

After 路由断言只有一个ZonedDateTime生成的datetime参数,只有在这个事件之后的请求才能匹配上。

Example 1. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

https://example.org这个地址在美国Denver时间2017-01-20 17:42:00之后才能

// 生成时间的代码
// demo 2020-05-06T19:16:43.338+08:00[Asia/Shanghai]
System.out.println(ZonedDateTime.now().toString());
5.2 Before 路由断言

Before 同样是一个时间断言,只不过是在配置的时间之前有效,过时之后失效。

Example 2. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

上面的链接地址在America/Denver时间2017-01-20 17:42::00之前有效

5.3 Between 路由断言

Between 路由断言,时间类断言,在声明的时间内有效。并且第二个时间必须大于第一个时间。

Example 3. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

上面的链接在America/Denver时间2017-01 20日17时42-21日17时47分之间有效。

5.4 Cookie 路由断言

Cookie 断言有两个参数,一个cookie 名称和一个java 正则表达式,这个断言匹配给定的cookie 名和值正则匹配的请求。

Example 4. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

上面的匹配那些请求中包含参数名为chocolate并且值匹配ch.p的请求。

5.5 Header 路由断言

Header断言有两个参数,一个参数名,一个正则。只有当有这个参数并且值匹配正则的时候才能执行下去。

Example 5. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, d+

这个路由规则匹配Header中包含X-Request-Id并且值为纯数字的请求。

5.6 Host 路由断言

Host 路由断言接受一个正则域名列表,正则是用英文句号分割的Ant-Style(ant 风格?)正则表达式。

Example 6. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

这个路由匹配somehost.org anotherhost.org这两个域名下的所有请求。

5.7 Method 路由断言

Method路由断言匹配一个或多个Http请求方式(GET POST PUT DELETE HEAD).

Example 7. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST

这个路由匹配所有的GET POST请求。

5.8 Path 路径路由断言

Path路由断言接受两个参数:Spring PathMatcher 正则列表和一个名为 matchOptionalTrailingSeparator 的可选标志。

Example 8. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

如果请求路径为(例如: / red / 1或 / red / blue 或 / blue / green) ,则此路由匹配。
这个断言提取了URL中模板变量(像前面例子中声明的segment)组为key/value键值对放置在ServerWebExchange.getAttributes()中,并用已经声明的ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE来声明。这些内容可被GatewayFilter factories的一个名为get的方法很简单的去访问。

Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);

String segment = uriVariables.get("segment");
5.9 Query 查询路由断言

Query路由断言有两个参数: 一个必传参数 and 和一个可选的正则表达式。

Example 9. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

如果一个请求中包含green的参数,则匹配成功。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=red, gree.

如果一个请求中包含参数red并且值匹配·gree.·这个正则,那么路由匹配。比如:green和greet。

5.10 RemoteAddr 远程地址路由断言

RemoteAddr路由断言接受一个来源地址的list,由IPv4或者IPv6组成,比如:192.168.0.1/16(其中192.168.0.1是IP地址,16是子网掩码)

Example 10. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

如果请求的远程地址是192.168.1.10,则此路由匹配。

5.11. Weight 权重路由断言

权重路由断言接受两个参数:group分组和Weight权重。权重是按组计算的。

Example 11. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

这条路线将80% 的流量转发到 weighthigh. org,20% 的流量转发到 weighlow. org。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HPL: Vol. IV: Functional and Logic Programming Languages by Peter Salus Macmillan Technical Publishing, Macmillan Computer Publishing ISBN: 1578700116 Pub Date: 06/11/98 Foreword to the Handbook of Programming Languages About the Authors Part 1—Lisp Chapter 1—The LISP Language Chapter 2—Emacs Lisp: A Short Description 2.1. GNU Emacs and Emacs Lisp 2.2. Lisp Lists 2.2.1. Parts of Lisp 2.3. Example: Two Plus Two 2.4. Evaluation 2.5. A Function Definition 2.5.1. An Example of a Search Within a Buffer 2.5.2. An Example: multiply-by-seven 2.6. Variables 2.7. A Chest of Drawers 2.8. Functions 2.9. The read-eval-print Loop and Side Effects 2.10. Types of Variables 2.10.1. defvar and defconst 2.10.2. setq 2.10.3. Passing an Argument 2.10.4. A let Expression 2.10.5. Buffer-Local Variables 2.11. Sequencing 2.12. Conditionals 2.12.1. and, or, and not 2.13. while Loops and Recursion 2.13.1. while 2.13.2. car, cdr, cons: Fundamental Functions 2.13.3. while, Continued 2.13.4. Recursion 2.14. Macros 2.14.1. The list Built-in Function 2.14.2. Backquote 2.15. Property Lists 2.16. Keymaps 2.17. Editing Lisp 2.18. Help 2.19. Debugging 2.19.1. The Built-in Debugger 2.19.2. Edebug 2.20. Backups and Auto-Saving 2.21. Evaluating or Loading a Whole File 2.22. Byte Compilation 2.23. Your .emacs Initialization File Part II—Scheme Chapter 3—Scheme 3.1. Who Uses Scheme? 3.2. Scheme as a Dialect of Lisp 3.2.1. Expressions 3.2.2. Uniform Syntax 3.2.3. Automatic Storage Management 3.2.4. High-Level Data Types 3.2.5. Untyped Variables 3.3. Scheme as a Dialect of Algol 3.3.1. Internal Definitions 3.3.2. Lexical Scope 3.4. Innovations in Scheme 3.4.1. First-Class Data 3.4.2. Tail Call Elimination 3.4.3. First-Class Continuations 3.4.4. Lexical Scoping Rules for Macros 3.5. Functional Programming in Scheme 3.6. Object-Oriented Programming in Scheme 3.7. Common Problems for Beginning Scheme Programmers 3.7.1. Function Comp
I 数学分析(Proofs) 简介(Introduction) 0.1 参考文献(References) 1 什么是证明?(What is a Proof?) 1.1 命题(Propositions) 1.2 谓词(Predicates) 1.3 公理化方法(The Axiomatic Method) 1.4 我们的公理(Our Axioms) 1.5 证明命题的含义(Proving an Implication) 1.6 证明「有且仅有」(Proving an「If and Only If」) 1.7 案例证明(Proof by Cases) 1.8 反证法(Proof by Contradiction) 1.9 证明的实战演练(Good Proofs in Practice) 1.10 参考文献(References) 2 良序原则(The Well Ordering Principle) 2.1 良序证明(Well Ordering Proofs) 2.2 良序证明模式(Template for Well Ordering Proofs) 2.3 素数因子分解(Factoring into Primes) 2.4 良序集合(Well Ordered Sets) 3 逻辑公式(Logical Formulas) 3.1 命题中的命题(Propositions from Propositions) 3.2 计算机程序中的命题逻辑(Propositional Logic in Computer Programs) 3.3 等价性和有效性(Equivalence and Validity) 3.4 命题的代数(The Algebra of Propositions) 3.5 SAT 问题(The SAT Problem) 3.6 谓词公式(Predicate Formulas) 3.7 参考文献(References) 4 数学上的数据类型(Mathematical Data Types) 4.1 集合(Sets) 4.2 序列(Sequences) 4.3 函数(Functions) 4.4 元关系(Binary Relations) 4.5 有限基数(Finite Cardinality) 5 简介(Induction) 5.1 一般归纳法(Ordinary Induction) 5.2 强归纳法(Strong Induction) 5.3 强归纳法、一般归纳法和良序法(Strong Induction vs. Induction vs. Well Ordering) 6 状态机(State Machines) 6.1 状态和转换(States and Transitions) 6.2 不变量原则(The Invariant Principle) 6.3 部分正确性和终止(Partial Correctness & Termination) 6.4 稳定婚姻问题(The Stable Marriage Problem) 7 递归数据型(Recursive Data Types) 7.1 递归定义和结构归纳法(Recursive Definitions and Structural Induction) 7.2 Matched Brackets 字符串(Strings of Matched Brackets) 7.3 非负整数递归函数(Recursive Functions on Nonnegative Integers) 7.4 算术表达式(Arithmetic Expressions) 7.5 递归数据型在计算机科学中的简介(Induction in Computer Science) 8 无限集(Infinite Sets) 8.1 无限基数集(Infinite Cardinality) 8.2 停止问题(The Halting Problem) 8.3 集合的逻辑(The Logic of Sets) 8.4 这些真的有效吗?(Does All This Really Work?) II 结构(Structures) Introduction 9 数论(Number Theory) 9.1 可分性(Divisibility) 9.2 最大公约数(The Greatest Common Divisor) 9.3 神秘的素数(PrimeMysteries) 9.4 算术的基本定理(The Fundamental Theorem of Arithmetic) 9.5 Alan Turing 9.6 模运算(Modular Arithmetic) 9.7 余数运算(Remainder Arithmetic) 9.8 Turing's Code (Version 2.0) 9.9 乘法逆运算和消除(Multiplicative Inverses and Cancelling) 9.10 欧拉定理(Euler's Theorem) 9.11 RSA 公钥加密(RSA Public Key Encryption) 9.12 SAT 与它有什么关系?(What has SAT got to do with it?) 9.13 参考文献(References) 10 有向图和部分排序(Directed graphs & Partial Orders) 10.1 顶点度(Vertex Degrees) 10.2 步长与路径(Walks and Paths) 10.3 临近矩阵(Adjacency Matrices) 10.4 Walk Relations 10.5 有向非循环图标和时序(Directed Acyclic Graphs & Scheduling) 10.6 局部排序(Partial Orders) 10.7 通过集合遏制表征局部排序(Representing Partial Orders by Set Containment) 10.8 线性排序(Linear Orders) 10.9 乘积排序(Product Orders) 10.10 等价关系(Equivalence Relations) 10.11 关系属性总结(Summary of Relational Properties) 11 通信网络(Communication Networks) 11.1 路由(Routing) 11.2 Routing Measures) 11.3 网络设计(Network Designs) 12 简单图(Simple Graphs) 12.1 Vertex Adjacency and Degrees) 12.2 美国性别人口统计(Sexual Demographics in America) 12.3 一些常见的图(Some Common Graphs) 12.4 同构(Isomorphism) 12.5 部图&匹配(Bipartite Graphs & Matchings) 12.6 Coloring 12.7 Simple Walks 12.8 连接(Connectivity) 12.9 森林和树(Forests & Trees) 12.10 References 13 平面图(Planar Graphs) 13.1 在平面中绘制图(Drawing Graphs in the Plane) 13.2 平面图的定义(Definitions of Planar Graphs) 13.3 欧拉公式(Euler's Formula) 13.4 在平面图中限定边的数量(Bounding the Number of Edges in a Planar Graph) 13.5 Returning to K5 and K3;3 13.6 Coloring Planar Graphs 13.7 Classifying Polyhedra 13.8 平面图的另一种特征化(Another Characterization for Planar Graphs) III 计数(Counting) Introduction 14 逼近求和(Sums and Asymptotics) 14.1 养老金的价值(The Value of an Annuity) 14.2 幂级数求和 Sums of Powers) 14.3 逼近求和(Approximating Sums) 14.4 Hanging Out Over the Edge) 14.5 乘积(Products) 14.6 Double Trouble 14.7 渐进的符号表示(Asymptotic Notation) 15 基数法则(Cardinality Rules) 15.1 由计算另一项计算该项(Counting One Thing by Counting Another) 15.2 计算序列(Counting Sequences) 15.3 广义乘积法则(The Generalized Product Rule) 15.4 除法法则(The Division Rule) 15.5 子集计算(Counting Subsets) 15.6 重复序列(Sequences with Repetitions) 15.7 Counting Practice: Poker Hands 15.8 鸽巢原理(The Pigeonhole Principle) 15.9 包含与排斥(Inclusion-Exclusion) 15.10 组合证明(Combinatorial Proofs) 15.11 References 16 母函数(Generating Functions) 16.1 无穷级数(Infinite Series) 16.2 使用母函数进行计数(Counting with Generating Functions) 16.3 部分分式(Partial Fractions) 16.4 求解线性递归(Solving Linear Recurrences) 16.5 形式幂级数(Formal Power Series) 16.6 References IV 概率论(Probability) Introduction 17 事件和概率空间(Events and Probability Spaces) 17.1 Let's Make a Deal 17.2 四步法(The Four Step Method) 17.3 Strange Dice 17.4 生日原则(The Birthday Principle) 17.5 集合论和概率论(Set Theory and Probability) 17.6 References 18 条件概率(Conditional Probability) 18.1 Monty Hall Confusion 18.2 定义和符号(Definition and Notation) 18.3 条件概率的四步法(The Four-Step Method for Conditional Probability) 18.4 为什么树状图如此有效(Why Tree Diagrams Work) 18.5 全概法则(The Law of Total Probability) 18.6 辛普森悖论(Simpson's Paradox) 18.7 独立性(Independence) 18.8 相互独立性(Mutual Independence) 18.9 概率与置信度(Probability versus Confidence) 19 随机变量(Random Variables) 19.1 随机样本(Random Variable Examples) 19.2 独立性(Independence) 19.3 分布函数(Distribution Functions) 19.4 期望(Great Expectations) 19.5 线性期望(Linearity of Expectation) 20 平均偏差(Deviation from the Mean) 20.1 马尔可夫定理(Markov‘s Theorem) 20.2 切比雪夫定理(Chebyshev's Theorem) 20.3 方差的性质(Properties of Variance) 20.4 随机样本估计(Estimation by Random Sampling) 20.5 估计置信度(Confidence in an Estimation) 20.6 随机变量加和(Sums of Random Variables) 20.7 Really Great Expectations 21 随机步(Random Walks)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值