Spring cloud gateway工作流程原理解析

spring cloud gateway的包结构(在Idea 2019.3中展示)

这个包是spring-cloud-gateway-core.这里是真正的spring-gateway的实现的地方.

为了证明,我们打开spring-cloud-starter-gateway的pom文件

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-gateway-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
  </dependencies>

除了cloud-start,starter-webflux就是cloud-gateway-core.所以后面我们就分析

cloud-gateway-core这个jar包.

其中actuate中定义了GatewayControllerEndpoint,它提供了对外访问的接口.

  // TODO: Flush out routes without a definition
  @GetMapping("/routes")
  public Flux<Map<String, Object>> routes() {
    return this.routeLocator.getRoutes().map(this::serialize);
  }

  @GetMapping("/routes/{id}")
  public Mono<ResponseEntity<Map<String, Object>>> route(@PathVariable String id) {
    //......
  }
//以下方法是继承于父类,抽象类AbstractGatewayControllerEndpoint
  @PostMapping("/refresh")
  public Mono<Void> refresh() {
    this.publisher.publishEvent(new RefreshRoutesEvent(this));
    return Mono.empty();
  }

  @GetMapping("/globalfilters")
  public Mono<HashMap<String, Object>> globalfilters() {
    return getNamesToOrders(this.globalFilters);
  }

  @GetMapping("/routefilters")
  public Mono<HashMap<String, Object>> routefilers() {
    return getNamesToOrders(this.GatewayFilters);
  }

  @GetMapping("/routepredicates")
  public Mono<HashMap<String, Object>> routepredicates() {
    return getNamesToOrders(this.routePredicates);
  }
  @PostMapping("/routes/{id}")
  @SuppressWarnings("unchecked")
  public Mono<ResponseEntity<Object>> save(@PathVariable String id,
      @RequestBody RouteDefinition route) {}
  @DeleteMapping("/routes/{id}")
  public Mono<ResponseEntity<Object>> delete(@PathVariable String id) {
    return this.routeDefinitionWriter.delete(Mono.just(id))
        .then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build())))
        .onErrorResume(t -> t instanceof NotFoundException,
            t -> Mono.just(ResponseEntity.notFound().build()));
  }

  @GetMapping("/routes/{id}/combinedfilters")
  public Mono<HashMap<String, Object>> combinedfilters(@PathVariable String id) {
    // TODO: missing global filters
  }

config包里定义了一些Autoconfiguration和一些properties.读取配置文件就在这里完成.

我们这里看一下GatewayProperties.java

@ConfigurationProperties("spring.cloud.gateway")
@Validated
public class GatewayProperties {

  /**
   * List of Routes.
   */
  @NotNull
  @Valid
  private List<RouteDefinition> routes = new ArrayList<>();

  /**
   * List of filter definitions that are applied to every route.
   */
  private List<FilterDefinition> defaultFilters = new ArrayList<>();

  private List<MediaType> streamingMediaTypes = Arrays
      .asList(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_STREAM_JSON);
  #该类包括三个属性,路由列表,默认过滤器列表和MediaType列表.路由列表中的路由定义RouteDefinition.
    过滤器中定义的FilterDefinition.

discovery定义了注册中心的一些操作.

event定义了一系列事件,都继承自ApplicationEvent.

filter定义了spring gateway实现的一些过滤器,包括gatewayfilter,globalfilter.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • spring cloud gateway网关路由分配代码实例解析

    这篇文章主要介绍了spring cloud gateway网关路由分配代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1, 基于父工程,新建一个模块 2,pom文件添加依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-

  • spring cloud gateway整合sentinel实现网关限流

    这篇文章主要介绍了spring cloud gateway整合sentinel实现网关限流,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 说明: sentinel可以作为各微服务的限流,也可以作为gateway网关的限流组件. spring cloud gateway有限流功能,但此处用sentinel来作为替待. 说明:sentinel流控可以放在gateway网关端,也可以放在各微服务端. 1,以父工程为基础,创建子工程 2,添加pom依赖

  • spring cloud gateway请求跨域问题解决方案

    这篇文章主要介绍了spring cloud gateway请求跨域问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 @Configuration public class CorsConfig implements GlobalFilter, Ordered { private static final String ALL = "*"; private static final String MAX_AGE =

  • Spring Cloud Gateway使用Token验证详解

    引入依赖 <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <ty

  • springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

    前段时间spring boot 2.0发布了,与之对应的spring cloud Finchley版本也随之而来了,两者之间的关系和版本对应详见我这边文章:spring boot和spring cloud对应的版本关系 项目地址:spring-cloud-demo spring boot 1.x和spring cloud Dalston和Edgware版本搭建的微服务项目现在已经很流行了,现在很多企业都已经在用了,这里就不多说了. 使用版本说明: spring boot 2.0.x spring

  • 创建网关项目(Spring Cloud Gateway)过程详解

    创建网关项目 加入网关后微服务的架构图 创建项目 POM文件 <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springfram

  • 详解Spring Cloud Gateway基于服务发现的默认路由规则

    1.Spring Gateway概述 1.1 什么是Spring Cloud Gateway Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式.Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由

  • SpringCloud Gateway跨域配置代码实例

    这篇文章主要介绍了SpringCloud Gateway跨域配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Springboot版本:2.1.8.RELEASE SpringCloud版本:Greenwich.SR2 yml配置: spring: cloud: gateway: globalcors: cors-configurations: '[/**]': # 允许携带认证信息 # 允许跨域的源(网站域名/ip),设置*为全部

  • Spring cloud gateway工作流程原理解析

    spring cloud gateway的包结构(在Idea 2019.3中展示) 这个包是spring-cloud-gateway-core.这里是真正的spring-gateway的实现的地方. 为了证明,我们打开spring-cloud-starter-gateway的pom文件 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId

  • Spring Cloud Gateway重试机制原理解析

    重试,我相信大家并不陌生.在我们调用Http接口的时候,总会因为某种原因调用失败,这个时候我们可以通过重试的方式,来重新请求接口. 生活中这样的事例很多,比如打电话,对方正在通话中啊,信号不好啊等等原因,你总会打不通,当你第一次没打通之后,你会打第二次,第三次-第四次就通了. 重试也要注意应用场景,读数据的接口比较适合重试的场景,写数据的接口就需要注意接口的幂等性了.还有就是重试次数如果太多的话会导致请求量加倍,给后端造成更大的压力,设置合理的重试机制才是最关键的. 今天我们来简单的了解下Spr

  • spring cloud Ribbon用法及原理解析

    这篇文章主要介绍了spring cloud Ribbon用法及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 简介 这篇文章主要介绍一下ribbon在程序中的基本使用,在这里是单独拿出来写用例测试的,实际生产一般是配置feign一起使用,更加方便开发.同时这里也通过源码来简单分析一下ribbon的基本实现原理. 基本使用 这里使用基于zookeeper注册中心+ribbon的方式实现一个简单的客户端负载均衡案例. 服务提供方 首先是一个

  • Spring Cloud Ribbon的使用原理解析

    目录 一.概述 1.Ribbon是什么 2.Ribbon能干什么 3.Ribbon现状 4.未来替代方案 5.架构说明 二.RestTemplate 用法详解 三.Ribbon核心组件IRule 四.实战项目 1.回顾之前的项目 2.@RibbonClient注解用法 3.配置文件用法 3.测试 五.Ribbon原理 1.负载均衡算法 2.源码分析 六.手写负载均衡器 一.概述 1.Ribbon是什么 Ribbon是Netflix发布的开源项目,Spring Cloud Ribbon是基于Net

  • spring cloud gateway 限流的实现与原理

    在高并发的系统中,往往需要在系统中做限流,一方面是为了防止大量的请求使服务器过载,导致服务不可用,另一方面是为了防止网络攻击. 常见的限流方式,比如Hystrix适用线程池隔离,超过线程池的负载,走熔断的逻辑.在一般应用服务器中,比如tomcat容器也是通过限制它的线程数来控制并发的:也有通过时间窗口的平均速度来控制流量.常见的限流纬度有比如通过Ip来限流.通过uri来限流.通过用户访问频次来限流. 一般限流都是在网关这一层做,比如Nginx.Openresty.kong.zuul.Spring

  • Spring Cloud Gateway 服务网关快速实现解析

    Spring Cloud Gateway 服务网关 API 主流网关有NGINX.ZUUL.Spring Cloud Gateway.Linkerd等:Spring Cloud Gateway构建于 Spring 5+,基于 Spring Boot 2.x 响应式的.非阻塞式的 API.同时,它支持 websockets,和 Spring 框架紧密集成,用来代替服务网关Zuul,开发体验相对来说十分不错. Spring Cloud Gateway 是 Spring Cloud 微服务平台的一个子

  • Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)的过程解析

    目录 1.漏洞描述 2.影响版本 3.漏洞环境搭建 4.漏洞复现 5.修复方案 1.漏洞描述 Spring Cloud Gateway 是基于 Spring Framework 和 Spring Boot 构建的 API 网关,它旨在为微服务架构提供一种简单.有效.统一的 API 路由管理方式. Spring官方博客发布了一篇关于Spring Cloud Gateway的CVE报告,据公告描述,当启用和暴露 Gateway Actuator 端点时,使用 Spring Cloud Gateway

  • Spring Cloud Gateway整合sentinel 实现流控熔断的问题

    目录 一.什么是网关限流: 二.gateway 整合 sentinel 实现网关限流: 三.sentinel 网关流控规则的介绍: 3.1.网关流控规则: 3.2.API 分组管理: 四.sentinel 网关流控实现的原理: 五.网关限流了,服务就安全了吗? 六.自定义流控异常消息: 一.什么是网关限流: 在微服务架构中,网关层可以屏蔽外部服务直接对内部服务进行调用,对内部服务起到隔离保护的作用,网关限流,顾名思义,就是通过网关层对服务进行限流,从而达到保护后端服务的作用. Sentinel

  • spring boot jar的启动原理解析

     1.前言 近来有空对公司的open api平台进行了些优化,然后在打出jar包的时候,突然想到以前都是对spring boot使用很熟练,但是从来都不知道spring boot打出的jar的启动原理,然后这回将jar解开了看了下,与想象中确实大不一样,以下就是对解压出来的jar的完整分析. 2.jar的结构 spring boot的应用程序就不贴出来了,一个较简单的demo打出的结构都是类似,另外我采用的spring boot的版本为1.4.1.RELEASE网上有另外一篇文章对spring

随机推荐