Spring Cloud Gateway基于服务发现的默认路由规则
1.Spring Gateway概述1.1 什么是Spring Cloud GatewaySpring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gatewa...
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链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。
1.2 Spring Cloud Gateway的功能
Spring Cloud Gateway 的特征:
- 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
动态路由 - Predicates 和 Filters 作用于特定路由
- 集成 Hystrix 断路器
- 集成 Spring Cloud DiscoveryClient
- 易于编写的 Predicates 和 Filters
- 限流
- 路径重写
2. 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
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 |
其中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的默认路由规则:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**
3.3.2 Gateway正确的路由规则测试
注:内容来源点击打开链接
更多推荐
所有评论(0)