Spring Cloud Gateway Kubernetes Eureka java.net.UnknownHostException:解析失败
Posted
技术标签:
【中文标题】Spring Cloud Gateway Kubernetes Eureka java.net.UnknownHostException:解析失败【英文标题】:Spring Cloud Gateway Kubernetes Eureka java.net.UnknownHostException: failed to resolve 【发布时间】:2021-12-09 19:32:08 【问题描述】:我正在开发一个具有微服务架构的项目。我有 Spring 云网关、eureka 服务发现和预订微服务。当我在本地运行这些应用程序时,一切正常。当我用 docker compose 运行这些仍然没有问题。但是当我将这些部署到 kubernetes 时,我在 api 网关上遇到错误。当我向 API 网关上的 /book 发送 get 请求时出现此错误
api 网关错误:
java.net.UnknownHostException: failed to resolve 'book-service-55665db7ff-bd75t' after 2 queries
at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1046) ~[netty-resolver-dns-4.1.68.Final.jar!/:4.1.68.Final]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ HTTP GET "/book" [ExceptionHandlingWebHandler]
api网关配置:
server:
port: 8084
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: book
uri: lb://BOOK-SERVICE/book
predicates:
- Path=/book/**
- id: author
uri: lb://BOOK-SERVICE/author
predicates:
- Path=/author/**
- id: genre
uri: lb://BOOK-SERVICE/genre
predicates:
- Path=/genre/**
预订微服务配置:
spring:
application:
name: BOOK-SERVICE
datasource:
url: jdbc:postgresql://localhost:5432/book_service
username: postgres
password: password
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQL10Dialect
server:
port: 8081
kubernetes 清单:
#secret
apiVersion: v1
kind: Secret
metadata:
name: database
type: Opaque
data:
password: cGFzc3dvcmQ=
---
#api gateway
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-gateway
labels:
app: api-gateway
spec:
replicas: 1
selector:
matchLabels:
app: api-gateway
template:
metadata:
labels:
app: api-gateway
spec:
containers:
- name: api-gateway
image: rikaciv802/api-gateway
ports:
- containerPort: 8084
env:
- name: eureka.client.serviceUrl.defaultZone
value: http://service-discovery:8761/eureka
---
apiVersion: v1
kind: Service
metadata:
name: api-gateway
spec:
selector:
app: api-gateway
type: LoadBalancer
ports:
- protocol: TCP
port: 8084
targetPort: 8084
nodePort: 30000
---
#service discovery
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-discovery
labels:
app: service-discovery
spec:
replicas: 1
selector:
matchLabels:
app: service-discovery
template:
metadata:
labels:
app: service-discovery
spec:
containers:
- name: service-discovery
image: rikaciv802/service-discovery
ports:
- containerPort: 8761
---
apiVersion: v1
kind: Service
metadata:
name: service-discovery
spec:
selector:
app: service-discovery
ports:
- protocol: TCP
port: 8761
targetPort: 8761
---
#book service
apiVersion: apps/v1
kind: Deployment
metadata:
name: book-service-database
labels:
app: book-service-database
spec:
replicas: 1
selector:
matchLabels:
app: book-service-database
template:
metadata:
labels:
app: book-service-database
spec:
containers:
- name: book-service-database
image: postgres:14.0
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: book_service
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: database
key: password
---
apiVersion: v1
kind: Service
metadata:
name: book-service-database
spec:
selector:
app: book-service-database
ports:
- protocol: TCP
port: 5432
targetPort: 5432
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: book-service
labels:
app: book-service
spec:
replicas: 1
selector:
matchLabels:
app: book-service
template:
metadata:
labels:
app: book-service
spec:
containers:
- name: book-service
image: rikaciv802/book-service
ports:
- containerPort: 8081
env:
- name: eureka.client.serviceUrl.defaultZone
value: http://service-discovery:8761/eureka
- name: SPRING_DATASOURCE_URL
value: jdbc:postgresql://book-service-database:5432/book_service
- name: SPRING_DATASOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: database
key: password
---
apiVersion: v1
kind: Service
metadata:
name: book-service
spec:
selector:
app: book-service
ports:
- protocol: TCP
port: 8081
targetPort: 8081
服务发现配置:
eureka:
client:
register-with-eureka: false
fetch-registry: false
server:
port: 8761
【问题讨论】:
【参考方案1】:您的 pod 主机名似乎存在 DNS 问题。在 kubernetes 中,pod 的 DNS 名称通常格式如下:pod-ip-address.deployment-name.my-namespace.svc.cluster-domain.example
(参见https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pods)
您可以使用 IP 地址解决此问题:将 eureka.instance.preferIpAddress
设置为 true
。
见https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html#spring-cloud-eureka-server-prefer-ip-address
【讨论】:
我需要在服务发现或 eureka 客户端中设置吗? 在尤里卡客户端上。所以他们提供他们的 IP 而不是主机名以上是关于Spring Cloud Gateway Kubernetes Eureka java.net.UnknownHostException:解析失败的主要内容,如果未能解决你的问题,请参考以下文章
spring cloud gateway 某些路由中跳过全局过滤器
spring cloud gateway 报错 Unable to find GatewayFilterFactory with name