请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

通过网关访问服务404,直接访问能正常返回

老师你好我配置的gateway uri 直接输入服务地址, 通过网关访问404
图片描述

正在回答

1回答

您好,gateway的配置有问题:

将Path改为/v1即可。比如:

spring:
  cloud:
    gateway:
      routes:
        - id:  aaaaaaaa
          # 这里的小尾巴去掉
          uri: http://localhost:8081
          predicates:
            - Path=/v1

====

普及下定位问题的技巧:

将如下包的日志级别设为debug:

logging:
  level:
    reactor.netty: debug

这样,即可打印转发的细节。举个例子:

spring:
  cloud:
    gateway:
      routes:
        - id:  aaaaaaaa
          uri: http://www.itmuch.com/v1
          predicates:
            - Path=/test
  application:
    name: gateway
logging:
  level:
    reactor.netty: debug

当访问 :http://localhost:8080/test时候,会 打印类似如下日志:

2020-10-12 10:46:41.121 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] New http connection, requesting read
2020-10-12 10:46:41.121 DEBUG 35444 --- [ctor-http-nio-3] r.n.http.server.HttpServerOperations     : [id: 0xbe302274, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52779] New http connection, requesting read
2020-10-12 10:46:41.122 DEBUG 35444 --- [ctor-http-nio-2] reactor.netty.channel.BootstrapHandlers  : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Initialized pipeline DefaultChannelPipeline{(BootstrapHandlers$BootstrapInitializerHandler#0 = reactor.netty.channel.BootstrapHandlers$BootstrapInitializerHandler), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpServerCodec), (reactor.left.httpTrafficHandler = reactor.netty.http.server.HttpTrafficHandler), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
2020-10-12 10:46:41.122 DEBUG 35444 --- [ctor-http-nio-3] reactor.netty.channel.BootstrapHandlers  : [id: 0xbe302274, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52779] Initialized pipeline DefaultChannelPipeline{(BootstrapHandlers$BootstrapInitializerHandler#0 = reactor.netty.channel.BootstrapHandlers$BootstrapInitializerHandler), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpServerCodec), (reactor.left.httpTrafficHandler = reactor.netty.http.server.HttpTrafficHandler), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
2020-10-12 10:46:41.156 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Increasing pending responses, now 1
2020-10-12 10:46:41.160 DEBUG 35444 --- [ctor-http-nio-2] reactor.netty.http.server.HttpServer     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@26394e1a
2020-10-12 10:46:41.236 DEBUG 35444 --- [ctor-http-nio-2] r.n.resources.PooledConnectionProvider   : Creating a new client pool [PoolFactory {maxConnections=2147483647, pendingAcquireMaxCount=-1, pendingAcquireTimeout=0, maxIdleTime=-1, maxLifeTime=-1, metricsEnabled=false}] for [www.itmuch.com:80]
2020-10-12 10:46:41.250 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3] Created a new pooled channel, now 1 active connections and 0 inactive connections
2020-10-12 10:46:41.253 DEBUG 35444 --- [ctor-http-nio-4] reactor.netty.channel.BootstrapHandlers  : [id: 0x14b049e3] Initialized pipeline DefaultChannelPipeline{(BootstrapHandlers$BootstrapInitializerHandler#0 = reactor.netty.channel.BootstrapHandlers$BootstrapInitializerHandler), (PooledConnectionProvider$PooledConnectionAllocator$PooledConnectionInitializer#0 = reactor.netty.resources.PooledConnectionProvider$PooledConnectionAllocator$PooledConnectionInitializer), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpClientCodec), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
2020-10-12 10:46:41.299 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Registering pool release on close event for channel
2020-10-12 10:46:41.299 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Channel connected, now 1 active connections and 0 inactive connections
2020-10-12 10:46:41.300 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] onStateChange(PooledConnection{channel=[id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80]}, [connected])
2020-10-12 10:46:41.303 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] onStateChange(GET{uri=/, connection=PooledConnection{channel=[id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80]}}, [configured])
2020-10-12 10:46:41.304 DEBUG 35444 --- [ctor-http-nio-4] r.netty.http.client.HttpClientConnect    : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Handler is being applied: {uri=http://www.itmuch.com/test, method=GET}
2020-10-12 10:46:41.305 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] onStateChange(GET{uri=/test, connection=PooledConnection{channel=[id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80]}}, [request_prepared])
2020-10-12 10:46:41.318 DEBUG 35444 --- [ctor-http-nio-2] reactor.netty.channel.FluxReceive        : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Subscribing inbound receiver [pending: 0, cancelled:false, inboundDone: true]
2020-10-12 10:46:41.328 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] onStateChange(GET{uri=/test, connection=PooledConnection{channel=[id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80]}}, [request_sent])
2020-10-12 10:46:41.362 DEBUG 35444 --- [ctor-http-nio-4] r.n.http.client.HttpClientOperations     : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Received response (auto-read:false) : [Server=nginx, Date=Mon, 12 Oct 2020 02:47:33 GMT, Content-Type=text/html, Content-Length=548, Connection=keep-alive]
2020-10-12 10:46:41.362 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] onStateChange(GET{uri=/test, connection=PooledConnection{channel=[id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80]}}, [response_received])
2020-10-12 10:46:41.370 DEBUG 35444 --- [ctor-http-nio-4] reactor.netty.channel.FluxReceive        : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Subscribing inbound receiver [pending: 0, cancelled:false, inboundDone: false]
2020-10-12 10:46:41.376 DEBUG 35444 --- [ctor-http-nio-4] r.n.http.client.HttpClientOperations     : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Received last HTTP packet
2020-10-12 10:46:41.384 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] onStateChange(GET{uri=/test, connection=PooledConnection{channel=[id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80]}}, [response_completed])
2020-10-12 10:46:41.385 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] onStateChange(GET{uri=/test, connection=PooledConnection{channel=[id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80]}}, [disconnecting])
2020-10-12 10:46:41.385 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Releasing channel
2020-10-12 10:46:41.387 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Channel cleaned, now 0 active connections and 1 inactive connections
2020-10-12 10:46:41.391 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Last HTTP response frame
2020-10-12 10:46:41.391 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Decreasing pending responses, now 0
2020-10-12 10:46:41.391 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Last HTTP packet was sent, terminating the channel
2020-10-12 10:46:41.453 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Increasing pending responses, now 1
2020-10-12 10:46:41.453 DEBUG 35444 --- [ctor-http-nio-2] reactor.netty.http.server.HttpServer     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@26394e1a
2020-10-12 10:46:41.510 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Decreasing pending responses, now 0
2020-10-12 10:46:41.512 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Last HTTP packet was sent, terminating the channel
2020-10-12 10:46:41.513 DEBUG 35444 --- [ctor-http-nio-2] r.n.http.server.HttpServerOperations     : [id: 0x389fdfcb, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:52778] Last HTTP response frame
2020-10-12 10:47:41.365 DEBUG 35444 --- [ctor-http-nio-4] r.n.resources.PooledConnectionProvider   : [id: 0x14b049e3, L:/30.22.102.73:52781 ! R:www.itmuch.com/116.255.183.43:80] onStateChange(PooledConnection{channel=[id: 0x14b049e3, L:/30.22.102.73:52781 ! R:www.itmuch.com/116.255.183.43:80]}, [disconnecting])

由日志可知:

2020-10-12 10:46:41.304 DEBUG 35444 --- [ctor-http-nio-4] r.netty.http.client.HttpClientConnect    : [id: 0x14b049e3, L:/30.22.102.73:52781 - R:www.itmuch.com/116.255.183.43:80] Handler is being applied: {uri=http://www.itmuch.com/test, method=GET}

该路径转发到了http://www.itmuch.com/test ,而不是/v1。

=======

事实上,Gateway不能直接URL重写。如果想要实现重写,可需要额外配置 RewritePath (URL重写过滤器,详见过滤器工厂一节)。

1 回复 有任何疑惑可以回复我~
  • 提问者 yicj #1
    非常感谢!
    回复 有任何疑惑可以回复我~ 2020-10-12 21:21:25
  • 提问者 yicj #2
    太感谢老师了,我试了下可以了
    回复 有任何疑惑可以回复我~ 2020-10-12 21:22:20
  • 大目 回复 提问者 yicj #3
    不客气,解决就好哦!加油!!
    回复 有任何疑惑可以回复我~ 2020-10-12 21:35:08
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信