我正在使用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"
,这些路由不是我的(我没有定义它们)。
我看了这篇文章: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>
感谢您的帮助
答案 0 :(得分:1)
设置以下属性
spring:
cloud:
gateway:
discovery:
locator:
enabled: false
答案 1 :(得分:0)
我在失去一个下午后终于找到了解决方案。我认为在使用功能区时发现服务存在问题。我使用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来平衡请求?
答案 2 :(得分:0)
应该看起来像这样:
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