springcloud gateway自定义断言规则详解,以后缀结尾进行路由

时间:2022-03-09 13:34:10

springcloud gateway自定义断言规则,后缀结尾进行路由

因工作需要,需要使用springcloud gateway ,以.html结尾的进行路由进行websocket转发。

gateway自带的8种路由规则都不能满足,故需要自定义断言规则。

1.新建一个路由断言工厂ExtCheckRoutePredicateFactory

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Component
public class ExtCheckRoutePredicateFactory extends AbstractRoutePredicateFactory<ExtCheckRoutePredicateFactory.Config> {
    public ExtCheckRoutePredicateFactory() {
        super(Config.class);
    }
    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return new Predicate<ServerWebExchange>() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                String url=serverWebExchange.getRequest().getURI().toString();
                if(url.endsWith(".html")){
                    return true;
                }
                return false;
            }
        };
    }
    public static class Config{
        private String name;
        public String getName(){
            return name;
        }
        public void setName(String name){
            this.name=name;
        }
    }
}

如果以.html结尾,则匹配此路由

2.修改gateway配置

?
1
2
3
4
5
6
gateway:
    routes:
      - id: abc
        uri: http://localhost:8080
        predicates:
          - name: ExtCheck

ExtCheck即是我们新建断言工厂的前缀名,自动识别的。

这时运行发现,系统根本找不到我们自定义的断言类。

需要第三步

3.修改gateway源码,将自定义断言类加到系统 predicates里

?
1
2
3
4
5
6
7
8
9
10
@Bean
 public RouteLocator routeDefinitionRouteLocator(GatewayProperties properties,
  List<GatewayFilterFactory> gatewayFilters,
  List<RoutePredicateFactory> predicates,
  RouteDefinitionLocator routeDefinitionLocator,
  ConfigurationService configurationService) {
  predicates.add(new ExtCheckRoutePredicateFactory());
  return new RouteDefinitionRouteLocator(routeDefinitionLocator, predicates,
    gatewayFilters, properties, configurationService);
 }

再次运行,成功根据.html后缀转发,done!

Gateway自定义路由断言工厂类

application.yml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
server:
  port: 7000
spring:
  zipkin:
    base-url: http://127.0.0.1:9411/  #zipkin server的请求地址
    discoveryClientEnabled: false #让nacos把它当成一个URL,而不要当做服务名
  sleuth:
    sampler:
      probability: 1.0  #采样的百分比
  application:
    name: api-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # 将gateway注册到nacos
    gateway:
      discovery:
        locator:
          enabled: true # 让gateway从nacos中获取服务信息
      routes:
        - id: product_route
          uri: lb://service-product
          order: 1
          predicates:
            - Path=/product-serv/**
          filters:
            - StripPrefix=1
        - id: order_route
          uri: lb://service-order
          order: 1
          predicates:
            - Path=/order-serv/**
            - Age=18,60
          filters:
            - StripPrefix=1

路由断言工厂配置类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
//这是一个自定义的路由断言工厂类,要求有两个
//1 名字必须是 配置+RoutePredicateFactory
//2 必须继承AbstractRoutePredicateFactory<配置类>
@Component
public class AgeRoutePredicateFactory extends AbstractRoutePredicateFactory<AgeRoutePredicateFactory.Config> {
    //构造函数
    public AgeRoutePredicateFactory() {
        super(Config.class);
    }
    //读取配置文件的中参数值 给他赋值到配置类中的属性上
    public List<String> shortcutFieldOrder() {
        //这个位置的顺序必须跟配置文件中的值的顺序对应
        return Arrays.asList("minAge", "maxAge");
    }
    //断言逻辑
    public Predicate<ServerWebExchange> apply(Config config) {
        return new Predicate<ServerWebExchange>() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                //1 接收前台传入的age参数
                String ageStr = serverWebExchange.getRequest().getQueryParams().getFirst("age");
                //2 先判断是否为空
                if (StringUtils.isNotEmpty(ageStr)) {
                    //3 如果不为空,再进行路由逻辑判断
                    int age = Integer.parseInt(ageStr);
                    if (age < config.getMaxAge() && age > config.getMinAge()) {
                        return true;
                    } else {
                        return false;
                    }
                }
                return false;
            }
        };
    }
    //配置类,用于接收配置文件中的对应参数
    @Data
    @NoArgsConstructor
    public static class Config {
        private int minAge;//18
        private int maxAge;//60
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zhangzhen02/article/details/109082792