API 使用 Spring Cloud + Zuul + Consul 的网关:使用 HTTPS 时动态路由不起作用

API Gateway using Spring Cloud + Zuul + Consul : dynamic routing not working when using HTTPS

我目前正在开发 API 网关以集中对 REST API 的调用。我们正在使用 Spring Cloud(Edgware.SR3 版本),使用 Zuul (1.3.0) 来处理服务发现,并使用 Consul 作为服务注册中心。

在第一个版本中,每个服务的路由都使用 zuul.routes.myApiName.url 在网关配置中注册,并且工作正常。

然后,我们想使用动态路由来允许每个 API.

有多个实例

我为此删除了 zuul.routes.myApiName.url。 问题是我通过网关调用 API 返回错误:

Bad Request This combination of host and port requires TLS.

这里是 API 在 Consul 注册的配置:

spring.cloud.discovery.enabled=true
spring.cloud.consul.host=@consul_host_ip@
spring.cloud.consul.port=@consul_host_port@
spring.cloud.consul.discovery.scheme=https

这里是网关中Zuul路由的配置:

zuul.host.socket-timeout-millis=60000
zuul.add-proxy-headers=false
zuul.ignored-services=*
zuul.routes.myApiName.path=/myApiName/**
zuul.routes.myApiName.serviceId=myApiName

API 已在 Consul 中正确注册并且健康检查正在成功使用 HTTPS :

HTTP GET https://hostname:port/health: 200  Output: {"description":"Composite Discovery Client","status":"UP"}

证书也配置得很好,因为我可以使用 HTTPS 直接调用我的 API。 但似乎 Zuul 重定向使用的是 HTTP 而不是 HTTPS(如果我使用 HTTP 以直接模式(无网关)调用我的 API,我会遇到同样的错误)。

我已经为此苦苦挣扎了一段时间,所以我想知道是否缺少强制 Zuul 在对 API 的路由调用中使用 HTTPS 的配置?

提前致谢!

只需配置

  zuul.addProxyHeaders = false

因为 Zuul 在将任何请求转发到后端时添加了代理 headers。

我终于找到了解决方案:我必须添加这个 属性: ribbon.IsSecure=true