SpringCloud(学习 Gateway)三



前言

网关的存在就是让请求动态路由到正确的服务,也可以在网关层做鉴权、限流、日志输出等功能的实现。

在这里插入图片描述

本次网关采用 Gateway 进行,不使用Zuul


一、三大概念

1. Route(路由)

请求通过网关路由到对应的服务中。

  • ID:通过不同的ID来区分不同路由
  • URI:请求到服务的地址
  • 断言集合
  • 过滤器集合

注:只要断言为真视为匹配成功

2. Predicate(断言)

在请求经过网关时会根据请求头、参数等判断出是否符合某个路由规则

官方提供了12种断言规则

在这里插入图片描述
请添加图片描述

3. Filter(过滤器)

在路由到指定地址时会经过一系列过滤器,当响应请求时也会经过一系列过滤器。

官方提供了37种过滤器规则


在这里插入图片描述

二、整合Gateway

建议不要继承common中的依赖,会有很多依赖问题

  • 1.引入依赖

代码如下:微服务的版本为 2021.0.5

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
<!--        服务注册/发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
<!--        配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
<!--        网关依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>3.1.5</version>
        </dependency>
<!--        bootstrap.properties依赖文件适配高版本springcloud-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.0</version>
        </dependency>
  • 2.网关服务注册

在启动类添加注解

@EnableDiscoveryClient
  • 3.创建命名空间并创建配置
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 4.配置端口等信息(application.properties)
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=guli-gateway
server.port=88
  • 5连接nacos配置(bootstrap.properties)
spring.application.name=guli-gateway
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=4b130b5f-4b59-4aed-b1f8-2efdd14ae549
  • 6配置网关相关配置(application.yml)

目前测试阶段配置写在本地上,以后通过nacos配置中心配置

spring:
  cloud:
    gateway:
      routes: #是个集合
        - id: baidu_route #ID
          uri: https://www.baidu.com #路由转发地址
          predicates: #断言 是个集合 可配置多个断言
            - Query=url,baidu #参数断言 url参数为baidu时 断言通过 逗号之后可以使用正则
          filters: #过滤器 是个集合 
            - AddRequestHeader=X-Request-red, blue #添加请求头过滤器

        - id: qq_route
          uri: https://www.qq.com
          predicates:
            - Query=url,qq

具体配置方法、断言、过滤器配置可以查看官方文档


总结

本文主要讲了网关Gateway的简介以及如何在SpringCloud中整合Gateway、一些配置。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天将降大任于我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值