• 网关介绍和作用,Spring Cloud Gateway介绍


    网关是什么?

    顾名思义,就是网络关口,把守网络访问的山海关,关内的人只能从山海关出去,关外的人只能从山海关进来。
    A访问B,A访问C,变成A访问D,然后由D去访问B和C,那么D就是我们的网关。

    本文仅对HTTP网络层面的API网关进行探讨,其他网关不在讨论范围。

    网关能干什么?

    一、是跨域问题的一种解决方案

    如nginx的反向代理就是一个网关,可以解决不同模块单独部署之后,前端的访问路径、跨域问题。

    插个小问题:跨域问题是怎么产生的?
    一般来说,后端项目的子模块被拆分部署了,前端要调用的接口,分布在不同的ip和端口号上,从而造成了跨域问题。

    二、充当防火墙的重任
    网关可以统一校验用户的权限,对于权限不够的不允许访问。

    三、后端可以对外提供统一的访问路径,方便调用者调用。

    四、提供抽象层,方便后端重构接口。
    打个比方,网关类似于,一个java的List接口,调用者只需要定义:List list就行了,至于后端是new ArrayList还是new LinkedList对调用者没有影响。

    Spring Cloud Gateway

    介绍

    Spring cloud gateway是spring官方基于Spring 5.0、Spring Boot2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供简单、有效和统一的API路由管理方式,Spring Cloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Netflix Zuul,其不仅提供统一的路由方式,并且还基于Filer链的方式提供了网关基本的功能,例如:安全、监控/埋点、限流等

    在这里插入图片描述

    导入依赖

    	<dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>
    
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    修改配置(基于nacos)

    application.properties

    # 服务端口
    server.port=80
    # 服务名
    spring.application.name=service-gateway
    
    # nacos服务地址
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    
    #使用服务发现路由
    spring.cloud.gateway.discovery.locator.enabled=true
    
    #设置路由id
    spring.cloud.gateway.routes[0].id=stu
    #匹配该路由的访问路径,只有调用者路径里出现/stu/的才能进入该路由。
    spring.cloud.gateway.routes[0].predicates= Path=/*/stu/**
    #路由的真实目的路径
    spring.cloud.gateway.routes[0].uri=lb://service-stu
    #设置路由id
    spring.cloud.gateway.routes[1].id=tea
    #匹配该路由的访问路径,只有调用者路径里出现/stu/的才能进入该路由。
    spring.cloud.gateway.routes[1].predicates= Path=/*/tea/**
    #路由的真实目的路径
    spring.cloud.gateway.routes[1].uri=lb://service-tea
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    启动类

    @SpringBootApplication
    public class ServerGatewayApplication {
    public static void main(String[] args) {
          SpringApplication.run(ServerGatewayApplication.class, args);
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    FITC标记的脱氧胆酸修饰右旋糖酐纳米粒子,FITC-Dex-DCA-FI/FA NPs
    【Springcloud微服务】MybatisPlus下篇
    【安卓开发】安卓布局控件
    投稿期刊选择-中科院期刊分区排名查询(以光学类为例) -论文投稿经验总结-第2期
    QT基础教程(QPalette和QIcon)
    CyberController手机外挂番外篇:源代码的二次修改
    常用ADB指令
    ES6-promise
    秋招每日一题T10——峰会
    JAVA的参数传递方式
  • 原文地址:https://blog.csdn.net/u012643122/article/details/125884346