SpringCloud Gateway

1.SpringCloud Gateway 简介

SpringCloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。

SpringCloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 2.0之前的非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。

Spring Cloud Gateway 的目标,不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。

提前声明:Spring Cloud Gateway 底层使用了高性能的通信框架Netty

1.2 SpringCloud Gateway 特征

SpringCloud官方,对SpringCloud Gateway 特征介绍如下:

(1)基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0

(2)集成 Hystrix 断路器

(3)集成 Spring Cloud DiscoveryClient

(4)Predicates 和 Filters 作用于特定路由,易于编写的 Predicates 和 Filters

(5)具备一些网关的高级功能:动态路由、限流、路径重写

从以上的特征来说,和Zuul的特征差别不大。SpringCloud Gateway和Zuul主要的区别,还是在底层的通信框架上。

简单说明一下上文中的三个术语:

1Filter(过滤器)

和Zuul的过滤器在概念上类似,可以使用它拦截和修改请求,并且对上游的响应,进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。

(2)Route(路由):

网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。

3Predicate(断言)

这是一个 Java 8 的 Predicate,可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。断言的输入类型是一个 ServerWebExchange。

2.什么是服务网关

 

 

3.SpringCloud Gateway怎么用

   3.1 引入依赖(这里springboot版本2.2.8.RELEASE,springboot不能引入web(tomcat)组件

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3.2 YML配置

server:
  port: 7001

spring:
  application:
    name: gateway-server
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        namespace: e8c32ac0-391b-44dd-94bf-bd23ebaf5aca
    gateway:
      routes:
        - id: member
          uri: http://localhost:9005/
          predicates:
            - Path=/member/**

4.SpringCloud Gateway路由规则

4.1 Path

    gateway:
      routes:
        - id: member
          uri: http://localhost:9005/
          predicates:
            - Path=/member/**

4.2  Query

 

 4.3 Method

 

 4.4 DateTime

 

 4.5 RemoteAddr

 

4.6 Header

 

5.SpringCloud Gateway动态路由(服务发现的路由--和注册中心搭配用)

5.1 动态获取URI

    gateway:
      routes:
        - id: member
          uri: lb://member    #lb代表负载均衡   member为注册中心上的服务名
          predicates:
            - Path=/member/**

5.2 服务名称转发

gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true

这样访问:http://localhost:7001/oneserver-service/oneserver/get/demo     oneserver-service为服务在注册中心上的服务名

 

 

 

6.SpringCloud Gateway  过滤器

  6.1 网关过滤器

 

 

  6.2 全局过滤器

 

 6.2.1 自定义全局过滤器

package com.zdyl.gateway.filters;

import com.zdyl.gateway.common.utils.Constant;
import com.zdyl.gateway.config.WebserverConfig;
import com.zdyl.gateway.dto.SysUserEntityDTO;
import com.zdyl.gateway.dto.SysUserTokenEntityDTO;
import com.zdyl.gateway.utils.RedisUtil;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 全局过滤器
 */
@Configuration
public class CustomerFilter implements GlobalFilter, Ordered {

    @Resource
    private RedisUtil redisUtils;
    @Resource
    private WebserverConfig webserverConfig;


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String requestUrl = request.getPath().toString();
      //指定地址不需要验证
if (contains(requestUrl)) { return chain.filter(exchange); } else { String token = getRequestToken(request); if (org.apache.commons.lang.StringUtils.isEmpty(token)) { response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } String tokenDto = redisUtils.getString("token:" + token, 0); if (tokenDto == null) { response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } SysUserTokenEntityDTO sysUserTokenEntityDTO = redisUtils.fromJson(tokenDto, SysUserTokenEntityDTO.class); Date loginTime = sysUserTokenEntityDTO.getExpireTime(); long exitTime = loginTime.getTime() / 1000; long nowTime = new Date().getTime() / 1000; if (nowTime > exitTime) { response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } String currentUser = redisUtils.getString("user:" + sysUserTokenEntityDTO.getUserId(), 0); if (currentUser == null) { response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } SysUserEntityDTO sysUserEntityDTO = redisUtils.fromJson(currentUser, SysUserEntityDTO.class); //设置用户信息到请求,注意,这里是追加头部信息,token信息已经有了 ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(Constant.CURRENTUSER, currentUser).build(); exchange = exchange.mutate().request(mutableReq).build(); }
      //跨域设置 HttpHeaders headers
= response.getHeaders(); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST,GET,PUT,DELETE"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*"); headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*"); return chain.filter(exchange); } @Override public int getOrder() { return 0; } /** * 获取请求的token */ private String getRequestToken(ServerHttpRequest httpRequest) { //从header中获取token String token = null; token = httpRequest.getHeaders().getFirst("token"); //如果header中不存在token,则从参数中获取token if (StringUtils.isEmpty(token)) { token = httpRequest.getQueryParams().getFirst("token"); } return token; } private boolean contains(String requestUrl) { List<String> ignoreUrlList = webserverConfig.getIgnoreUrlList(); for (String string : ignoreUrlList) { if (requestUrl.contains(string)) { return true; } } return false; } }

 

 6.2.2 完整配置

spring:
    gateway:
      routes:
        - id: oneserver-service
          uri: lb://oneserver-service    #lb\u4EE3\u8868\u8D1F\u8F7D\u5747\u8861   member\u4E3A\u6CE8\u518C\u4E2D\u5FC3\u4E0A\u7684\u670D\u52A1\u540D
          predicates:
            - Path=/oneserver/**
          filters:
            - StripPrefix=1
            # \u9650\u6D41\u8FC7\u6EE4\u5668\uFF0C\u4F7F\u7528gateway\u5185\u7F6E\u4EE4\u724C\u7B97\u6CD5
            - name: RequestRateLimiter
              args:
                # \u4EE4\u724C\u6876\u6BCF\u79D2\u586B\u5145\u5E73\u5747\u901F\u7387,\u5373\u884C\u7B49\u4EF7\u4E8E\u5141\u8BB8\u7528\u6237\u6BCF\u79D2\u5904\u7406\u591A\u5C11\u4E2A\u8BF7\u6C42\u5E73\u5747\u6570
                redis-rate-limiter.replenishRate: 1
                # \u4EE4\u724C\u6876\u7684\u5BB9\u91CF\uFF0C\u5141\u8BB8\u5728\u4E00\u79D2\u949F\u5185\u5B8C\u6210\u7684\u6700\u5927\u8BF7\u6C42\u6570
                redis-rate-limiter.burstCapacity: 8
                # \u7528\u4E8E\u9650\u6D41\u7684\u952E\u7684\u89E3\u6790\u5668\u7684 Bean \u5BF9\u8C61\u7684\u540D\u5B57\u3002\u5B83\u4F7F\u7528 SpEL \u8868\u8FBE\u5F0F\u6839\u636E#{@beanName}\u4ECE Spring \u5BB9\u5668\u4E2D\u83B7\u53D6 Bean \u5BF9\u8C61\u3002
                key-resolver: "#{@apiKeyResolver}"
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true

 

posted @ 2021-06-02 11:57  william_zhao  阅读(316)  评论(0编辑  收藏  举报