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

Spring Cloud Kubernetes + Spring Cloud Gateway: Unable to find instance for k8s service

我正在使用 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""route_id": "CompositeDiscoveryClient_uiservice",那些路由不是我的(我没有定义它们)。

我看了这个post:How to set up Spring Cloud Gateway application so it can use the Service Discovery of Spring Cloud Kubernetes? 没有成功。

我的配置:

   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>

感谢您的帮助

设置如下属性

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false

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

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

K8s 用户界面配置:

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

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

应该是这样的:

spring:
  application.name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          url-expression: "'http://'+serviceId+':'+getPort()"
          lower-case-service-id: true

当调用 http://gateway/my-service-name/api/etc 时,如果服务存在于 kubernetes

中,则将调用 my-service-name/api/etc

所以你需要确保有这样的服务:

apiVersion: v1
kind: Service
metadata:
  name: my-service-name
  namespace: default
  labels:
    app: my-service-name
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30080
  selector:
    app: my-service-name

我认为您没有添加 starter-ribbon 依赖项,这将响应在 k8s 环境中查找您的服务实例。

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>