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

发布时间:2020-09-29 12:56:02 作者:lockie_zou
来源:脚本之家 阅读:608

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,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

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

1.2 Spring Cloud Gateway的功能

Spring Cloud Gateway 的特征:

2. Spring Cloud Gateway的工程流程

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

客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。
过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。

2.1 Pre和POST两种类型的过滤器

3.基于服务发现的默认路由规则

3.1 zuul和gateway的默认路由规则

3.1.1 zuul的默认路由规则

说明默认情况下,Zuul会代理所有注册到Eureka Server的微服务,并且Zuul的路由规则如下:

http://ZUUL_HOST:ZUUL_PORT/微服务在Eureka上的serviceId/** 会被转发到serviceId对应的微服务。

http://localhost:8040/sc-zuul-first-provider/sc/order/2

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

3.1.2 gateway的默认路由规则

规则:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**

其中微服务应用名默认大写访问。

实例代码:

模块 说明 端口
eureka-service Eureka Server注册中心 5000
gateway-service Spring Cloud Gateway Sever 5001
order-service 服务提供者 5100
user-service 服务消费者 5200

分别新建上面这四个服务,详见 spring cloud Finchley环境搭建

其中gateway-service服务的application.yml配置文件如下:

spring:
 application:
 name: gateway-service
 cloud:  # spring cloud gateway 路由配置方式
 gateway:
  discovery:  #是否与服务发现组件进行结合,通过 serviceId(必须设置成大写) 转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。
  locator:  #路由访问方式:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**,其中微服务应用名默认大写访问。
   enabled: true
  routes:
  - id: 163      #网关路由到网易官网
  uri: http://www.163.com/
  predicates:
   - Path=/163/**
#  - id: ORDER-SERVICE   #网关路由到订单服务order-service
#  uri: lb://ORDER-SERVICE
#  predicates:
#   - Path=/ORDER-SERVICE/**
#  - id: USER-SERVICE   #网关路由到用户服务user-service
#  uri: lb://USER-SERVICE
#  predicates:
#   - Pach=/USER-SERVICE/**

server:
 port: 5001


logging:
 level:
 org.springframework.cloud.gateway: trace
 org.springframework.http.server.reactive: debug
 org.springframework.web.reactive: debug
 reactor.ipc.netty: debug


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

feign:
 hystrix:
 enabled: true

配置项说明:

spring.cloud.gateway.discovery.locator.enabled:是否与服务发现组件进行结合,通过 serviceId 转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。

eureka.client.service-url.defaultZone: http://localhost:5000/eureka/,指定注册中心的地址,Spring Cloud Gateway从注册中心获取已经注册的服务列表。

logging.level.org.springframework.cloud.gateway: debug,开启spring-Cloud-gateway的日志级别为debug,方便debug调试。

3.3 启动测试

3.3.1 错误的路由规则访问

访问Spring Cloud Gateway对应的server,当访问http://localhost:5000/order-service/order/getOrderPort的时候,会出现报错内容如下所示:

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

正确的Spring Cloud Gateway的默认路由规则:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**

3.3.2 Gateway正确的路由规则测试

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

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

推荐阅读:
  1. Spring Cloud Gateway - 扩展
  2. 第二代微服务网关组件 - Spring Cloud Gateway

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring cloud gateway

上一篇:Node.js使用orm2进行update操作时关联字段无法修改的解决方法

下一篇:JS中判断某个字符串是否包含另一个字符串的五种方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》