Spring Cloud:默认从网关重定向到UI [英] Spring Cloud: default redirecting from Gateway to UI
问题描述
我是微服务和Spring Boot的新手.我有一些Spring Cloud微服务,其Zuul网关运行在端口8080上.
I'm new to microservices and Spring Boot. I have a few Spring Cloud microservices with a Zuul gateway running on port 8080.
browser
|
|
gateway (:8080)
/ \
/ \
/ \
resource UI (:8090)
端口8090上有一个UI微服务,该微服务具有一个内部带有方法的控制器,返回index.html.
There is a UI microservice on port 8090, which has a controller with a method inside, returning index.html.
我为UI配置了这样的Zuul路由(我也在使用Eureka):
I configured Zuul routing like this for UI (I'm using Eureka too):
zuul:
routes:
ui:
path: /ui/**
serviceId: myproject.service.ui
stripPrefix: false
sensitiveHeaders:
如果我调用http://localhost:8080/ui/
,则一切正常,我看到了我的index.html的呈现.
If I call http://localhost:8080/ui/
everything works fine and I see rendering of my index.html.
是否可以通过某种方式配置Spring Cloud以使以下流程正常工作:调用http://localhost:8080/
会将我们重定向到UI微服务的控制器,该控制器返回index.html?
Is it possible to configure Spring Cloud in some way to make the following flow work: calling http://localhost:8080/
redirects us to controller of UI microservice, which returns index.html?
因此,想法是从网站的根目录打开UI.
So the idea is to open UI from the root of my site.
提前谢谢!
推荐答案
最后,我使代码工作了!感谢@pan提到根路径上的Zuul路由问题,以及@RzvRazvan揭示了Zuul路由的工作原理.
Finally, I've made my code work! Thanks to @pan for mentioning Zuul Routing on Root Path question and @RzvRazvan for revealing about how Zuul routing works.
我刚刚将 controller 添加到具有一个端点的Zuul路由网关微服务,以从根http://localhost:8080/
重定向到http://localhost:8080/ui/
:
I've just added controller to Zuul routed Gateway microservice with one endpoint to redirect from root http://localhost:8080/
to http://localhost:8080/ui/
:
@Controller
public class GateController {
@GetMapping(value = "/")
public String redirect() {
return "forward:/ui/";
}
}
Zuul 属性,用于从端口 8080 上的网关微服务重定向为http://localhost:8080/ui/
到 UI微服务在端口 8090 上作为单独的Spring Boot应用程序实现为http://localhost:8090/ui/
:
Zuul properties for redirecting from Gateway microservice on port 8080 as http://localhost:8080/ui/
to UI microservice, which implemented as a separate Spring Boot application on port 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
:
Eventually, this call http://localhost:8080/
redirects us to controller of UI microservice, which returns view index.html
:
@Controller
public class UiController {
@GetMapping(value = "/")
public String index() {
return "index.html";
}
}
实际上,在这种体系结构中呈现静态内容时我遇到了另一个问题,但是它与我使用
Actually, I had another problem with rendering static content in such architecture, but it was connected with configuration of my front-end, which I develop using Vue.js framework. I will describe it here in a few sentences, in case it might be helpful for someone.
我具有以下UI微服务的文件夹结构:
I have the following folders structure of UI microservice:
myproject.service.ui
└───src/main/resources
└───public
|───static
| ├───css
| └───js
└───index.html
public
文件夹的所有内容都是由npm run build
任务从 webpack 和 vue.js 生成的.第一次,我给我的http://localhost:8080/
打电话了,对于index.html
我得到了 200 OK ,对于所有其他静态资源我得到了 404 ,因为它们的调用方式是这样的:
All content of public
folder is generated by npm run build
task from webpack and vue.js. First time, I called my http://localhost:8080/
I got 200 OK for index.html
and 404 for all other static resources, because they was called like this:
http:\\localhost:8080\static\js\some.js
因此为webpack中的静态资产配置了错误的公共路径.我在config\index.js
中对其进行了更改:
So it was configured wrong public path for static assets in webpack. I changed it in config\index.js
:
module.exports = {
...
build: {
...
assetsPublicPath: '/ui/',
...
}
...
}
并且静态资产被适当地调用了.例如:
And static assets became to be called properly. e.g.:
http:\\localhost:8080\ui\static\js\some.js
这篇关于Spring Cloud:默认从网关重定向到UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!