Spring Cloud Gateway + Eureka + Heroku上的微服务将端口放入url结果是通过网关请求微服务时出现错误503

问题描述

我已经将3个服务部署到了Heroku。

  • Spring cloud Gateway
  • 尤里卡
  • 微服务

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

GATEWAY application.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/}

MICROSERVICE application.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

解决方法

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

MICROSERVICE application.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
,

您只需要添加非安全端口并确保已在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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...