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服务,我仍然收到此错误:
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
是Angular应用.
uiservice
is Angular app.
当我查看.../actuator/gateway/routes
时,我得到以下结果:
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
},
....
]
请注意,由于以下原因而发现了很多服务:"route_id": "CompositeDiscoveryClient_gateway"
和"route_id": "CompositeDiscoveryClient_uiservice"
,这些路由不是我的(我没有定义它们).
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.
我的配置:
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"
Also, how can I disable gateway autodiscovering? I don't want the "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>
感谢您的帮助
推荐答案
我在失去一个下午后终于找到了解决方案.我认为在使用功能区时发现服务存在问题.我使用k8s dns服务发现而不是依赖Ribbon,所以我的新配置是:
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配置:
K8s uiservice config:
apiVersion: v1
kind: Service
metadata:
name: uiservice
spec:
sessionAffinity: ClientIP
selector:
app: uiservice
ports:
- name: http
port: 4200
targetPort: ui-port
一个新的问题出现了:由于k8s服务本身就是这样做,为什么要使用Ribbon来平衡请求?
A new question arised: Why using Ribbon to load balance requests since k8s services natively do that?
这篇关于Spring Cloud Kubernetes + Spring Cloud Gateway:无法找到k8s服务的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!