当前位置:网站首页>[micro service SCG] use of predict

[micro service SCG] use of predict

2022-07-04 21:02:00 Bulst

To put it bluntly Predicate Just to implement a set of matching rules , It is convenient for the request to come and find the corresponding one Route To deal with , So let's move on Spring Cloud GateWay Built in several Predicate Use .

Through time matching

Predicate Support for setting a time , When the request is forwarded , It can be judged that before this time 、 Forward later or between . So let's say that we're only setting in 2022 year 7 month 5 Will be forwarded to my website , No forwarding until then , I can configure it like this :

spring:
  cloud:
    gateway:
      routes:
       - id: time_route
        uri: http://issavior.com
        predicates:
         - After=2022-07-05T06:06:06+08:00[Asia/Shanghai]

Spring It's through ZonedDateTime To compare the time ,ZonedDateTime yes Java 8 In the date and time function , Class used to represent date and time information with time zone ,ZonedDateTime Support for setting time by time zone , China's time zone is :Asia/Shanghai.

After Route Predicate Requests after this time are forwarded to the destination address . The above example refers to , Request time is 2022 year 7 month 5 Japan 6 spot 6 branch 6 All requests after seconds are forwarded to the address http://issavior.com.+08:00 It means time and UTC The time difference is eight hours , The time area is Asia/Shanghai.

After adding the routing rules , Access address http://localhost:8080 Automatically forwarded to http://issavior.com.

Before Route Predicate Just the opposite , Requests made prior to a certain time are forwarded . We put the above routing rules in After Change it to Before, as follows :

spring:
  cloud:
    gateway:
      routes:
       - id: after_route
        uri: http:http://issavior.com
        predicates:
         - Before=2022-07-05T06:06:06+08:00[Asia/Shanghai]

That means you can route before that time , Stop routing after this time , After the modification, restart the project and visit the address again http://localhost:8080, The page will say 404 No address found .

Except before or after time ,Gateway It also supports restricting the routing of requests within a certain time frame , have access to Between Route Predicate To achieve .

spring:
  cloud:
    gateway:
      routes:
       - id: after_route
        uri: http://issavior.com
        predicates:
         - Between=2022-07-05T06:06:06+08:00[Asia/Shanghai], 2022-07-09T06:06:06+08:00[Asia/Shanghai]

This setting means that the path can be matched within this time period , No match will be made beyond this time period . The ability to route by time matching is cool , Can be used in flash sale In some scenes .

adopt Cookie matching

Cookie Route Predicate You can take two parameters , One is Cookie name , One is a regular expression , The routing rules are retrieved by the corresponding Cookie name The value matches the regular expression , If there is a match, the route is executed , If there is no match, it is not executed .

spring:
  cloud:
    gateway:
      routes:
       - id: cookie_route
         uri: http://itsmdev.com
         predicates:
         - Cookie=issavior, cool

Use curl test , Command line input :

curl http://localhost:8080 --cookie “issavior=cool”

Returns the page code , If you remove –cookie --cookie “issavior=cool”, Background report 404 error .

Header Route Predicate and Cookie Route Predicate equally , It's also reception 2 Parameters , One header Property name and a regular expression , This property value matches the regular expression .

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: http://itsmdev.com
        predicates:
        - Header=X-Request-Id, \d+

Use curl test , Command line input :

curl http://localhost:8080 -H “X-Request-Id:666666”

Returns the page code to prove that the match was successful . The parameter -H "X-Request-Id:666666" Change it to -H "X-Request-Id:neo" Returns when executed again 404 There is no match .

adopt Host matching

Host Route Predicate Receives a set of parameters , A list of matched domain names , This template is one ant Separate templates , use . Sign as the separator . It USES the host address in the parameter as the matching rule .

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://itsmdev.com
        predicates:
        - Host=**itsmdev.com

Use curl test , Command line input :

curl http://localhost:8080 -H “Host: www.itsmdev.com”
curl http://localhost:8080 -H “Host: mditsmdev.com”

Both were tested host Can be matched to host_route route , Get rid of host The parameter will report 404 error .

Match by request

It can be done by POST、GET、PUT、DELETE And so on different request way to route .

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://itsmdev.com
        predicates:
        - Method=GET

Use curl test , Command line input :

#curl  The default is  GET  The way to request 
curl http://localhost:8080

Test the return page code , Prove a match to route , Let's use POST Way to request a test .

#curl  The default is  GET  The way to request 
curl -X POST http://localhost:8080

return 404 Can't find , Prove no match by road

Match by request path

Path Route Predicate Receive a parameter that matches the path to determine whether to walk by .

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://itsmdev.com
        predicates:
        - Path=/foo/{
    segment}

If the request path meets the requirements , The path will match , for example :/foo/1 perhaps /foo/bar.

Use curl test , Command line input :

curl http://localhost:8080/foo/1
curl http://localhost:8080/foo/xx
curl http://localhost:8080/boo/xx

After testing the first and second commands can get the return value of the page normally , Last order 404, Proves that a route is matched by a specified route .

Matches by request parameters

Query Route Predicate Support for passing in two parameters , One is the property name and one is the property value , Property values can be regular expressions .

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://itsmdev.com
        predicates:
        - Query=smile

This configuration , As long as the request contains smile Property to match the route .

Use curl test , Command line input :

curl localhost:8080?smile=x&id=2

Testing found that as long as the request summary has smile The parameters will then match the route , No smile The arguments don't match .

Can also be Query Is configured as a key-value pair , This will match the property values to the regularization when the request comes through , Match up to be able to walk by .

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://itsmdev.com
        predicates:
        - Query=keep, pu.

This is as long as included in the request keep Property and the parameter value is pu Strings that start with a length of three digits are matched and routed .

Use curl test , Command line input :

curl localhost:8080?keep=pub

Tests can return page code , take keep The attribute value of pubx Another visit will be reported 404, Prove that the route needs to match a regular expression to be routed .

By request ip Address matching

Predicate Also supports setting something ip Interval segment requests are routed ,RemoteAddr Route Predicate Accept cidr Symbol (IPv4 or IPv6) List of strings ( The minimum size is 1), for example 192.168.0.1/16 ( among 192.168.0.1 yes IP Address ,16 It's the subnet mask ).

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: http://itsmdev.com
        predicates:
        - RemoteAddr=192.168.1.1/24

This address can be set to native ip Address test .

As a result, the requested remote address is 192.168.1.10, The path will match .

Weight routing

This route would forward ~80% of traffic to weighthigh.org and ~20% of traffic to weighlow.org.

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

The XForwarded Remote Addr Route Predicate Factory

This route matches if the X-Forwarded-For header contains, for example, 192.168.1.10.

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

Use a combination of

The above is to demonstrate each one Predicate Use , We did the configuration tests individually , You can actually combine all of these Predicate Combine and use together .

for example :

spring:
  cloud:
    gateway:
      routes:
       - id: host_foo_path_headers_to_httpbin
        uri: http://itsmdev.com
        predicates:
        - Host=**.foo.org
        - Path=/headers
        - Method=GET
        - Header=X-Request-Id, \d+
        - Query=foo, ba.
        - Query=baz
        - Cookie=chocolate, ch.p
        - After=2022-1-01T06:06:06+08:00[Asia/Shanghai]

Various Predicates Exists simultaneously on the same route , The request must satisfy all the criteria at the same time to be matched by this route .

When a request satisfies predicate conditions for multiple routes , Requests are forwarded only by the first successfully matched route .

原网站

版权声明
本文为[Bulst]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041949585199.html