使用eureka和spring-cloud-gateway路由到一个带有上下文的服务时,404错误?

版本如下:
<spring.cloud.version>Hoxton.SR8</spring.cloud.version>
<spring.boot.version>2.3.3.RELEASE</spring.boot.version>

有三个应用或服务:

  1. 服务注册中心
    使用eureka搭建
  2. 网关
    使用spring-cloud-gateway搭建
  3. 自定义应用
    基于spring-boot

以下是各个应用或服务的配置信息

  1. eureka的application.yml
server:
  port: 8761

eureka:
  server:
    enableSelfPreservation: false
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
  1. spring-cloud-gateway的application.yml

    server:
      port: 9000
    
    spring:
      application:
        name: my-gateway
      cloud:
       routes:
         - id:  demo-service
           uri: lb://DEMO-SERVICE
           predicates:
             - Path=/demo-service/**
    
    eureka:
      instance:
        prefer-ip-address: true
        instance-id: ${spring.cloud.client.ip-address}:${server.port}
      client:
        service-url:
        defaultZone: http://localhost:8761/eureka
  2. 自定义应用的application.yml

    # 自定义配置
    app:
      name: demo-service
    
    server:
      port: 7000
      servlet:
        context-path: /${app.name}
    
    spring:
      application:
        name: ${app.name}
    
    eureka:
      instance:
        prefer-ip-address: true
        instance-id: ${spring.cloud.client.ip-address}:${server.port}
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka

启用以上程序后,单独访问自定义应用

#可以访问,其中demo-service是上下文
http://localhost:7000/demo-service/hello/123abc

然后通过网关则404

http://localhost:9000/demo-service/hello/123abc

上面的配置,如果不使用自定义应用的上下文,
网关的路由规则配置

- Path=/hello/**

则可以通过网关直接访问成功

http://localhost:9000/hello/123abc

请问,现在流行做法是不带上下文的发布服务?
网关上如果要访问带有上下文的服务时,应该怎么配置呢?

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