Spring Cloud Kubernetes + Spring Cloud Gateway:无法找到 k8s 服务的实例

2024-05-09

我正在使用 Spring Cloud Kubernetes + Spring Cloud Gateway(SCG),但在 GKE 上部署应用程序时遇到一些问题。 SCG 找不到 k8s 服务,我仍然收到此错误:

There was an unexpected error (type=Service Unavailable, status=503).
Unable to find instance for uiservice

uiservice是 Angular 应用程序。

当我看一眼.../actuator/gateway/routes我有这个结果:

[
  {
    "route_id": "CompositeDiscoveryClient_gateway",
    "route_definition": {
      "id": "CompositeDiscoveryClient_gateway",
      "predicates": [
        {
          "name": "Path",
          "args": {
            "pattern": "/gateway/**"
          }
        }
      ],
      "filters": [
        {
          "name": "RewritePath",
          "args": {
            "regexp": "/gateway/(?<remaining>.*)",
            "replacement": "/${remaining}"
          }
        }
      ],
      "uri": "lb://gateway",
      "order": 0
    },
    "order": 0
  },
  {
    "route_id": "CompositeDiscoveryClient_uiservice",
    "route_definition": {
      "id": "CompositeDiscoveryClient_uiservice",
      "predicates": [
        {
          "name": "Path",
          "args": {
            "pattern": "/uiservice/**"
          }
        }
      ],
      "filters": [
        {
          "name": "RewritePath",
          "args": {
            "regexp": "/uiservice/(?<remaining>.*)",
            "replacement": "/${remaining}"
          }
        }
      ],
      "uri": "lb://uiservice",
      "order": 0
    },
    "order": 0
  },
  {
    "route_id": "uiservice_route",
    "route_definition": {
      "id": "uiservice_route",
      "predicates": [
        {
          "name": "Path",
          "args": {
            "_genkey_0": "/*"
          }
        }
      ],
      "filters": [],
      "uri": "lb://uiservice",
      "order": 0
    },
    "order": 0
  },
  ....
]

请注意,服务很容易被发现,因为:"route_id": "CompositeDiscoveryClient_gateway" and "route_id": "CompositeDiscoveryClient_uiservice",这些路线不是我的(我没有定义它们)。

我看了这个帖子:如何设置 Spring Cloud Gateway 应用程序以便它可以使用 Spring Cloud Kubernetes 的服务发现? https://stackoverflow.com/questions/56170511/how-to-set-up-spring-cloud-gateway-application-so-it-can-use-the-service-discove没有成功。

我的配置:

   spring:
      profiles:
        active: prod
      cloud:
        kubernetes:
          reload:
            enabled: true
        gateway:
          discovery:
            locator:
              enabled: true 
              lower-case-service-id: true
          globalcors:
            cors-configurations: 
              '[/**]':
                allowedOrigins: uiservice
                allowedMethods: "*"
                allowCredentials: true
                maxAge: 7200
                allowedHeaders: "*"
                exposedHeaders:
                - "Access-Control-Allow-Origin"
                - "Access-Control-Allow-Methods"
                - "Access-Control-Max-Age"
                - "Access-Control-Allow-Headers"
                - "Cache-Control"
                - "Authorization"
                - "Content-Type"
          routes:
          #======UISERVICE========
          - id: uiservice_route
            uri: lb://uiservice 
            predicates:
            - Path=/* #default route

          - id: uiservice_route_assets
            uri: lb://uiservice
            predicates:
            - Path=/assets/**
   management:
      endpoints:
        web:
          exposure:
            include: "*"
      endpoint:
          restart:
            enabled: true

另外,如何禁用网关自动发现?我不想要"route_id": "CompositeDiscoveryClient_gateway"

依赖项:

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-kubernetes-all</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

感谢您的帮助


折腾了一下午终于找到解决办法了。我认为使用 Ribbon 时服务发现存在问题。我使用 k8s dns 服务发现而不是依赖 Ribbon,所以我的新配置是:

routes:
- id: uiservice_route
  uri: http://uiservice:4200 # switch 'lb://' to 'http://'
  predicates:
  - Path=/* 

K8s uiservice 配置:

apiVersion: v1
kind: Service
metadata:
  name: uiservice
spec:
  sessionAffinity: ClientIP
  selector:
    app: uiservice
  ports:
    - name: http
      port: 4200
      targetPort: ui-port

一个新问题出现了:既然 k8s 服务本身就是这样做的,为什么要使用 Ribbon 来负载平衡请求呢?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Cloud Kubernetes + Spring Cloud Gateway:无法找到 k8s 服务的实例 的相关文章

随机推荐