【Gateway】01-Gateway与SpringCloud

Gateway 网关

概述

简介

网关Gateway是Zuul1.0的替代版。是在Spring生态系统之上构建的API网关服务,基于Spring5,Spring Boot 2和Project Reactor等技术。

Gateway旨在提供一种简单而有效的方式对API进路由,以及提供一些强大的过滤功能,例如:熔断、限流、重试等。

特性

  • 基于 Spring Framework 5、Project Reactor 和 Spring Boot 2.0
  • 能够匹配任何请求属性的路由。
  • 谓词和过滤器特定于路由。
  • 断路器集成。
  • Spring Cloud DiscoveryClient 集成
  • 易于编写谓词和过滤器
  • 请求速率限制
  • 路径重写

工作原理

客户端向 Spring Cloud Gateway 发出请求。如果网关处理程序映射确定请求与路由匹配,则将其发送到网关 Web 处理程序。此处理程序通过特定于请求的过滤器链运行请求。过滤器被虚线分隔的原因是过滤器可以在发送代理请求之前和之后运行逻辑。执行所有“pre”过滤器逻辑。然后进行代理请求。发出代理请求后,将运行“post”过滤器逻辑。

img-01

代码及使用

项目代码

  1. 项目目录结构如下

  2. 编写代码:

    1. pom.xml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      <dependencies>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
      </dependency>
      </dependencies>
    2. 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
      server:
      port: 8041
      spring:
      application:
      name: cloud-gateway
      cloud:
      gateway:
      routes:
      - id: qq
      uri: https://www.qq.com
      predicates:
      - Query=qq
      - id: baidu
      uri: https://www.baidu.com
      predicates:
      - Path=/baidu
      - id: cloud-consumer
      uri: http://127.0.0.1:8021/
      predicates:
      - Path=/consumer/**
      # - Cookie=username, zsq # curl http://localhost:8041/consumer/ok/1 --cookie username=zsq
      # - Header=X-Request-Id, \d+ # curl http://localhost:8041/consumer/ok/1 --cookie username=zsq -H "X-Request-Id:23"
      - id: cloud-feign
      uri: lb://CLOUD-HYSTRIX-PAYMENT-FEIGN
      predicates:
      - Path=/feign/**
      filters:
      - RewritePath=/feign/(?<segment>.*), /$\{segment} # http://localhost:8041/feign/consumer/ok/1 的实际访问路径为 http://localhost:8021/consumer/ok/1
      eureka:
      client:
      fetch-registry: true
      register-with-eureka: true
      service-url:
      defaultZone: http://localhost:7001/eureka
    3. GatewayMain7041.java

      1
      2
      3
      4
      5
      6
      7
      8
      9
      @EnableEurekaClient
      @SpringBootApplication
      public class GatewayMain8041 {

      public static void main(String[] args) {
      SpringApplication.run(GatewayMain8041.class);
      }

      }
  3. 启动即可

自定义全局过滤器

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
@Slf4j
@Component
public class MyLogGatewayFilter implements GlobalFilter, Ordered {

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("MyLogGatewayFilter.filter() - " + new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if (StrUtil.isBlank(uname)) {
log.info("用户名为空!!");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}

/**
* 过滤器链的优先级
* @return
*/
@Override
public int getOrder() {
return 0;
}

}

总结

-------------本文结束感谢您的阅读-------------