首页 文章

Spring Cloud:默认从Gateway重定向到UI

提问于
浏览
0

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

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?

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

提前致谢!

2 回答

  • 1

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

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

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

    Zuul 属性,用于从端口 8080 上的 Gateway microservice 重定向为 http://localhost:8080/ui/UI microservice ,在端口 8090 上实现为单独的Spring Boot应用程序 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 文件夹的所有内容均由 webpack 任务 webpackvue.js 生成 . 第一次,我打电话给我 http://localhost:8080/ 我为所有其他静态资源获得 index.html404 ,因为它们被调用如下:

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

    因此,为webpack中的静态资产配置了错误的公共路径 . 我在 config\index.js 更改了它:

    module.exports = {
      ...
      build: {
        ...
        assetsPublicPath: '/ui/',
        ...
      }
    ...
    }
    

    静态资产变得恰到好处 . 例如 . :

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

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

    关于路由,Zuul只重定向http请求 . http://localhost:8080/ui/ . 在 8080 上运行代理(Zuul)但是 /ui 是资源服务器的上下文路径 . 当您在此路径上进行呼叫时,代理将重定向到资源服务器并实际发出请求http://localhost:8090/ui/

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

    还有一件事......您可以在http请求和index.html上使用相同的路径( /ui ),当您的浏览器使用http请求方法访问http://localhost:8080/ui/ a .js文件时,将向http://localhost:8080/ui/发出http请求,然后将重定向到http://localhost:8090/ui/,资源服务器的响应将在http://localhost:8080/ui/的页面上呈现 .

相关问题