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

相关推荐
廋到被风吹走29 分钟前
【Spring】 Spring Cloud 服务注册与发现深度解析:Eureka/Nacos/Consul 源码、AP vs CP 与自我保护机制
spring·spring cloud·java-consul
计算机学姐1 小时前
基于SpringBoot的汽车租赁系统【个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·spring·汽车·推荐算法
8***f3951 小时前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
我是人✓1 小时前
Spring IOC入门
java·数据库·spring
rgeshfgreh2 小时前
Spring Bean管理机制深度解析
java·spring boot·spring
L***d6702 小时前
SpringColoud GateWay 核心组件
gateway
悟空码字2 小时前
Spring Cloud Gateway实战,从零搭建API网关,构建高性能微服务统一入口
java·gateway·springcloud·编程技术·后端开发
i***13242 小时前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
9***g6872 小时前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
xiaolyuh12312 小时前
Spring 框架 核心架构设计 深度详解
spring·设计模式·spring 设计模式