【问题标题】:How Eureka Server and Spring Cloud API Gateway communicates with each other?Eureka Server 和 Spring Cloud API Gateway 是如何相互通信的?
【发布时间】:2021-10-02 07:19:06
【问题描述】:

Spring Boot 微服务架构中,我们通常通过 eureka.client.register-with-eureka=trueeureka.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-boot microservices netflix-eureka spring-cloud-gateway


    【解决方案1】:
    spring:
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true
    

    在 API Gateway 中启用 Discovery Locator 后,除非绝对需要,否则您无需手动配置路由。

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

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

    然后如果 order-service getOrders 端点:http://localhost:8080/orders,那么在启用 Discovery Locator 的情况下,需要使用以下 URL 通过 API 网关路由请求:https://localhost:8082/order -service/orders,即{ApiGatewayHost}/{EurekaServiceId}/{ActualEndpoint}

    【讨论】:

      【解决方案2】:

      将Gatway注册到Eureka Server

      【讨论】:

        猜你喜欢
        • 2020-11-04
        • 2021-12-02
        • 2019-06-24
        • 2021-12-09
        • 2019-01-29
        • 2018-06-29
        • 2015-09-08
        • 2020-04-30
        • 1970-01-01
        相关资源
        最近更新 更多