• springCloud(踩坑)springCloud-gateway配合Nacos访问接口404,503


    请检查您是否设置了服务提供者的**全局路径映射**

    首先大家都知道springCloudAlibaba + Nacos是依靠spring.application.name进入服务注册的;
    我们如果设置了servlet.context.path 则正常的接口就变成了${context.path}/api

    我出现404的问题是:

    • 服务端接口:user/getWay

    并且配置了servlet.context.path=user-center则接口访问为:user-center/user/getWay

    • gateway映射:user-center/**
    • nacos的服务名为user-center
    • 而gateway实际的重写规则是:把服务名user-center去掉,即访问如下:
      接口user-center/user/getWay --> gateway访问接口user/getWay
    • 解决方案:我们可以重写路径 …如下

    项目架构:

    1、服务提供接口

    因为使用Nacos是通过服务名注册,所以必须配置
    spring.application.name=user-center
    在这里插入图片描述
    在这里插入图片描述

    2、gateway调用配置

    在这里插入图片描述
    在这里插入图片描述

    分析1、正常的访问方式(我们预想到的):

    # 请求gateway服务器
    http://localhost:8888/user-center/user/get/100
    # 对应nacos user-center服务提供者的接口
    http://localhost:7000/user/get/100
    
    • 1
    • 2
    • 3
    • 4

    请检查是否设置了全局路径前缀,例如设置了servlet.context.path
    所以上面服务提供者的接口就变成了
    http://localhost:7000/user-center/user/get/100

    分析结果!!!!

    所以这时候我们还是调用gateway服务就显示404

    http://localhost:8888/user-center/user/get/100
    
    • 1

    gateway中的user-center是路径断言里面的,可以自定义匹配

    解决!!!

    解决一:
    # 这样是可以访问到的,/-_-||
    http://localhost:8888/user-center/user-center/user/get/100
    
    • 1
    • 2

    user-center/user/get/100由于我们不小心设置了全局路径映射,所以服务访问地址是这个
    前面的user-center才是gateway路由匹配地址

    解决二:

    重写访问地址(已经设置了全局路径映射,推荐

    spring:
      cloud:
        # Spring Cloud Gateway 配置项,对应 GatewayProperties 类
        gateway:
          # 路由配置项,对应 RouteDefinition 数组
          routes:
            - id: user
              uri: lb://user-center
              predicates:
                - Path=/user-api/user/**
              filters:
              #这里重写了跳转地址,转成真实的user-center;也可以自定义地址
                - RewritePath=/user-api/user(?/?.*), /user-center/user${remaining} 
            - id: baidu # 路由的编号
              uri: https://www.baidu.com # 路由到的目标地址
              predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
                - Path=/baidu1
              filters: #过滤器,对请求进行拦截,实现自定义的功能。Gateway 内置了多种 Filter 的实现,提供了多种请求的处理逻辑,比如说限流、熔断等等。
                - StripPrefix=1
                # 这里配置的 StripPrefix 会将请求的 Path 去除掉前缀。
                # 例如:我们以第一个 Route 举例子,假设我们请求 http://127.0.0.1:8888/baidu1 时:
                # 如果有配置 StripPrefix 过滤器,则转发到的最终 URI 为 https://www.baidu.com,正确返回首页
                # 如果未配置 StripPrefix 过滤器,转发到的最终 URI 为 https://www.baidu.com/baidu1,错误返回 404
          discovery: # Gateway 与 Spring Cloud 注册中心的集成
            locator:
              enabled: true
              # lb:// 前缀,表示将请求负载均衡转发到对应的服务的实例。
              # "'lb://' + serviceId" Spring EL 表达式,将从注册中心获得到的服务列表,每一个服务的名字对应 serviceId,最终使用 Spring EL 表达式进行格式化。
              url-expression: "'lb://' + serviceId" # 路由的目标地址的表达式,默认为 "'lb://' + serviceId"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    我们配置的gateway匹配地址为
    http://localhost:8888/user-api/user/getWay
    经过 RewritePath 重写,真实请求的地址为
    http://localhost:7000/user-center/user/getWay

    如果解决了你的问题,右下角在这里插入图片描述

  • 相关阅读:
    0829|C++day7 auto、lambda、C++数据类型转换、C++标准模板库(STL)、list、文件操作
    【区块链实战】如何创建一个带参数的智能合约
    内链外链抓取生成sitemap和主动推送的技巧
    GEO生信数据挖掘(六)实践案例——四分类结核病基因数据预处理分析
    如何制作很火的抖音配音?原来爆款短视频配音方法这么简单
    Python-语句
    大河弯弯:CSS 文档流与三大元素显示模式
    交换机基础知识之安全配置
    关于gorm的preload方法笔记
    【Linux进程间通信】二、pipe管道
  • 原文地址:https://blog.csdn.net/m0_67393342/article/details/126597318