mvc重定向位置和额外的k8s入口路径

qcbq4gxm  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(169)

我正在开发一个简单的spring引导应用程序,并尝试在集群中部署它。
控制器如下所示:

  1. @Controller
  2. @RequestMapping("/")
  3. @RequiredArgsConstructor
  4. public class UiController {
  5. private final ScenarioRepository scenarioRepository;
  6. @GetMapping
  7. public String index() {
  8. return "redirect:/scenarios";
  9. }
  10. @GetMapping("/debug")
  11. public String debug(HttpServletRequest request) {
  12. log.info("RequestURI: {}", request.getRequestURI());
  13. log.info("Context path: {}", request.getContextPath());
  14. log.info("Servlet path: {}", request.getSession());
  15. log.info("Path translated: {}", request.getPathTranslated());
  16. log.info("Path info: {}", request.getPathInfo());
  17. return "redirect:/scenarios";
  18. }
  19. @GetMapping("/scenarios")
  20. public String scenarios() {
  21. List<Scenario> scenarios = scenarioRepository.findAll();
  22. model.addAttribute("scenarios", scenarios);
  23. return "scenarios";
  24. }
  25. }

入口配置:

  1. apiVersion: extensions/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: myservice
  5. namespace: dev
  6. labels:
  7. app.kubernetes.io/name: myservice
  8. app.kubernetes.io/instance: myservice
  9. app.kubernetes.io/version: "1.0"
  10. annotations:
  11. kubernetes.io/ingress.class: nginx
  12. nginx.ingress.kubernetes.io/rewrite-target: /$1
  13. spec:
  14. rules:
  15. - host: dev.cluster
  16. http:
  17. paths:
  18. - path: /apps/myservice/(.*)
  19. backend:
  20. serviceName: myservice
  21. servicePort: http

没有额外的web/spring配置。
在这种配置下,我期望以下行为:
本地:请求 http://localhost/ 重定向到 http://localhost/scenarios 群集:请求 http://dev.cluster/apps/myservice/ 重定向到 http://dev.cluster/apps/myservice/scenarios 在我的ide中,它可以正常工作。但当我将它部署到k8s索引请求时,它会将我重定向到 http://dev.cluster/scenarios 缺少入口路径 /apps/myservice/ .
访问 /debug 在集群中生成以下输出:

  1. 12:43:21.338 Servlet path: org.apache.catalina.session.StandardSessionFacade@12da0eae
  2. 12:43:21.338 Path translated: null
  3. 12:43:21.338 Path info: null
  4. 12:43:21.236 RequestURI: /debug
  5. 12:43:21.236 Context path:

响应的位置标头: http://dev.cluster/scenarios . 很明显,spring对入口重写一无所知,并生成非法的重定向url。
所以,问题是:如何解决这个配置问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题