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

And*_*rew 3 spring spring-boot netflix-zuul

我是微服务和 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:
Run Code Online (Sandbox Code Playgroud)

如果我调用http://localhost:8080/ui/一切正常并且我看到我的 index.html 的渲染。

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

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

提前致谢!

And*_*rew 5

最后,我让我的代码工作了!感谢@pan 提及Zuul Routing on Root Path问题和 @RzvRazvan 揭示 Zuul 路由如何工作。

我刚刚将控制器添加到 Zuul 路由网关微服务,并通过一个端点从 root 重定向http://localhost:8080/http://localhost:8080/ui/

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

Zuul从重定向特性网关的microService端口8080作为http://localhost:8080/ui/UI微服务,其实现端口隔离的弹簧启动应用程序8090http://localhost:8090/ui/

zuul:
  routes:
    ui:
      path: /ui/**
      serviceId: myproject.service.ui
      stripPrefix: false
      sensitiveHeaders:
Run Code Online (Sandbox Code Playgroud)

UI微服务的属性:

server:
  port: 8090
  servlet:
     contextPath: /ui
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

myproject.service.ui
    ????src/main/resources
        ????public
            |???static
            |    ????css
            |    ????js
            ????index.html
Run Code Online (Sandbox Code Playgroud)

public文件夹的所有内容都是由webpackvue.js 的npm run build任务生成的。第一次,我调用了我的I got 200 OK for和404 for all other static resources,因为它们是这样调用的:http://localhost:8080/index.html

http:\\localhost:8080\static\js\some.js
Run Code Online (Sandbox Code Playgroud)

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

module.exports = {
  ...
  build: {
    ...
    assetsPublicPath: '/ui/',
    ...
  }
...
}
Run Code Online (Sandbox Code Playgroud)

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

http:\\localhost:8080\ui\static\js\some.js
Run Code Online (Sandbox Code Playgroud)