Spring全家桶--SpringCloud(中级),2024年最新收割快手,字节,百度,美团的Offer之旅

1.5 Gateway配置路由的两种方式


写配置类的方式配置路由

package com.caq.cloud.config;

import org.springframework.cloud.gateway.route.RouteLocator;

import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class GateWayConfig {

@Bean

public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {

RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();

routes.route(“path_route_caq”,

r -> r.path(“/guonei”)

.uri(“http://news.baidu.com/guonei”)).build();

return routes.build();

}

}

image-20220325103050027

1.6 GateWay配置动态路由


locator

n. 定位器,探测器

默认情况下Gateway会根据注册中心注册的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能(不写死一个地址)。

启动

  • eureka7001

  • payment8001/8002

YML

需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。

lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri。

server:

port: 9527

spring:

application:

name: cloud-gateway

#############################新增网关配置###########################

cloud:

gateway:

discovery:

locator:

enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由

routes:

  • id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名

#uri: http://localhost:8001 #匹配后提供服务的路由地址

uri: lb://cloud-payment-service #匹配后提供服务的路由地址

predicates:

  • Path=/payment/get/** # 断言,路径相匹配的进行路由

  • id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名

#uri: http://localhost:8001 #匹配后提供服务的路由地址

uri: lb://cloud-payment-service #匹配后提供服务的路由地址

predicates:

  • Path=/payment/lb/** # 断言,路径相匹配的进行路由

####################################################################

eureka:

instance:

hostname: cloud-gateway-service

client: #服务提供者provider注册进eureka服务列表内

service-url:

register-with-eureka: true

fetch-registry: true

defaultZone: http://eureka7001.com:7001/eureka

测试

浏览器输入 - http://localhost:9527/payment/lb

image-20220325162522438

image-20220325162532222

1.7 GateWay常用的Predicate


predicate

美: ['predɪkeɪt] 英: ['predɪkət]

v. 断言;使基于;使以…为依据;表明

adj. 述语的;谓项的

n. 谓语(句子成分,对主语加以陈述,如 John went home 中的 went home)

官方文档

Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。

Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个RoutePredicate工厂可以进行组合。

Spring Cloud Gateway创建Route 对象时,使用RoutePredicateFactory 创建 Predicate对象,Predicate 对象可以赋值给Route。Spring Cloud Gateway包含许多内置的Route Predicate Factories。

所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合,并通过逻辑and。

常用的Route Predicate Factory

  1. The After Route Predicate Factory

  2. The Before Route Predicate Factory

  3. The Between Route Predicate Factory

  4. The Cookie Route Predicate Factory

  5. The Header Route Predicate Factory

  6. The Host Route Predicate Factory

  7. The Method Route Predicate Factory

  8. The Path Route Predicate Factory

  9. The Query Route Predicate Factory

  10. The RemoteAddr Route Predicate Factory

  11. The weight Route Predicate Factory

The After Route Predicate Factory

spring:

cloud:

gateway:

routes:

  • id: after_route

uri: https://example.org

predicates:

这个时间后才能起效

  • After=2017-01-20T17:42:47.789-07:00[America/Denver]

The Between Route Predicate Factory

spring:

cloud:

gateway:

routes:

  • id: between_route

uri: https://example.org

两个时间点之间

predicates:

  • Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

The Cookie Route Predicate Factory

spring:

cloud:

gateway:

routes:

  • id: cookie_route

uri: https://example.org

predicates:

  • Cookie=chocolate, ch.p

测试

该命令相当于发get请求,且没带cookie

curl http://localhost:9527/payment/lb

带cookie的

curl http://localhost:9527/payment/lb --cookie “chocolate=chip”

The Header Route Predicate Factory

spring:

cloud:

gateway:

routes:

  • id: header_route

uri: https://example.org

predicates:

  • Header=X-Request-Id, \d+

测试

带指定请求头的参数的CURL命令

curl http://localhost:9527/payment/lb -H "X-Request-Id:123

总结

Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理。

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值