【问题标题】:spring cloud gateway + eureka + microservice on heroku is putting the port in the url result is error 503 on request to microservice via gatewayheroku 上的 spring cloud gateway + eureka + microservice 将端口放入 url 结果是错误 503 on request to microservice via gateway
【发布时间】:2020-12-07 01:15:57
【问题描述】:

我有 3 个服务部署到 Heroku。

  • Spring 云网关
  • 尤里卡
  • 微服务

网关和微服务在eureka中注册。

网关应用程序.yml

server:
  port: ${PORT:8080}

eureka:
  instance:
    hostname: gateway.herokuapp.com
    homePageUrl: https://${eureka.instance.hostName}/
  client:
    serviceUrl:
      defaultZone: http://eureka.herokuapp.com/eureka/
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: employeeModule
          uri: lb://MICROSERVICE
          predicates:
            - Path=/employee/**
logging:
  level:
    org.springframework.cloud.gateway: TRACE

EUREKA application.yml

server:
  port: ${PORT:8761}

eureka:
  instance:
    hostname: ${EUREKA_HOSTNAME:localhost}
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: ${EUREKA_URI:http://${eureka.instance.hostname}:${server.port}/eureka/}

微服务应用程序.yml

spring:
  application:
    name: MICROSERVICE
server:
  port: ${PORT:8081}

eureka:
  instance:
    hostname: microservice.herokuapp.com
    homePageUrl: https://${eureka.instance.hostName}/
    home-page-url-path: https://${eureka.instance.hostName}
  client:
    serviceUrl:
      defaultZone: http://eureka.herokuapp.com/eureka/

当我直接拨打https://MICROSERVICEURL/employee/message时,我得到了正确的回应

当我调用网关https://GATEWAYURL/employee/message时,它应该向eureka询问微服务的地址,然后将请求路由到微服务以获得响应。

但不是路由到https://MICROSERVICEURL/employee/message,而是路由到带有随机heroku端口的eureka版本。例如:https://MICROSERVICEURL:RANDOMHEROKUPORT/employee/message。由于超时,这会导致错误 503。

有没有办法告诉网关忽略端口号? 还是我做错了什么?

网关日志

2020-08-18T19:59:56.683925+00:00 app[web.1]: 2020-08-18 19:59:56.683 TRACE 4 --- [or-http-epoll-1] o.s.c.g.filter.LoadBalancerClientFilter  : LoadBalancerClientFilter url chosen: http://microservice.herokuapp.com:41632/employee/message
2020-08-18T20:00:26.673252+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/employee/message" host=gateway.herokuapp.com request_id=236faa79-1d66-4e30-8c32-f879228d08db fwd="79.205.56.29" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0 protocol=https

【问题讨论】:

  • 你在微服务中有server.port: ${PORT:8081}。 $PORT 可能来自 heroku。如果您想要端口 80 或 443,请将其设置为 server.port
  • 如果你使用heroku,你为什么要使用eureka? Heroku 已经为每个服务提供了一个平衡器,并且您已经在使用 heroku dns。我建议你放弃 eureka 并在 apigateway 中使用服务的 dns 名称
  • @spencergibb 我现在尝试将 server.port 设置为 80、443,只是 $PORT 以便它可以通过 Heroku 变量绑定。它仍然尝试通过这个 heroku 端口连接并超时。我编辑了我的问题并添加了网关日志的输出。
  • @cool 我必须使用尤里卡,因为使用尤里卡是我大学任务的一部分。我不必使用heroku,但我想使用,因为数据库实现比大学数据库服务器好得多。
  • 啊,我搞错了。您需要单独留下server.port,因为这是实际进程正在侦听的端口。您需要设置eureka.instance.non-secure-port=80eureka.instance.secure-port=443,因为这是报告给eureka 的端口。

标签: heroku microservices spring-cloud netflix-eureka spring-cloud-gateway


【解决方案1】:

解决方案是在 appllication.yml 中为 eureka 实例设置安全和非安全端口

微服务应用程序.yml

spring:
  application:
    name: MICROSERVICE
server:
  port: ${PORT:8081}

eureka:
  instance:
    hostname: microservice.herokuapp.com
    homePageUrl: https://${eureka.instance.hostName}/
    home-page-url-path: https://${eureka.instance.hostName}
  client:
    serviceUrl:
      defaultZone: http://eureka.herokuapp.com/eureka/
      non-secure-port: 80
      secure-port: 443

【讨论】:

    【解决方案2】:

    您只需要添加非安全端口并确保在 application.properties 或 .yml 中启用它

    eureka.instance.non-secure-port-enabled=true
    eureka.instance.secure-port-enabled=false
    eureka.instance.non-secure-port=80
    

    您也可以在安全端口上运行它,但您只能运行 1 个服务。

    My application.properties

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 2020-11-04
      • 2021-05-19
      • 2021-12-09
      • 2021-08-27
      • 1970-01-01
      • 2021-02-25
      • 2018-11-20
      • 2021-07-13
      相关资源
      最近更新 更多