【问题标题】:Spring Cloud: default redirecting from Gateway to UISpring Cloud:默认从网关重定向到 UI
【发布时间】:2018-08-27 20:46:30
【问题描述】:

我是微服务和 Spring Boot 的新手。我有一些 Spring Cloud 微服务,其中 Zuul 网关在端口 8080 上运行。

 浏览器
      |
      |
    网关 (:8080)
     / \
    / \
   / \
资源用户界面 (:8090)

在 8090 端口上有一个 UI 微服务,里面有一个控制器,里面有一个方法,返回 index.html。

我为 UI 配置了这样的 Zuul 路由(我也在使用 Eureka):

zuul:
  routes:
    ui:
      path: /ui/**
      serviceId: myproject.service.ui
      stripPrefix: false
      sensitiveHeaders:

如果我打电话给http://localhost:8080/ui/,一切正常,我会看到我的 index.html 的渲染。

是否可以通过某种方式配置 Spring Cloud 以使以下流程工作:调用 http://localhost:8080/ 将我们重定向到 UI 微服务的控制器,该控制器返回 index.html?

所以想法是从我网站的根目录打开 UI。

提前致谢!

【问题讨论】:

  • @pan,非常感谢。非常有趣的解决方案,但据我了解,它不能提供 jscss 之类的 static 文件。不幸的是,它只能返回index.html。如果它想加载一些静态内容,由于某种原因重定向机制不起作用,浏览器得到404。

标签: spring spring-boot netflix-zuul


【解决方案1】:

终于,我的代码运行起来了!感谢@pan 提到了Zuul Routing on Root Path 问题,感谢@RzvRazvan 揭示了Zuul 路由的工作原理。

我刚刚将 controller 添加到 Zuul 路由网关微服务,其中一个端点可以从根 http://localhost:8080/ 重定向到 http://localhost:8080/ui/

@Controller
public class GateController {    
    @GetMapping(value = "/")
    public String redirect() {
        return "forward:/ui/";
    }    
}

Zuul 属性,用于从端口 8080 上的 Gateway 微服务 作为 http://localhost:8080/ui/ 重定向到 UI 微服务,其中在端口 8090 上作为单独的 Spring Boot 应用程序实现为 http://localhost:8090/ui/:

zuul:
  routes:
    ui:
      path: /ui/**
      serviceId: myproject.service.ui
      stripPrefix: false
      sensitiveHeaders:

UI 微服务的属性:

server:
  port: 8090
  servlet:
     contextPath: /ui

最终,这个调用 http://localhost:8080/ 将我们重定向到 UI 微服务的控制器,它返回视图 index.html

@Controller
public class UiController {
    @GetMapping(value = "/")
    public String index() {
        return "index.html";
    }
}

实际上,我在这种架构中渲染静态内容时遇到了另一个问题,但它与我使用 Vue.js 框架开发的前端的配置有关。我会在这里用几句话来描述它,以防它对某人有帮助。

我有以下 UI 微服务的文件夹结构:

myproject.service.ui
    └───src/main/resources
        └───public
            |───static
            |    ├───css
            |    └───js
            └───index.html

public 文件夹的所有内容都是由 webpackvue.js 中的npm run build 任务生成的。第一次,我打电话给我的http://localhost:8080/ 我得到了index.html200 OK和所有其他静态资源的404,因为它们是这样调用的:

http:\\localhost:8080\static\js\some.js

所以它在 webpack 中为静态资源配置了错误的公共路径。我在config\index.js改了:

module.exports = {
  ...
  build: {
    ...
    assetsPublicPath: '/ui/',
    ...
  }
...
}

并且静态资产变得可以正确调用。例如:

http:\\localhost:8080\ui\static\js\some.js

【讨论】:

    【解决方案2】:

    如果您想在 Zuul 上拥有 UI(前端),您可以在 resources/static 文件夹(html、css 和 js 文件)中添加静态内容。通过这种方式,您的代理能够呈现 index.html(当然,您必须在静态文件夹中有一个 index.html)。 O 这种方式在http://localhost:8080 上,代理将呈现 index.html;你也可以有其他路径,但所有这些路径都由 index.html 管理)

    关于路由,Zuul 只重定向 http 请求。 http://localhost:8080/ui/。在8080 上运行代理(Zuul)但/ui 是资源服务器的上下文路径。 Se 当您在此路径上调用 http://localhost:8080/ui/ 时,代理将重定向到资源服务器并实际向 http://localhost:8090/ui/

    发出请求

    是浏览器路径和http请求路径的区别。浏览器路径由 index.html 管理,http 请求由 Zuul 管理。我不知道我是否足够明确。

    还有一件事...您可以在 http 请求和 index.html 上使用相同的路径 (/ui),并且当您的浏览器访问 http://localhost:8080/ui/ 时,您可以使用 http 访问 .js 文件request 方法将向 http://localhost:8080/ui/ 发出 http 请求,然后将被重定向到 http://localhost:8090/ui/ 并且来自资源服务器的响应将在 的页面上呈现>http://localhost:8080/ui/.

    【讨论】:

    • 感谢您的回答。所以,根据我的架构,不可能将静态内容存储在 UI 微服务中,并尝试通过调用 http://localhost:8080/ui 来获取它,不是吗?顺便说一句,当我调用 http://localhost:8090/ui/ 时,我也无法渲染整个 UI,因为所有静态资源都显示 404,除了 index.html
    • 当您在localhost:8090/ui 拨打电话时,您将收到来自UI 服务器的数据(例如json);当您在localhost:8080/ui 拨打电话时,实际上您是在 Zuul 拨打电话,它将请求重定向到 UI 服务器(在端口 8090 上)。所以,第一个问题是:你在哪里调用 8080 端口?在 JavaScript 文件中使用方法?第二个问题是:你在哪里存储静态内容?你有第二台服务器,比如 node.js 吗?您在问题中的详细信息非常模糊...您还说您可以打开 index.html 但在其他您收到 404;阅读有关 WebMvcConfigurerAdapter
    • 1. 例如,我在浏览器中调用了http://localhost:8080/ui/,因此 Zuul 将我重定向到 UI 微服务,该微服务实现为单独的 Spring Boot 应用程序。 2. 所以我将所有静态内容存储在src/main/resources/public/static 文件夹中。当我调用 UI 时,浏览器向我显示(在 Chrome 控制台中)它无法获取静态内容。它试图从例如获得静态http://localhost:8080/static/... 得到 404。幸运的是,我已经解决了这个问题,现在一切正常!
    猜你喜欢
    • 2021-05-27
    • 2017-10-18
    • 2018-07-17
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多