springcloud gateway 路由配置问题

使用nacos作为配置和注册中心,spring cloud版本是Hoxton.SR3,
spring-cloud-alibaba版本是2.2.1.RELEASE,nacos版本是1.3.1-BETA。

gateway使用了像下面这样的路由配置:

      routes:
        - id: service-one
          uri: lb://service-one
          predicates:
            - Path=/service/**

请求/service/** 404,但是请求/service-one/**就没问题,这是什么情况?

这是工程结构:
image

这是gateway的启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

这是gateway的配置文件:

server:
  port: 8001

spring:
  application:
    name: gateway
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: service-one
          uri: lb://service-one
          predicates:
            - Path=/service/**

这是service-one的启动类:

@SpringBootApplication
@EnableFeignClients
@RestController
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @RequestMapping("/test/map")
    public Map<String, String> test() {
        Map<String, String> map = new HashMap<>();
        map.put("hello", "nihao");
        return map;
    }
}

这是service-one的配置文件:

server:
  port: 8002

spring:
  application:
    name: service-one
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
      discovery:
        server-addr: 127.0.0.1:8848
阅读 5.4k
2 个回答

需要把前缀的service去掉,或者是在service-one这个服务里uri加上service
gateway的filters里的

StripPrefixGatewayFilterFactory
      routes:
        - id: service-one
          uri: lb://service-one
          predicates:
            - Path=/service/**
          filters:
            - StripPrefix=1

额,不是服务启动写入到nacos,gateway读取到nacos中到路由吗

好像Route是用ID来区分到,你尝试吧ID的service-one换成service试试

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题