spring 云网关 + eureka + heroku 上的微服务正在将端口放入 url 结果是通过网关请求微服务时出现错误 503
spring cloud gateway + eureka + microservice on heroku is putting the port in the url result is error 503 on request to microservice via gateway
我有 3 个服务部署到 Heroku。
- Spring云网关
- 尤里卡
- 一个微服务
网关和微服务在eureka中注册。
网关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
尤里卡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/}
微服务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 实例设置安全和非安全端口
微服务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
我有 3 个服务部署到 Heroku。
- Spring云网关
- 尤里卡
- 一个微服务
网关和微服务在eureka中注册。
网关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
尤里卡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/}
微服务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 实例设置安全和非安全端口微服务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