gateway网关

  • 网关介绍
  • 为什么需要网关
  • 网关技术实现
  • 搭建网关环境
  • 路由断言工厂
  • 路由过滤器(Gateway Filter)
  • 默认过滤器
  • 全局过滤器
  • 案例 拦截并判断用户的身份
  • 定义过滤器的执行顺序
  • 方法一 注解
  • 方法二 实现接口


网关介绍

为什么需要网关

springcloud网关自定义路由过滤器 springcloud配置网关_java

网关技术实现

springcloud网关自定义路由过滤器 springcloud配置网关_spring_02

搭建网关环境

1.新建一个项目

springcloud网关自定义路由过滤器 springcloud配置网关_spring cloud_03

2.引入依赖

<dependencies>
<!--        网关组件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
<!--        nacos组件-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

3.修改配置文件

server.port=9999
spring.application.name=service-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
debug=true


#使用服务发现路由,设置为true,gateway才可以发现调用其他的服务
spring.cloud.gateway.discovery.locator.enabled=true
#服务路由名小写
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

#设置路由id
spring.cloud.gateway.routes[0].id=service-order
spring.cloud.gateway.routes[0].uri=lb://service-order
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates= Path=/orderservice/**
#配置service-edu服务
spring.cloud.gateway.routes[1].id=service-edu
spring.cloud.gateway.routes[1].uri=lb://service-edu
spring.cloud.gateway.routes[1].predicates= Path=/eduservice/**

springcloud网关自定义路由过滤器 springcloud配置网关_ide_04

路由断言工厂

springcloud网关自定义路由过滤器 springcloud配置网关_spring_05


springcloud网关自定义路由过滤器 springcloud配置网关_java_06


springcloud网关自定义路由过滤器 springcloud配置网关_gateway_07

server.port=9999
spring.application.name=service-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
debug=true


#使用服务发现路由,设置为true,gateway才可以发现调用其他的服务
spring.cloud.gateway.discovery.locator.enabled=true
#服务路由名小写
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

#设置路由id
spring.cloud.gateway.routes[0].id=service-order
spring.cloud.gateway.routes[0].uri=lb://service-order
#设置路由断言,代理servicerId为auth-service的/auth/路径
# 只转发在指定时间之后的请求
#spring.cloud.gateway.routes[0].predicates=After=2020-07-04T19:16:43.338+08:00[Asia/Shanghai]
#当有多个断言可以使用这样的配置
# 只转发带有指定请求头的请求
spring.cloud.gateway.routes[0].predicates[0]= Header=Host
spring.cloud.gateway.routes[0].predicates[1]= After=2020-07-04T19:16:43.338+08:00[Asia/Shanghai]
spring.cloud.gateway.routes[0].predicates[2]= Path=/aaa/**

路由过滤器(Gateway Filter)

springcloud网关自定义路由过滤器 springcloud配置网关_gateway_08


springcloud网关自定义路由过滤器 springcloud配置网关_gateway_09

spring.cloud.gateway.routes[0].filters[0]= AddRequestHeader=Color, blue
spring.cloud.gateway.routes[0].filters[1]= AddResponseHeader=X-Request-red, blue

默认过滤器

# 默认过滤器,会对所有的请求进行拦截
spring.cloud.gateway.default-filters[0]= AddResponseHeader=Meg, hello!

全局过滤器

和默认过滤器一样,但是默认过滤器只能实现简单的逻辑

springcloud网关自定义路由过滤器 springcloud配置网关_java_10

案例 拦截并判断用户的身份

springcloud网关自定义路由过滤器 springcloud配置网关_java_11

package com.zhw.gatewayservice.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;
@Component
public class GlobalFilter implements org.springframework.cloud.gateway.filter.GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取请求参数
        ServerHttpRequest request = exchange.getRequest();
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        //2.获取参数
        String authorization = queryParams.getFirst("authorization");
        //3.判断参数值是否等于admin
        if ("admin".equals(authorization)){
            //4.是放行
            chain.filter(exchange);
        }
        //5.不是,拦截
        //5.1 设置状态码,提示用户
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        //5.2 完成拦截
        return response.setComplete();
    }
}

springcloud网关自定义路由过滤器 springcloud配置网关_java_12

定义过滤器的执行顺序

方法一 注解

package com.zhw.gatewayservice.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;
@Order(-1)  //定义过滤器的执行顺序,数值越小,越先执行
@Component
public class GlobalFilter implements org.springframework.cloud.gateway.filter.GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取请求参数
        ServerHttpRequest request = exchange.getRequest();
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        //2.获取参数
        String authorization = queryParams.getFirst("authorization");
        //3.判断参数值是否等于admin
        if ("admin".equals(authorization)){
            //4.是放行
            chain.filter(exchange);
        }
        //5.不是,拦截
        //5.1 设置状态码,提示用户
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        //5.2 完成拦截
        return response.setComplete();
    }
}

方法二 实现接口

package com.zhw.gatewayservice.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取请求参数
        ServerHttpRequest request = exchange.getRequest();
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        //2.获取参数
        String authorization = queryParams.getFirst("authorization");
        //3.判断参数值是否等于admin
        if ("admin".equals(authorization)){
            //4.是放行
            chain.filter(exchange);
        }
        //5.不是,拦截
        //5.1 设置状态码,提示用户
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        //5.2 完成拦截
        return response.setComplete();
    }

    @Override
    public int getOrder() {
        return -1;
    }
}