Spring Cloud: default redirecting from Gateway to UI

前端 未结 2 1517
时光说笑
时光说笑 2021-01-14 08:50

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         


        
2条回答
  •  抹茶落季
    2021-01-14 09:23

    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.

    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 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 microservice's properties:

    server:
      port: 8090
      servlet:
         contextPath: /ui
    

    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.

    I have the following folders structure of UI microservice:

    myproject.service.ui
        └───src/main/resources
            └───public
                |───static
                |    ├───css
                |    └───js
                └───index.html
    

    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
    

    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
    

提交回复
热议问题