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

2023-12-15

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


   browser
      |
      |
    gateway   (:8080)
     /   \
    /     \
   /       \
resource   UI (: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?

所以我们的想法是从我网站的根目录打开用户界面。

提前致谢!


最后,我让我的代码工作了!感谢@pan 提及根路径上的 Zuul 路由问题和@RzvRazvan 揭示了 Zuul 路由的工作原理。

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

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

Zuul用于重定向的属性网关微服务在港口8080 as http://localhost:8080/ui/ to 用户界面微服务,它在端口上作为单独的 Spring Boot 应用程序实现8090 as 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文件夹是由生成的npm run build任务来自webpack and vue.js。第一次,我打电话给我的http://localhost:8080/ I got 200 OK for index.html and 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Cloud:默认从网关重定向到 UI 的相关文章

随机推荐