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

 ̄綄美尐妖づ 提交于 2019-12-11 15:54:34

问题


I am using Spring Cloud Kubernetes + Spring Cloud Gateway(SCG) and I have some trouble to deploy my app on GKE. SCG does not find k8s service, I still get this error:

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

uiservice is Angular app.

When I take a look at .../actuator/gateway/routes I have this result:

[
  {
    "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
  },
  ....
]

Please note that services are well discovered because of that: "route_id": "CompositeDiscoveryClient_gateway" and "route_id": "CompositeDiscoveryClient_uiservice", those routes are not mine (I did not define them).

I took a look at this post:How to set up Spring Cloud Gateway application so it can use the Service Discovery of Spring Cloud Kubernetes? without success.

My configuration:

   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

Also, how can I disable gateway autodiscovering? I don't want the "route_id": "CompositeDiscoveryClient_gateway"

Dependencies:

<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>

Thanks for your help


回答1:


Set the following property

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



回答2:


I finally found a solution after losing an afternoon. I think there is an issue on service discovering when using Ribbon. I use k8s dns service discovering instead of relying on Ribbon, so my new config is:

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

K8s uiservice config:

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

A new question arised: Why using Ribbon to load balance requests since k8s services natively do that?



来源:https://stackoverflow.com/questions/57594228/spring-cloud-kubernetes-spring-cloud-gateway-unable-to-find-instance-for-k8s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!