我正在开发一个微服务,我试图使用 Zuul 代理来路由服务。但由于某种原因,Zuul无法路由到eureka中注册的应用程序。 我尝试增加 hystrix 和 Zuul 超时但没有任何效果
抛出异常
com.netflix.zuul.exception.ZuulException: Forwarding error at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.handleException(RibbonRoutingFilter.java:198) ~[spring-cloud-netflix-zuul-2.2.10.RELEASE.jar:2.2.10.RELEASE]
Zuul应用程序.yml
server:
port: 8082
spring:
application:
name: gateway
eureka:
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
hostname: localhost
ribbon:
eureka:
enabled: true
zuul:
ignoredServices: "*"
host:
connect-timeout-millis: 5000000
socket-timeout-millis: 5000000
prefix: /api
routes:
game:
path: /game/**
serviceId: game
trend:
path: /trend/**
serviceId: trend
这里我有两个名为“game”和“trend”的微服务,它们都在 eureka 服务中注册,但无法通过 Zuul 代理导航到应用程序。
最佳答案
经过一些研究,我发现 Ribbon、Zuul 处于维护模式,我们不会得到任何新版本,Spring 也为 Ribbon、Zuul 等提供了替代方案。请参阅此链接 spring alternative用于负载平衡、代理服务器等
对于这个问题,我使用了 spring cloud gateway 而不是 Zuul,这是 spring 提供的替代方案
所需的依赖项
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.1.0</version>
</dependency>
现在代理服务器的应用程序yml看起来像
server:
port: 8082
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: game
uri: lb://game
predicates:
- Path=/game/**
- id: trend
uri: lb://trend
predicates:
- Path=/trend/**
eureka:
client:
serviceURL:
defaultZone: http://localhost:8761/eureka
关于java - Zuul无法路由到在 Eureka 服务器中注册的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71195587/