当前位置: 首页 > 知识库问答 >
问题:

Spring云网关路由到另一个名称空间中的服务

仇高韵
2023-03-14

我有一个 Spring 启动应用程序正在运行并使用 Spring 云网关。该应用程序在 kubernetes 上运行(在 EKS 和本地 docker 桌面上尝试过)。我在访问指向另一个命名空间(非默认)中的服务的路由时遇到问题。当我启动 http://localhost:8080/actuator/gateway/routes 时,我看到下面的路线是路线之一

{
predicate: "Paths: [/my-app/**], match trailing slash: true",
metadata: {
  port.http: "8080",
  k8s_namespace: "customns",
  app.kubernetes.io/name: "my-app",
  spring-boot: "yes",
  kubectl.kubernetes.io/last-applied-configuration: "{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app.kubernetes.io/name":"my-app","platform":"no","spring-boot":"yes"},"name":"my-app","namespace":"customns"},"spec":{"ports":[{"name":"http","port":8082,"targetPort":8080}],"selector":{"app.kubernetes.io/name":"my-app"},"type":"LoadBalancer"}} ",
  platform: "no"
},
route_id: "ReactiveCompositeDiscoveryClient_my-app",
filters: [
"[[RewritePath /my-app/(?<remaining>.*) = '/${remaining}'], order = 1]"
],
uri: "lb://my-app",
order: 0
}

在应用程序中。yml,我添加了以下网关路由

spring:
  main:
    banner-mode: off
  cloud:
    kubernetes:
      discovery:
        enabled: true
        all-namespaces: true
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      metrics:
        enabled: true
      routes:
        - id: my-app
          uri: lb://my-app
          predicates:
            - Path=/my-custom-url/**
          filters:
            - RewritePath=/my-custom-url(?<segment>/?.*), $\{segment}

但是,当我启动 http://localhost:8080/my-app 或 http://localhost:8080/my-custom-url 时,出现如下错误

2021-03-04 16:18:24.201 ERROR [my-gateway-service,,] 1 --- [or-http-epoll-4] a.w.r.e.AbstractErrorWebExceptionHandler : [9a85e6b2-7]  500 Server Error for HTTP GET "/my-app"

java.lang.IllegalArgumentException: The path does not have a leading slash.
        at org.springframework.util.Assert.isTrue(Assert.java:121) ~[spring-core-5.3.3.jar:5.3.3]
        Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
        |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.authorization.AuthorizationWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.authorization.ExceptionTranslationWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.authentication.logout.LogoutWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.savedrequest.ServerRequestCacheWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.ui.LogoutPageGeneratingWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.ui.LoginPageGeneratingWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.oauth2.client.web.server.authentication.OAuth2LoginAuthenticationWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.oauth2.client.web.server.OAuth2AuthorizationRequestRedirectWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.context.ReactorContextWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.header.HttpHeaderWriterWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.config.web.server.ServerHttpSecurity$ServerWebExchangeReactorContextWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.security.web.server.WebFilterChainProxy [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.cloud.sleuth.instrument.web.TraceWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ HTTP GET "/my-app" [ExceptionHandlingWebHandler]
Stack trace:
                at org.springframework.util.Assert.isTrue(Assert.java:121) ~[spring-core-5.3.3.jar:5.3.3]
                at org.springframework.http.server.reactive.DefaultServerHttpRequestBuilder.path(DefaultServerHttpRequestBuilder.java:95) ~[spring-web-5.3.3.jar:5.3.3]
                at org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$1.filter(RewritePathGatewayFilterFactory.java:70) ~[spring-cloud-gateway-server-3.0.0.jar:3.0.0]
                at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44) ~[spring-cloud-gateway-server-3.0.0.jar:3.0.0]
                at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.lambda$filter$0(FilteringWebHandler.java:117) ~[spring-cloud-gateway-server-3.0.0.jar:3.0.0]
                at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:44) ~[reactor-core-3.4.2.jar:3.4.2]
                at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64) ~[reactor-core-3.4.2.jar:3.4.2]
                at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.4.2.jar:3.4.2]
                at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.4.2.jar:3.4.2]
                at reactor.core.publisher.Mono.subscribe(Mono.java:4046) ~[reactor-core-3.4.2.jar:3.4.2]
                at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:173) ~[reactor-core-3.4.2.jar:3.4.2]
                at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56) ~[reactor-core-3.4.2.jar:3.4.2]
                at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64) ~[reactor-core-3.4.2.jar:3.4.2]
                :
                :
                :
                at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324) ~[netty-codec-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296) ~[netty-codec-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:795) ~[netty-transport-native-epoll-4.1.58.Final-linux-x86_64.jar:4.1.58.Final]
                at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:480) ~[netty-transport-native-epoll-4.1.58.Final-linux-x86_64.jar:4.1.58.Final]
                at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378) ~[netty-transport-native-epoll-4.1.58.Final-linux-x86_64.jar:4.1.58.Final]
                at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
                at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
                at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]

我的问题是,如何使 Spring 云网关路由到另一个命名空间中的服务?

共有1个答案

洪琦
2023-03-14

尝试以下更改。

> < li>

如果您正在定义路由,则无需启用网关发现。

还要更改过滤器正则表达式的使用,如下所示。

RewritePath=/my-custom-url(?

  • 如果它不起作用,请更改路线url,如下所示。

超文本传输协议://

 类似资料:
  • 我使用Spring创建微服务。我使用Eureka进行服务发现,使用Zuul进行路由。现在我想切换到Spring Cloud Gateway(因为它的非阻塞特性),但是我没有找到自动路由到每个Eureka服务的方法。 例如,如果一个服务'eureka-client'注册到了Eureka,那么Zuul本身就为这个服务提供了类似于localhost:8762/eureka-client的路径。使用Spr

  • 我们使用的实现以编程方式实现路由。我们有两个服务,它们应该在相同的路由路径上注册,如果另一个不存在,其中一个将作为后备。具有特定路径的首选路由是: 因此,当调用时,应该使用第一条路由,而当调用时,应该使用第二条路由。 问题是,即使是路线上的order属性似乎也不能解决这个问题;目前,我们发现没有可能设置这种路线的顺序/优先级。这是intender吗?

  • 我试图将使用工作的网关迁移到Spring Cloud网关,但遇到了请求路由问题。 下面是Spring Cloud Gateway配置的一个片段,我试图在其中配置一个等效的路由: 我使用Spring Cloud Eureka作为我的Discovery服务器(在一个单独的微服务中),并且我目前没有任何配置,如配置DiscoveryClient路由的谓词和过滤器中所述 如果我向发出请求,我将收到一个40

  • 我在访问另一个命名空间中存在的服务时遇到问题。 我有两个名称空间(在同一个集群中)airflow-dev和dask-dev。 在dask-dev名称空间中,我部署了dask集群(dask调度器和工作器)。此外,还为dask-scheduler pod创建了一个服务(集群IP)。我可以使用“kubectl port-forward”命令从chrome访问dask-scheduler pod。 然而,

  • 我正在使用Spring Cloud(hoxton.sr10)和Spring Boot(2.2.6.release) 我在尤里卡服务器8761注册了我的服务 gateway mainClass java