最后,我让我的代码工作了!感谢@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