Spring Cloud Gateway(一)

1,232 阅读2分钟

官方文档:cloud.spring.io/spring-clou…

1,创建 spring-cloud-gateway项目

pom.xml文件中会增加spring-cloud-starter-gateway依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

application.yml配置文件配置:

spring:
  application:
    name: spring-cloud-gateway
  cloud:
    gateway:
      routes:
        - predicates:
            - Path=/gateway/**
          filters:
            - StripPrefix=1
          uri: http://localhost:8082/

server:
  port: 8080
  
# 【标注】StripPrefix=1 表示路由到目标服务地址后请求会去掉/gateway一个层级
#  比如 http://localhost:8080/gateway/getName 转发到 http://localhost:8082/getName
#  StripPrefix=2 表示会去掉/gateway/XXX/两个层级 以此类推。

这个配置文件,这次在配置的时候出现了一些问题,高低不生效,经过一番排查,怎么都是失败的,记得好像是用了Ta键,但是后来把所有的空格都用空格键重新缩进一下还是不行,所以把配置文件内容删除重新写了一遍,严格按照规则,最后可以了,哎,这个配置感觉有点坑!具体规则如下:

1,使用缩进表示层级关系
   缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 
2,缩进时不允许使用Tab键,只允许使用空格。空格来控制层级关系
3,k:(空格)v:表示一对键值对(空格必须有);

访问:http://localhost:8080/gateway/getName

2,Spring Cloud Gateway的核心概念

Route 路由,它是网关的基础元素,包含ID、目标URI、断言、过滤器组成,当前请求到达网关 时,会通过Gateway Handler Mapping,基于断言进行路由匹配,当断言为true时,匹配到路由 进行转发 Predicate,

  • 断言,学过java8的同学应该知道这个函数,它可以允许开发人员去匹配HTTP请求中 的元素,一旦匹配为true,则表示匹配到合适的路由进行转发 Filter,
  • 过滤器,可以在请求发出的前后进行一些业务上的处理,比如授权、埋点、限流等。

3,自定义RoutePredicateFactory

@Component
public class AuthRoutePredicateFactory extends AbstractRoutePredicateFactory<AuthRoutePredicateFactory.Config>{

    public AuthRoutePredicateFactory() {
        super(Config.class);
    }

    private static final String NAME_KEY="name";
    private static final String VALUE_KEY="value";

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(NAME_KEY,VALUE_KEY);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        //Header中携带了某个值,进行header的判断
        return (exchange->{
            HttpHeaders headers=exchange.getRequest().getHeaders();
            List<String> headerList=headers.get(config.getName());
            return headerList.size()>0;
        });
    }

    public static class Config{
        private String name;
        private String value;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
}

相关配置:

#spring:
#  application:
#    name: spring-cloud-gateway
#  cloud:
#    gateway:
#       routes:
#         - id: config_route
#           predicates:
#             - Path=/gateway/**
#           filters:
#             - StripPrefix=1
#           uri: http://localhost:8082/
#         - id: cookie_route
#           predicates:
#             - Path=/define/**
#               - Auth=MyAuthorization,token
#           filters:
#             - StripPrefix=1
#           uri: http://www.renaich.com
#
#server:
#  port: 8080

# 标注:AuthRoutePredicateFactory 截取RoutePredicateFactory之前的Auth,
#  MyAuthorization,hhhTest对应的是AuthRoutePredicateFactory中静态类Config中的name和value,

spring:
  application:
    name: spring-cloud-gateway
  cloud:
    gateway:
      routes:
        - id: config_route
          predicates:
            - Path=/gateway/**
          filters:
            - StripPrefix=1
          uri: http://localhost:8082/
        - id: cookie_route
          predicates:
            - Path=/define/**
            - Auth=MyAuthorization,hhhTest
          filters:
            - StripPrefix=1
          uri: http://www.renaich.com

server:
  port: 8080

发起请求: