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

转载: http://www.springcloud.cn/view/256

摘要:本篇文章主要介绍了Spring Cloud Gateway的基于服务发现的默认路由规则,从中可以看出Gateway的路由规则:http://Gateway_HOST:Gateway_PORT/大写的serviceId/* 和 zuul的默认路由规则http://ZUUL_HOST:ZUUL_PORT/微服务在Eureka上的serviceId/* 差不多。

原文链接:http://xujin.org/sc/gw/gw05/

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://localhost:9000/SC-CONSUMER/hello/xujin

http://Gateway_HOST:Gateway_PORT/大写的serviceId/**,其中微服务应用名默认大写访问。

3.2 案例示例代码

https://github.com/SoftwareKing/sc-gateway/tree/master/ch1

<img src="http://springcloud-new.oss-cn-shenzhen.aliyuncs.com/74473f8fa112dbf946452dd234be9783.jpeg?Expires=1841935730&OSSAccessKeyId=LTAI57F52hRuWq3h&Signature=8hk%2BcIs0lpEA3S01TQsu%2BVdome8%3D" width="450px" height="252px">

模块说明端口
ch1-sc-consumer服务消费者8000
ch1-sc-eurekaEureka Server注册中心8761
ch1-sc-gatewaySpring Cloud Gateway Sever9000
ch1-sc-provider服务提供者8001

3.2.1 ch1-sc-gateway工程说明

3.2.1.1 Maven依赖

Spring Cloud Gateway sever主要的maven依赖如下所示

 
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-gateway</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10. </dependencies>

3.2.1.2 yml文件配置

 
  1. spring:
  2. application:
  3. name: sc-gateway-server
  4. cloud:
  5. gateway:
  6. discovery:
  7. locator:
  8. enabled: true
  9.  
  10. server:
  11. port: 9000
  12. eureka:
  13. client:
  14. service-url:
  15. defaultZone: http://localhost:8761/eureka/
  16.  
  17. logging:
  18. level:
  19. org.springframework.cloud.gateway: debug

配置说明:

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


修改spring cloud gateway server监听的端口为9000


eureka.client.service-url.defaultZone: http://localhost:8761/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:9000/sc-consumer/hello/xujin的时候,报错如下所示,正确的Spring Cloud Gateway的默认路由规则:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**

 
  1. 2018-05-18 01:10:49.742 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-CONSUMER applying {pattern=/SC-CONSUMER/**} to Path
  2. 2018-05-18 01:10:49.743 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-CONSUMER applying filter {regexp=/SC-CONSUMER/(?<remaining>.*), replacement=/${remaining}} to RewritePath
  3. 2018-05-18 01:10:49.743 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: CompositeDiscoveryClient_SC-CONSUMER
  4. 2018-05-18 01:10:49.744 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-PRODUCER applying {pattern=/SC-PRODUCER/**} to Path
  5. 2018-05-18 01:10:49.744 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-PRODUCER applying filter {regexp=/SC-PRODUCER/(?<remaining>.*), replacement=/${remaining}} to RewritePath
  6. 2018-05-18 01:10:49.745 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: CompositeDiscoveryClient_SC-PRODUCER
  7. 2018-05-18 01:10:49.745 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-GATEWAY-SERVER applying {pattern=/SC-GATEWAY-SERVER/**} to Path
  8. 2018-05-18 01:10:49.747 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-GATEWAY-SERVER applying filter {regexp=/SC-GATEWAY-SERVER/(?<remaining>.*), replacement=/${remaining}} to RewritePath
  9. 2018-05-18 01:10:49.748 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: CompositeDiscoveryClient_SC-GATEWAY-SERVER
  10. 2018-05-18 01:10:49.748 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.h.RoutePredicateHandlerMapping : Route matched: CompositeDiscoveryClient_SC-CONSUMER
  11. 2018-05-18 01:10:49.749 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.h.RoutePredicateHandlerMapping : Mapping [Exchange: GET http://localhost:9000/SC-CONSUMER/hello/xujin] to Route{id='CompositeDiscoveryClient_SC-CONSUMER', uri=lb://SC-CONSUMER, order=0, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$707/751096818@7f4c6373, gatewayFilters=[OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$709/672603106@293895d2, order=1}]}
  12. 2018-05-18 01:10:49.749 DEBUG 6462 --- [ctor-http-nio-5] o.s.c.g.handler.FilteringWebHandler : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@5e85c21b}, order=-1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$709/672603106@293895d2, order=1}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@38e83838}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@6ef2f7ad}, order=10100}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@41def031}, order=2147483637}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@4966bab1}, order=2147483646}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@22d477c2}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@39832280}, order=2147483647}]

从上面的log,看到返回了 404 错误,进一步可以看到 Spring Cloud Gateway 已经为我们的 provider 和 consumer 自动创建了对应的路由转发规则,但是这里的 pattern/regexp 里都是大写的,下面换成大写的测试一下。

3.3.2 Gateway正确的路由规则测试

访问正确的http://localhost:9000/SC-CONSUMER/hello/xujin,可以成功访问。

 
  1. 2018-05-22 09:04:21.204 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-CONSUMER applying {pattern=/SC-CONSUMER/**} to Path
  2. 2018-05-22 09:04:21.205 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-CONSUMER applying filter {regexp=/SC-CONSUMER/(?<remaining>.*), replacement=/${remaining}} to RewritePath
  3. 2018-05-22 09:04:21.205 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: CompositeDiscoveryClient_SC-CONSUMER
  4. 2018-05-22 09:04:21.206 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-PRODUCER applying {pattern=/SC-PRODUCER/**} to Path
  5. 2018-05-22 09:04:21.207 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-PRODUCER applying filter {regexp=/SC-PRODUCER/(?<remaining>.*), replacement=/${remaining}} to RewritePath
  6. 2018-05-22 09:04:21.207 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: CompositeDiscoveryClient_SC-PRODUCER
  7. 2018-05-22 09:04:21.208 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-GATEWAY-SERVER applying {pattern=/SC-GATEWAY-SERVER/**} to Path
  8. 2018-05-22 09:04:21.208 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition CompositeDiscoveryClient_SC-GATEWAY-SERVER applying filter {regexp=/SC-GATEWAY-SERVER/(?<remaining>.*), replacement=/${remaining}} to RewritePath
  9. 2018-05-22 09:04:21.209 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: CompositeDiscoveryClient_SC-GATEWAY-SERVER
  10. 2018-05-22 09:04:21.209 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : Route matched: CompositeDiscoveryClient_SC-CONSUMER
  11. 2018-05-22 09:04:21.209 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : Mapping [Exchange: GET http://localhost:9000/SC-CONSUMER/hello/xujin] to Route{id='CompositeDiscoveryClient_SC-CONSUMER', uri=lb://SC-CONSUMER, order=0, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$706/57023854@24f1a91e, gatewayFilters=[OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$708/2036079541@cbb7393, order=1}]}
  12. 2018-05-22 09:04:21.209 DEBUG 1677 --- [ctor-http-nio-2] o.s.c.g.handler.FilteringWebHandler : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@29a98d9f}, order=-1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$708/2036079541@cbb7393, order=1}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@544e8149}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@55d58825}, order=10100}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@2da3b078}, order=2147483637}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@1a96d94c}, order=2147483646}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@19a64eae}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@7fb66650}, order=2147483647}]

可以看出,Spring Cloud Gateway 自动的为我们的 consumer 创建了一个路由,类似于下边这样

 
  1. routes:
  2. - id: CompositeDiscoveryClient_SC-CONSUMER
  3. uri: lb://SC-CONSUMER
  4. order: 0
  5. predicates:
  6. - Path=/SC-CONSUMER/**
  7. filters:
  8. - RewritePath=/SC-CONSUMER/(?<segment>.*), /$\{segment}

所以从zuul迁移到gateway的时候,服务路由规则中的微服务应用Id默认从小写变为大写。

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要基于Nacos实现Spring Cloud Gateway的动态网关路由,可以按照以下步骤进行操作: 1. 添加依赖:在Spring Cloud Gateway项目的pom.xml文件中添加相应的依赖,包括spring-cloud-starter-gatewayspring-cloud-starter-alibaba-nacos-discovery等。 2. 配置Nacos注册中心:在application.properties或application.yml配置文件中添加Nacos注册中心的相关配置,包括Nacos服务器地址、命名空间、分组等信息。 3. 配置动态路由:创建一个RouteLocator Bean,并在其中使用Nacos的服务发现来定义动态路由规则。可以通过Nacos的配置中心来管理路由规则的动态更新。 ```java @Configuration public class GatewayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("service_route", r -> r.path("/api/v1/**") .uri("lb://service-provider")) .build(); } } ``` 上述示例中,定义了一个名为service_route的路由规则,将请求路径以/api/v1/开头的请求转发到名为service-provider的微服务上。 4. 启动Gateway应用:启动Spring Cloud Gateway应用,它会自动从Nacos注册中心获取动态路由规则并进行路由转发。 5. 管理动态路由:使用Nacos的配置中心来管理动态路由规则。可以通过Nacos的控制台或API来添加、修改或删除路由规则Gateway应用会自动更新并生效。 通过以上步骤,就可以基于Nacos实现Spring Cloud Gateway的动态网关路由了。你可以根据实际需求和业务场景,添加更多的路由规则配置。希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值