Eureka Server和Spring Cloud API Gateway如何相互通信?



InSpring Boot微服务架构我们通常通过eureka.client.register-with-eureka=true,eureka.client.fetch-registry=trueeureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka将每个微服务(它的许多实例)注册到Eureka服务器。因此Eureka充当这些服务的服务注册中心(服务名称、主机名及其IP)。

Spring Cloud API Gateway作为任何微服务调用的单一入口点。它可以作为代理服务,将请求路由到相关的微服务,抽象生产者的详细信息。它只有路由信息,那么Spring Cloud API网关如何知道要调用哪个微服务实例?API Gateway和Eureka如何通信和负载平衡?
spring:
application:
name: api-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/users/**

- id: order-service
uri: lb://department-service
predicates:
- Path=/departments/**
spring:
cloud:
gateway:
discovery:
locator:
enabled: true

在API Gateway中启用发现定位器后,除非绝对需要,否则您不必手动配置路由。

API Gateway知道将传入请求路由到哪个Eureka Service的方式如下:

假设,订单服务运行在http://localhost:8080上API网关运行在http://localhost:8082上Eureka订单服务的服务名称- order-service

然后,如果订单服务getOrders端点:http://localhost:8080/orders,那么发现定位器启用后,请求需要通过API网关通过以下URL路由:https://localhost:8082/order-service/orders,即{ApiGatewayHost}/{EurekaServiceId}/{ActualEndpoint}.

Eureka Server注册网关

最新更新