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

相关推荐
AI_小站5 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
晓晓_za8986689 小时前
Geo 优化源码蒸馏词机制:地域词库构建与匹配逻辑详解
运维·tcp/ip·spring·缓存·ci/cd
nvd119 小时前
LiteLLM 如何使用 Redis:从部署位置到精确响应缓存
redis·缓存·gateway
ycjunhua11 小时前
Spring AI 2.0 GA:ToolCallingAdvisor 重构与 Java Agent 新范式
java·人工智能·spring
让你三行代码QAQ12 小时前
SpringAI-依赖问题
spring·ai编程
风流 少年14 小时前
Spring AI 2.0:阿里云百炼平台(智能体应用)
redis·spring·阿里云
仍然.16 小时前
SpringCloud---OpenFeign
spring boot·spring·spring cloud
风流 少年16 小时前
Spring AI 2.0:阿里云百炼平台(工作流应用)
java·后端·spring
手握风云-18 小时前
Spring Cloud:分布式系统的“粘合剂”(五)
后端·spring·spring cloud
SQL-First布道者19 小时前
全面解构传统持久层框架,拥抱真正的 SQL-First
java·数据库·spring boot·sql·spring·mybatis·spring jdbc