Spring Cloud Zuul 不会在后端服务响应中重写 URL [英] Spring Cloud Zuul not rewriting URLs in backend service response

查看:106
本文介绍了Spring Cloud Zuul 不会在后端服务响应中重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用几个 Spring Cloud 服务(配置、服务注册表和 Zuul)设置了一个本地环境.代理使用默认映射正确公开服务:

I have setup a local environment with a couple Spring Cloud services (config, service registry, and Zuul). The proxy correctly exposes the services using the default mapping:

2015-11-23 14:06:30.184  INFO 11532 --- [           main] o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapped URL path [/api-proxy/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
2015-11-23 14:06:30.184  INFO 11532 --- [           main] o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapped URL path [/service-discovery/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
2015-11-23 14:06:30.184  INFO 11532 --- [           main] o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapped URL path [/config-service/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
2015-11-23 14:06:30.184  INFO 11532 --- [           main] o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapped URL path [/circuitbreaker-collector/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
2015-11-23 14:06:30.184  INFO 11532 --- [           main] o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapped URL path [/circuitbreaker-dashboard/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]

我看到的问题是当我去http://localhost/service-discovery/我看到 Eureka 仪表板,但没有任何相关的静态资源加载.浏览器显示如下错误:

The problem I am seeing is that when I go to http://localhost/service-discovery/ I see the Eureka dashboard but none of the related static resources load. Browser shows errors like:

http://localhost/eureka/css/wro.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost/eureka/js/wro.js Failed to load resource: the server responded with a status of 404 (Not Found)
service-discovery:223 Uncaught ReferenceError: $ is not defined
http://localhost/eureka/css/wro.css Failed to load resource: the server responded with a status of 404 (Not Found)

有没有办法解决这个问题?据我所知,Eureka 仪表板在加载资源时没有使用相对路径,而是假设应用程序部署到根 servlet 上下文.

Is there a way to get around this issue? From what I can tell Eureka dashboard isn't using a relative path when loading resources and instead assumes the app is deployed to the root servlet context.

更新我意识到我想念代表了问题.我对这个帖子发表了评论,但我想我会把它带到主要帖子中.问题是 Zuul 代表客户端发出后端请求.当 Zuul 发出该请求时,HTML 响应包含与为 Eureka 配置的上下文路径相关的 URL.通常代理服务会通过重写在响应中找到的任何 URL 来管理这个,以获得关于代理如何公开它的正确基本上下文.Zuul 有这样的东西吗?

UPDATE I realized I miss represented the problem. I commented on this thread but thought I would bring it up to the main post. The issue is that Zuul makes the backend request on behalf of the client. When Zuul makes that request the HTML response contains URL's that are relative to the context path that was configured for Eureka. Normally proxy services would manage this by re-writing any URLs found in the response to have the correct base context in regard to how the proxy is exposing it. Does something like this exist for Zuul?

更改主题以更好地描述问题.

Changed subject to better describe the issue.

推荐答案

我认为原因是主页和静态文件之间的上下文路径不同,我通过将它们分开来解决这个问题:

I think the reason is that the context-paths are different between home page and static file, I solved that problem by setting up them apart:

registry:
  path: /registry/**
  url: http://localhost:8006/
  strip-prefix: true
registry-static:
  path: /eureka/**
  url: http://localhost:8006/eureka/

这篇关于Spring Cloud Zuul 不会在后端服务响应中重写 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆