gateway(gateway)

1.引入jar

        <!--nacos服务注册发现依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--网关gateway依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

2.属性配置,routes-> id,uri,predicates,filters

(1)id,指定路由唯一标识。

(2)uri,可以指定服务地址也可以指定ip:端口。

(3)predicates,断言有多种规则Path只是其中一种。

(4)路由过滤器filters,过滤器有多种规则。

(5)default-filters默认过滤器。

server:
  port: 10010
spring:
  application:
    name: gateway
  cloud:
    nacos:
      server-addr: localhost:80
    gateway:
      routes:
        - id: user-service # 路由标示,必须唯一
          uri: lb://userservice # 路由的目标地址
          predicates: # 路由断言,判断请求是否符合规则
            - Path=/user/** # 路径断言,判断路径是否是以/user开头,如果是则符合
          #filters:
          #  - AddRequestHeader=X-Request-Foo, Bar
        - id: order-service
          uri: lb://orderservice
          predicates:
            - Path=/order/**
      default-filters:
        - AddRequestHeader=X-Request-Foo, haha

3.全局过滤器

(1)实现GlobalFilter接口,编写业务逻辑。

(2)实现Ordered接口设置过滤器顺序值越小越优先,也可以使用@Order(1)注解直接指定值。

@Order(1)
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取参数
        MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
        //参数校验
        String auth = queryParams.getFirst("auth");
        if(auth != null && auth.equals("admin")){
            return chain.filter(exchange);
        }

        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        //异常返回
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

 4.过滤器排序规则

(1)按照order值排序,路由过滤器和默认过滤器按照定义的顺序从1依次递增2,3,4(路由过滤器和默认过滤器分别递增)。

(2)order值相同时按照 默认过滤器 》路由过滤器 》全局过滤器的顺序执行。

————————

1. Introduce jar

        <!--nacos服务注册发现依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--网关gateway依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

2.属性配置,routes-> id,uri,predicates,filters

(1) ID, specifies the unique identifier of the route.

(2) URI, you can specify the service address or IP: port.

(3) Predicates, there are many rules for assertions, and path is just one of them.

(4) Routing filters. Filters have a variety of rules.

(5) Default filters default filters.

server:
  port: 10010
spring:
  application:
    name: gateway
  cloud:
    nacos:
      server-addr: localhost:80
    gateway:
      routes:
        - id: user-service # 路由标示,必须唯一
          uri: lb://userservice # 路由的目标地址
          predicates: # 路由断言,判断请求是否符合规则
            - Path=/user/** # 路径断言,判断路径是否是以/user开头,如果是则符合
          #filters:
          #  - AddRequestHeader=X-Request-Foo, Bar
        - id: order-service
          uri: lb://orderservice
          predicates:
            - Path=/order/**
      default-filters:
        - AddRequestHeader=X-Request-Foo, haha

3. Global filter

(1) Implement globalfilter interface and write business logic.

(2) Implement the ordered interface to set the filter order. The smaller the order value, the higher the priority. You can also use the @ order (1) annotation to specify the value directly.

@Order(1)
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取参数
        MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
        //参数校验
        String auth = queryParams.getFirst("auth");
        if(auth != null && auth.equals("admin")){
            return chain.filter(exchange);
        }

        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        //异常返回
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

4. Filter collation

(1) According to the order value, the routing filter and default filter are incremented from 1 to 2, 3 and 4 in the defined order (the routing filter and default filter are incremented respectively).

(2) If the order value is the same, it shall be executed in the order of default filter, routing filter and global filter.