Gateway网关

作者: o_O小薯条 | 来源:发表于2021-01-18 17:18 被阅读0次

网关概述

  • 网关旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。
  • 在微服务架构中, 不同的微服务可以有不同的网络地址,各个微服务之间通过相互调用完成用户请求,客户端可能通过调用N个微服务的接口完成一个用户请求。
  • 存在的问题:
  • 客户端多次请求不同的微服务,增加客户端的复杂性
  • 认证复杂,每个服务都要进行认证
  • http求情不同服务次数增加,性能不高
  • 网关就是系统的入口,封装了应用程序的内部结构,为客户端提供统一服务,一些与业务本身功能无关的公共逻辑可以在这里实现,诸如认证、鉴权、监控、缓存、负载均衡、流量监控、路由转发等。

Gateway 网关快速入门

1. 搭建网关模块

2. 引入依赖:starter-gateway

<dependencies>
        <!--引入gateway网关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

3. 编写启动类

package com.itheima.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayApp {
    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApp.class, args);
    }
}

4. 编写配置文件

server:
  port: 80

spring:
  application:
    name: api-gateway-server

  cloud: #网关配置
    gateway: #路由配置: 转发规则
      routes:  #集合。
      - id: gateway-provider  #ID:唯一标示,默认是一个UUID
        uri: http://localhost:8001/  #uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/goods/**

5. 启动测试

Gateway-1.png

Gateway 网关路由配置 - 静态路由

server:
  port: 80

spring:
  application:
    name: api-gateway-server

  cloud: #网关配置
    gateway: #路由配置: 转发规则
      routes:  #集合。
      - id: gateway-provider  #ID:唯一标示,默认是一个UUID
        uri: http://localhost:8001/  #uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/goods/**

      - id: gateway-consumer  #ID:唯一标示,默认是一个UUID
        uri: http://localhost:9000/  #uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/order/**

Gateway 网关路由配置 - 动态路由

  • 引入eureka-client配置
  • 修改uri属性:uri:lb://服务名称
server:
  port: 80

spring:
  application:
    name: api-gateway-server

  cloud: #网关配置
    gateway: #路由配置: 转发规则
      routes:  #集合。
      - id: gateway-provider  #ID:唯一标示,默认是一个UUID
        uri: lb://GATEWAY-PROVIDER #动态路由 #uri: http://localhost:8001/  #静态路由uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/goods/**

      - id: gateway-consumer  #ID:唯一标示,默认是一个UUID
        uri: lb://GATEWAY-CONSUMER #动态路由 #静态路由uri: http://localhost:9000/  #uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/order/**


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

Gateway 网关路由配置 - 微服务名称配置

server:
  port: 80

spring:
  application:
    name: api-gateway-server

  cloud: #网关配置
    gateway: #路由配置: 转发规则
      routes:  #集合。
      - id: gateway-provider  #ID:唯一标示,默认是一个UUID
        uri: lb://GATEWAY-PROVIDER #动态路由 #uri: http://localhost:8001/  #静态路由uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/goods/**

      - id: gateway-consumer  #ID:唯一标示,默认是一个UUID
        uri: lb://GATEWAY-CONSUMER #动态路由 #静态路由uri: http://localhost:9000/  #uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/order/**
      discovery: #微服务名称配置
        locator:
          enabled: true #设置为true 请求路径前可以添加微服务名称
          lower-case-service-id: true #允许为小写
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

Gateway 过滤器

  • Gateway支持过滤器功能,怼请求或响应进行拦截,完成一些通用操作。


    Gateway-1.png
  • Gateway 提供两种过滤方式:“pre” 和 “post”
  • pre过滤器,在转发之前执行,可以做参数校验、权限校验、流量监控、日志输出、协议转换等。
  • post过滤器,在响应之前执行,可以做响应内容、响应头的修改、日志的输出、流量监控等。
  • Gateway还提供了两种类型过滤器
  • GatewayFilter:局部过滤器,针对单个路由
  • GlobalFilter:全局过滤器,针对所有路由

Gateway过滤器 - 局部过滤器

  • GatewayFilter局部过滤器,是针对单个路由的过滤器。
  • 在SpringCloudGateway组件中提供了大量内置的局部过滤器,对请求和响应坐过滤操作。
  • 遵循约定大于配置的思想,只需要在配置文件配置局部过滤器名称,并为其制定对应的值。就可以让其生效。
GatewayFilter-1.png
server:
  port: 80

spring:
  application:
    name: api-gateway-server

  cloud: #网关配置
    gateway: #路由配置: 转发规则
      routes:  #集合。
      - id: gateway-provider  #ID:唯一标示,默认是一个UUID
        uri: lb://GATEWAY-PROVIDER #动态路由 #uri: http://localhost:8001/  #静态路由uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/goods/**
        filter:
          - AddRequestParameter=username,zhangsan #filters过滤器:配置局部过滤器

      - id: gateway-consumer  #ID:唯一标示,默认是一个UUID
        uri: lb://GATEWAY-CONSUMER #动态路由 #静态路由uri: http://localhost:9000/  #uri:转发路径
        predicates:  #predicates: 条件,用于请求网关路径的匹配规则
          - path=/order/**
      discovery: #微服务名称配置
        locator:
          enabled: true #设置为true 请求路径前可以添加微服务名称
          lower-case-service-id: true #允许为小写
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

Gateway过滤器 - 全局过滤器

  • GlobaFilter 全局过滤器,不需要在配置文件中配置,系统初始化时加载,并作用在每个路由上。

  • SpringCloud Gateway核心的功能也是通过内置的全局过滤器来完成。


    GlobaFilter-1.png
  • 自定义全局过滤器步骤:

  1. 定义类实现GlobalFilter和Ordered接口
  2. 复写方法
  3. 完成逻辑处理
package com.itheima.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class MyFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("自定义全局过滤器执行了。。。");
        return chain.filter(exchange); //放行
    }

    /**
     * 过滤器排序
     * @return 数值越小 越先执行
     */
    @Override
    public int getOrder() {
        return 0;
    }
}

相关文章

网友评论

    本文标题:Gateway网关

    本文链接:https://www.haomeiwen.com/subject/wllwaktx.html