springcloud gateway正确路由配置 uri找不到服务?

引言

在集成nacos组件以及springcloudalibaba 的gateway时,出现正确配置,如下文配置,却在测试demo调用该服务的时候,无法路由到此服务,说是无效服务。经过查阅资料得知其命名不符合gateway的命名规范,因而无法识别。规范详情见下文。

yml 复制代码
spring:
	cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
      routes:
        # 研究模块
        - id: j-research
          uri: lb://j_modules_research
          predicates:
            - Path=/research/**
          filters:
            - StripPrefix=1

gateway可以识别的nacos服务名,命名规范

在gateway中配置uri配置有三种方式,包括

  • 第一种:ws(websocket)方式: uri: ws://localhost:9000

  • 第二种:http方式: uri: http://localhost:8130/

  • 第三种:lb(注册中心中服务名字)方式: uri: lb://brilliance-consumer

其中ws和http方式不容易出错,因为http格式比较固定,但是lb方式比较灵活自由。不考虑网关,只考虑服务时,服务名命名时比较自由,都能启动被访问,被注册到注册中心,但是如果提供给gateway使用时,就会对服务命名方式有特殊要求了。

能被gateway的lb方式识别到的命名规则为:

"a-zA-Z:."

这也意味着,java命名规范中可以使用的英文下划线("_")不能被识别,而我命名为:brilliance_consumer,刚好带下划线,改为brilliance-consumer后则可以正常通过网关访问自己项目。

如果名字中有非*"a-zA-Z:."*规则字符,则会报错,

规则见包org.springframework.cloud.gateway.filter中的类RouteToRequestUrlFilter

相关推荐
卓怡学长7 小时前
m111基于MVC的舞蹈网站的设计与实现
java·前端·数据库·spring boot·spring·mvc
笔墨新城17 小时前
Agent Spring Ai 开发之 (一) 基础配置
人工智能·spring·agent
梁下轻语的秋缘18 小时前
ESP32-WROOM-32E存储全解析:RAM/Flash/SD卡读写与速度对比
java·后端·spring
鸽鸽程序猿20 小时前
【JavaEE】【SpringCloud】注册中心_nacos
java·spring cloud·java-ee
想用offer打牌21 小时前
Spring AI Alibaba与 Agent Scope到底选哪个?
java·人工智能·spring
crossaspeed1 天前
Java-SpringBoot的启动流程(八股)
java·spring boot·spring
递归尽头是星辰1 天前
Spring Cloud Alibaba 核心理论体系:Nacos、Sentinel、Seata深度解析
spring cloud·nacos·sentinel·seata·微服务治理
lpfasd1231 天前
springcloud docker 部署问题排查与解决方案
spring·spring cloud·docker
qqqahhh1 天前
xml文件的动态化配置,导入
xml·spring·springboot
BullSmall1 天前
SEDA (Staged Event-Driven Architecture, 分阶段事件驱动架构
java·spring·架构