我在Heroku部署了3个服务。
- Spring云网关
- 尤里卡
- 微型服务
网关和微服务在尤里卡注册。
GATEWAY应用程序.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应用程序.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应用程序.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
解决方案是在application.yml 中为eureka实例设置安全和非安全端口
MICROSERVICE应用程序.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个服务。
我的应用程序属性