Spring 云:默认从网关重定向到 UI

Spring Cloud: default redirecting from Gateway to UI

我不熟悉微服务和 Spring Boot。我在端口 8080 上有几个 Spring 带有 Zuul 网关 运行 的云微服务。

   browser
      |
      |
    gateway   (:8080)
     /   \
    /     \
   /       \
resource   UI (:8090)

在8090端口有一个UI微服务,里面有一个controller,里面有一个方法,返回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 微服务的控制器,returns index.html?

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

提前致谢!

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

管理

关于路由,Zuul只重定向http请求。 http://localhost:8080/ui/. On 8080 is running the proxy (Zuul) BUT /ui is the context path of resource server. Se when you make a call on this path http://localhost:8080/ui/ the proxy will redirect to resource server and will actually make a request to http://localhost:8090/ui/

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

还有一件事...您可以在 http 请求和 index.html 上使用相同的路径 (/ui) 以及您的浏览器何时访问 http://localhost:8080/ui/ a .js file with http request method will make an http request to http://localhost:8080/ui/ and then will be redirected to http://localhost:8090/ui/ and the response from the resource server will be rendered on the page from http://localhost:8080/ui/ .

我终于让我的代码工作了!感谢@pan 提到 Zuul Routing on Root Path 问题和@RzvRazvan 揭示 Zuul 路由是如何工作的。

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

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

Zuul 属性,用于从端口 8080 上的 网关微服务 重定向为 http://localhost:8080/ui/UI 微服务 ,它作为单独的 Spring 在端口 8090 上作为 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 微服务的控制器,returns 视图 index.html:

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

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

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

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

public文件夹的所有内容都是由npm run build任务从webpackvue.js[=62=生成的].第一次,我调用了我的 http://localhost:8080/ 我得到了 index.html200 OK 和所有其他静态资源的 404,因为他们被这样称呼:

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

所以配置错误 public webpack 中静态资源的路径。我在 config\index.js:

中更改了它
module.exports = {
  ...
  build: {
    ...
    assetsPublicPath: '/ui/',
    ...
  }
...
}

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

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