项目用到了Gateway 作为网关,网关的相关使用场景很值得了解,还有路由配置。今天就来记录一下。
开启基于注册中心的路由表。gateway可以通过开启以下配置来打开根据服务的serviceId来匹配路由,默认是大写:
cloud:
gateway:
discovery:
locator:
enabled: true
如果需要小写serviceId,则配置
spring.cloud.gateway.locator.lowerCaseServiceId:true
注意事项
不管小写大写,不能使用下划线,否则会报:
org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for localhost
所以服务的spring.application.name 必须用中划线而不是下划线
如果开启了lowerCaseServiceId,则只能用小写,不能识别大写,如果不开启,只能识别大写
也可以手动设置,在routes中配置 配置对应的类
这是每个路由信息的属性, 对应着.yml配置文件
可以看到 id 如果不写的话默认是uuid 唯一值。不可重复