gateway网关

  • 一、gateWay网关
    • 1.1 gateway简介
    • 1.2 微服务架构
    • 1.3 spring cloud gateway的特性
    • 1.4 与zuul的区别
    • 1.5 zuul 1.x的模型
    • 1. 6 webflux
    • 1.7 流程
  • 二、简单案例
    • 2.1 案例pro
      • 2.2.1 pom
      • 2.2.2 yml
      • 2.2.5 主启动类
    • 2.3 gateWay效果
    • 2.4 路由配置方式
    • 2.5 动态路由
      • 2.5.1 动态路由概念
      • 2.5.2 YML配置
    • 2.6 Predicate
      • 2.6.1 作用
      • 2.6.2 cookie相关
      • 2.6.3 对请求头等其他断言
    • 2.7 filter
      • 2.7.1 官网提供的过滤器
      • 2.7.2 自定义过滤器

一、gateWay网关

1.1 gateway简介

1.2 微服务架构

1.3 spring cloud gateway的特性

1.4 与zuul的区别

1.5 zuul 1.x的模型

1. 6 webflux

1.7 流程


二、简单案例

2.1 案例pro

2.2.1 pom

    <dependencies>><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><version>2.2.3.RELEASE</version></dependency><!--eureka-client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity --><dependency><groupId>com.atguigu.springcloud</groupId><artifactId>cloud-api-commons</artifactId><version>${project.version}</version></dependency><!--        <dependency>--><!--            <groupId>org.springframework.boot</groupId>--><!--            <artifactId>spring-boot-devtools</artifactId>--><!--            <scope>runtime</scope>--><!--            <optional>true</optional>--><!--        </dependency>--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

2.2.2 yml

server:port: 9527spring:application:name: cloud-gatewaycloud:gateway:discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由routes:- id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001          #匹配后提供服务的路由地址predicates:- Path=/payment/get/**         # 断言,路径相匹配的进行路由
eureka:instance:hostname: cloud-gateway-serviceclient: #服务提供者provider注册进eureka服务列表内service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://eureka7001.com:7001/eureka

2.2.3 config

@Configuration
public class configGateway {@Beanpublic RouteLocator customRouteLocator(configGateway routeLocatorBuilder) {RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();routes.route("path_route_atguigu", r -> r.path("/guoji").uri("http://news.baidu.com/guonei")).build();return routes.build();}
}

2.2.5 主启动类

@SpringBootApplication
@EnableEurekaClient
public class gatewayApp {public static void main(String[] args) {SpringApplication.run(gatewayApp.class, args);System.out.println("启动成功");}
}

2.3 gateWay效果

  • 原8001端口的接口,现在9527也可以访问的到

2.4 路由配置方式

  • yaml配置
  • 代码中注入RouterLocator的bean
package com.pack.gateway.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 configGateway {@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();routes.route("path_route_atguigu", r -> r.path("/guoji").uri("http://news.baidu.com/guonei")).build();routes.route("path_route_atguigu", r -> r.path("/guonei").uri("http://news.baidu.com/guoji")).build();return routes.build();}
}
  • 作用
    访问http://localhost:9527/guoj会转到http://news.baidu.com/guonei,但是路由还是http://localhost:9527/guoj

2.5 动态路由

2.5.1 动态路由概念

2.5.2 YML配置

  • 开启动态路由,去掉具体的uri配置
  • uri: lb://CONSUMERORDER-80:配置uri,uri对应eureka中的服务名称,lb是负载均衡的意思,动态路由就是由eureka动态调用多个同名的服务提供者,(轮询…等其他方式)
server:port: 9527spring:application:name: cloud-gatewaycloud:gateway:discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由routes:- id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#          uri: http://localhost:8001          #匹配后提供服务的路由地址uri: lb://CONSUMERORDER-80  #匹配后提供服务的路由地址predicates:- Path=/payment/get/**         # 断言,路径相匹配的进行路由- id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#          uri: http://localhost:8001          #匹配后提供服务的路由地址#lb是负载均衡的意思uri: lb://CONSUMERORDER-80 #匹配后提供服务的路由地址predicates:- Path=/getServiceInfo        # 断言,路径相匹配的进行路由- id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#          uri: http://localhost:8001          #匹配后提供服务的路由地址#uri: lb://cloud-payment-service #匹配后提供服务的路由地址predicates:- Path=/payment/lb/**         # 断言,路径相匹配的进行路由- After=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]#            - Before=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]#            - Between=2020-03-08T10:59:34.102+08:00[Asia/Shanghai] ,  2020-03-08T10:59:34.102+08:00[Asia/Shanghai]#            curl http://localhost:9527/payment/lb --cookie "username=zzyy"#            - Cookie=username,zzyy   #Cookie=cookieName,正则表达式# 请求头要有X-Request-Id属性并且值为整数的正则表达式 curl http://localhost:9527/payment/lb --cookie "username=zzyy" -H "X-Request-Id:11"
#            - Header=X-Request-Id, \d+
#            - Host=**.atguigu.com  # curl http://localhost:9527/payment/lb -H "Host:afae.atguigu.com"eureka:instance:hostname: cloud-gateway-serviceclient: #服务提供者provider注册进eureka服务列表内service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://eureka7001.com:7001/eureka

2.6 Predicate

2.6.1 作用

  • 可以对路由的判定加一些条件,比如路由什么时间生效之类的
  • 简单的说就是对请求添加一些先决的条件,只有通过条件才去进行转发。
  cloud:gateway:discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由routes:- id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#          uri: http://localhost:8001          #匹配后提供服务的路由地址uri: lb://CONSUMERORDER-80  #匹配后提供服务的路由地址predicates:- Path=/payment/get/**         # 断言,路径相匹配的进行路由- After=2022-06-21T20:27:25.987+08:00[Asia/Shanghai]
  • 路由的时间格式可以通过ZonedDateTime获得
public class tets {public static void main(String[] args) {ZonedDateTime now = ZonedDateTime.now();System.out.println(now);}
}

2.6.2 cookie相关

  • predicates中设置请求Cookie的username是zzyy
server:port: 9527spring:application:name: cloud-gatewaycloud:gateway:discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由routes:- id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#          uri: http://localhost:8001          #匹配后提供服务的路由地址uri: lb://CONSUMERORDER-80  #匹配后提供服务的路由地址predicates:- Path=/payment/get/**         # 断言,路径相匹配的进行路由- After=2022-06-21T20:27:25.987+08:00[Asia/Shanghai]- Cookie=username,zzyy
  • 通过cmd模拟请求

2.6.3 对请求头等其他断言

   predicates:- Path=/payment/get/**         # 断言,路径相匹配的进行路由
#            - After=2022-06-21T20:27:25.987+08:00[Asia/Shanghai]
#            - Cookie=username,zzyy- Header=X-Request-Id, \d+ #请求头断言- Host=**.atguigu.com  #Host断言

C:\Users\Administrator>curl http://localhost:9527/payment/get/Cookie -H "X-Request-Id;12"

2.7 filter

2.7.1 官网提供的过滤器

  • 在请求前后对请求进行修改

2.7.2 自定义过滤器

package com.pack.gateway.filter;import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.util.Date;@Component
@Slf4j
class MyLogGateWayFilter implements GlobalFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {log.info("***********come in MyLogGateWayFilter:  " + new Date());// 获取请求参数中的 unameString uname = exchange.getRequest().getQueryParams().getFirst("uname");if (uname == null) {log.info("*******用户名为null,非法用户,o(╥﹏╥)o");exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);return exchange.getResponse().setComplete();}
//合法放行,到新的过滤器,传到新的过滤链return chain.filter(exchange);}/*** 加载过滤器顺序,数字越小优先级越高** @return*/@Overridepublic int getOrder() {return 0;}
}
  • 上述自定义全局过滤器的作用就是没有加uname时中断请求

spring cloud day(6) gateway网关相关推荐

  1. 【Spring Cloud Alibaba】Gateway 服务网关

    [Spring Cloud Alibaba]Gateway 服务网关 1 架构图 2 Predicate 断言 3 路由 3.1 静态路由 3.2 动态路由 3.3 Nacos 配置 4 过滤器 4. ...

  2. Gateway网关简介及使用。Spring Cloud Alibaba---Gateway概述、简单示例。什么是Gataway网关?网关能干什么?Spring Cloud如何搭建一个网关。

    一.什么是网关: 在微服务架构里,每一个微服务都是一个个体,各个服务可以被独立的设计.开发.测试.部署和管理.这时,各个独立部署单元可以用不同的开发测试团队维护,可以使用不同的编程语言和技术平台进行设 ...

  3. 【Spring Cloud Spring Security Oauth2】Spring Security Oauth2 + Redis + Gateway网关 + Mybatis + Mysql 整合

    目录 一.环境要求 二.源码下载地址 三.参考文献 四.项目预览截图说明 五.oauth2.0认证授权项目 5.1 POM依赖 5.2 yml配置及启动类 5.3 认证配置与资源配置 5.4 自定义U ...

  4. 整合spring cloud云架构 - Gateway的基本入门

    1.gateway和zuul Spring Cloud Finchley版本的gateway比zuul 1.x系列的性能和功能整体要好,且使用 Gateway 做跨域相比应用本身或是 Nginx 的好 ...

  5. Spring Cloud Alibaba - 23 Gateway初体验

    文章目录 概述 网关的作用 官网 来个栗子 step1 搞依赖 step2 搞注解 (gateway没有注解) step3 搞配置 其他工程 & 验证 参数解读 spring.cloud.ga ...

  6. Spring Cloud 7:Gateway

    Zuul 网关 Zuul 是 Netfilx 开源的一个 API Gateway 服务器,本质是一个 Web Servlet 应用.其在微服务架构体系中提供动态路由.监控.弹性.安全等边缘服务. 使用 ...

  7. Spring Cloud微服务之网关服务创建(十四)完结篇

    网关服务 1.在parent父工程中创建一个空的Maven项目infrastructure 2.在infrastructure模块下创建api_gateway模块 3.编写application.pr ...

  8. SpringCloud - Spring Cloud Netflix 之 Zuul网关;路由(十一)

    阅读本文前可先参考 SpringCloud - Spring Cloud根/父项目,开发准备(二)_MinggeQingchun的博客-CSDN博客 一.API网关 引自百度百科 API网关,软件术语 ...

  9. Spring Cloud Alibaba 集成 Gateway 实现动态路由功能

    文章目录 1 摘要 2 核心 Maven 依赖 3 名词释义 4 Gateway 动态路由原理 5 数据库表 6 核心代码 6.1 配置信息 6.2 路由实体类 6.3 本地路由数据库持久层(DAO/ ...

  10. Spring Cloud中查看服务网关(Zuul)中的所有路由节点

    问题描述:在Spring Cloud的早期版本(例如1.X版本)中如果想查看Zuul的所有路由节点,我们除了在pom.xml中引入依赖外,还需要在application.yml文件中添加如下配置: # ...

最新文章

  1. rsyslog概要以及源码安装mysql,rsyslog输出到Mysql基于loganalyzer查看分析
  2. 四、唱歌不如跳舞(上)
  3. 配置Git绑定Git@OSC
  4. 容灾系统的云中嬗变-【软件和信息服务】2013.12
  5. Deep Reinforcement Learning: Pong from Pixels
  6. 分布式 ID的 9 种生成方式
  7. if函数python_python入门(if函数)
  8. Java Web实训项目:西蒙购物网(中)
  9. 联想拯救者电竞手机成功“截胡”:全球首发骁龙865+
  10. mysql 数据缓冲区,MySQL写入缓冲区在数据库中的作用( Change Buffer )
  11. 每日算法系列【LeetCode 1363】形成三的最大倍数
  12. CSF三层主机:渗透测试靶场笔记
  13. 佳能MP145/140打印机 出现错误代码“E5”怎么处理
  14. 【STP】STP(802.1D)端口状态及拓扑变更
  15. DM数据库更换key
  16. 【Leetcode】1214. Two Sum BSTs
  17. 汉字字符集编码查询_刨根究底字符编码之十——Unicode字符集的字符编码方式...
  18. java中config是什么意思_在计算机的命令中Config是什么意思啊?
  19. 如何辅导孩子做学前班数学题?
  20. 电热毯亚马逊欧洲站CE认证审核标准解析

热门文章

  1. 共享指定文件给指定电脑
  2. 1.22.FLINK Watermark\Flink窗口(Window)\watermark有什么用?\如何使用Watermarks处理乱序的数据流?\机制及实例详解\生成方式\代码实例
  3. 谷歌浏览器开发者工具network_关于Chrome谷歌浏览器开发者工具网络Network中返回无数据的问题...
  4. 干货|龙芯架构明御综合日志审计分析平台迁移技术
  5. Acwing第741题(斐波那契数列)
  6. Pytorch向量各个元素的n次方求和
  7. 北京市关于印发建筑工人实名制管理办法(试行)的通知
  8. 微信小程序客服消息配置 token 验证失败 微信无请求记录 问题现象的解决办法
  9. php判断360浏览器是否是兼容模式,JS判断是否360安全浏览器极速内核的方法
  10. Linux基础操作篇