SpringCloud Gateway使用和配置,SpringCloud Gateway predicates详细配置

SpringCloud Gateway使用和配置,

SpringCloud Gateway routes详细配置,SpringCloud Gateway predicates详细配置SpringCloud Gateway 跨域配置,SpringCloud Gateway 超时配置

================================

©Copyright 蕃薯耀 2021-03-18

https://www.cnblogs.com/fanshuyao/

一、SpringCloud Gateway概述


spring cloud gateway旨在提供一种简单而有效的方法来路由到api,并为它们提供跨领域的关注点,例如:安全性、监视/度量和恢复能力。

客户端向Spring云网关发出请求。如果网关处理程序映射确定请求与路由匹配,则将其发送到网关Web处理程序。此处理程序通过特定于请求的筛选器链运行请求。过滤器被虚线分割的原因是,过滤器可以在代理请求发送之前和之后运行逻辑。执行所有“预”过滤器逻辑。然后发出代理请求。在发出代理请求之后,运行“post”过滤器逻辑。

官方文档地址:

https://docs.spring.io/spring-cloud-gateway/docs/2.2.7.RELEASE/reference/html/

SpringCloud Gateway主要成员:Route(路由):网关的基本构建块。它由一个ID、一个目标URI、一组谓词和一组筛选器定义。如果聚合谓词为true,则匹配路由。Predicate(断言,即路由匹配规则):这是一个Java 8 Function Predicate。输入类型是springframework serverwebexchange。这允许您匹配来自HTTP请求的任何内容,例如头或参数。Filter(过滤器):这些是用特定工厂构建的Spring Framework GatewayFilter的实例。在这里,您可以在发送下游请求之前或之后修改请求和响应。

流程图:

 

二、SpringCloud Gateway使用和配置1、pom.xml引入依赖
主要的包是:spring-cloud-starter-gateway

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.7.RELEASE</version>
</dependency>

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.4.4</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


<!-- Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency. -->
<!-- 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
 -->

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    <version>2.2.7.RELEASE</version>
</dependency>

2、application.properties文件配置

基于Eureka为服务注册中心

server.port=8701

spring.application.name=SPRINGCLOUD-EUREKA-GATEWAY

#gateway路由配置
#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true
#服务路由名小写
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

#设置路由id,没有固定规则,要求唯一,建设配合服务名
spring.cloud.gateway.routes[0].id=GATEWAY-SERVICE
#设置路由的uri,可以是调用的服务名,也可以请求的地址,当predicates匹配成功后,使用该路由的uri进行服务调用
#设置为服务名:lb://SPRINGCLOUD-EUREKA-SERVER
#设置为请求的地址:http://127.0.0.1:8601
#使用lb,有2个微服务,先启动一个,再启动gateway,然后再启动第二个微服务,未自动实现负载均衡;要先启动2个微服务后,再启动gateway,这样才能实现负载均衡
spring.cloud.gateway.routes[0].uri=lb://SPRINGCLOUD
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值