xiaoymin / knife4j

Knife4j is a set of Swagger2 and OpenAPI3 All-in-one enhancement solution
https://doc.xiaominfo.com
Apache License 2.0
4.06k stars 612 forks source link

网关聚合页面访问子微服务404,丢失服务前缀 #673

Open sangyuan6122 opened 10 months ago

sangyuan6122 commented 10 months ago

Describe the bug 网关聚合页面访问子微服务404,丢失服务前缀,版本4.3.0和4.4.0-SNAPSHOT都试了还是不行,是那里配置不合适吗,望赐教。 gateway xml如下:

server:
  port: 10000
reactor:
  netty:
    worker-count: 16  
knife4j:
  gateway:
    enabled: true
#    strategy: manual
#    routes:
#      - name: 订单openapi2
#        service-name: user-service
#        url: /order-service-openapi2/v2/api-docs?group=default
#        context-path: /
#      - name: 订单openapi3
#        service-name: order-service
#        url: /order/v3/api-docs/default
#        context-path: /order
    strategy: discover
    discover:
      version: openapi3
      enabled: true
    tags-sorter: order
    operations-sorter: order
spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        group: DEFAULT_GROUP
    discovery:
      reactive:
        enabled: true
    loadbalancer:
      retry:
        enabled: true
    # 路由网关配置
    gateway:
      # 启用了自动根据服务名建立路由
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/user/**
          filters:
            - RewritePath=/user/(?<segment>.*),/$\{segment}
        - id: order-service
          uri: lb://order-service
          predicates:
            - Path=/order/**
          filters:
            - RewritePath=/order/(?<segment>.*),/$\{segment}
  config:
    import:
      - optional:nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}               
logging:
  config: classpath:logback-spring.xml

image

To Reproduce image 这里的地址应该是:/order/order/hello少了"/order"

Knife4j Version (please complete the following information): spring.boot.version:3.1.4 spring-cloud.version:2022.0.4 spring-cloud-alibaba.version:2022.0.0.0 spring-cloud-gateway.version:4.0.7 knife4j-gateway-spring-boot-starter:4.4.0-SNAPSHOT

xiaoymin commented 10 months ago

如果你的子服务设置了server.servlet.context-path

那你应该配上聚合时候的context-path。

knife4j:
  gateway:
    enabled: true
    strategy: manual
    routes:
        context-path: /
      - name: 订单openapi3
        service-name: order-service
        url: /order/v3/api-docs/default
        context-path: /order

strategy: discover这个模式 拿不到你子服务的context-path的 建议不配server.servlet.context-path

sangyuan6122 commented 10 months ago

子服务用的官网demo,没有配置server.servlet.context-path,并且swagger-config(见上图)接口已经返回了正确的context-path,前端页面拼地址的时候没加上context-path,感觉加上就对了:

spring:
  application:
    name: user-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        group: DEFAULT_GROUP
        namespace: xiaoyumin
        username: nacos
        password: nacos
    discovery:
      reactive:
        enabled: false
    loadbalancer:
      retry:
        enabled: true
springdoc:
  swagger-ui:
    path: /swagger-ui.html
    tags-sorter: alpha
    operations-sorter: alpha
    show-extensions: true
  api-docs:
    path: /v3/api-docs
  group-configs:
    - group: 'default'
      paths-to-match: '/org/**'
      packages-to-scan: com.github.knife4j.user
  default-flat-param-object: false
server:
  port: 10091
zdpbj2014 commented 7 months ago

如果你的子服务设置了server.servlet.context-path

那你应该配上聚合时候的context-path。

knife4j:
  gateway:
    enabled: true
    strategy: manual
    routes:
        context-path: /
      - name: 订单openapi3
        service-name: order-service
        url: /order/v3/api-docs/default
        context-path: /order

strategy: discover这个模式 拿不到你子服务的context-path的 建议不配server.servlet.context-path

com.github.xiaoymin knife4j-gateway-spring-boot-starter 4.3.0

有同样问题,在接口调试的时候缺少服务名转发路径,导致网关无法转发到微服务