繁体   English   中英

在 Kube.netes 集群中运行的 Thymeleaf 应用程序中处理服务器上下文

[英]Handle server context in a Thymeleaf application running in a Kubernetes cluster

我有一个 Spring Boot 应用程序,前端为 Thymeleaf。 我需要将应用程序部署到 Kube.netes 集群,以便可以通过my-k8s-cluster.com/my-application之类的路径访问它。

上下文路径/my-application是在 Kube.netes 端配置的,不是应用程序要了解的主题。 它可以在不更改应用程序代码的情况下独立更改。

问题:无论我使用的是 上下文相关的 URL 还是服务器相关的 URL ,Thymeleaf 都指向服务器根目录。 当应用程序在 my-k8s-cluster.com/my-application 上运行时, my-k8s-cluster.com/my-applicationmy-k8s-cluster.com/中查找其资源,但未能加载它们。

问题:如何配置 Thymeleaf 以不同的上下文根独立运行?

更新:这是我的 Ingress 配置的样子:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: my-k8s-cluster.com
      http:
        paths:
          - path: /my-application/?(.*)
            backend:
              serviceName: my-service
              servicePort: 80

如果我正确理解您的问题,下一个解决方案可能对您有用。

根据Spring Boot documentation ,可以通过属性文件更改context-path

默认情况下context-path\ ,但是使用具有应用程序属性的文件中的下一个设置server.servlet.context-path可以更改它。

例如,为了使用/my-application作为上下文路径,应该将下一个属性server.servlet.context-path=/my-application添加到application.properties中。

同样在Spring 引导文档中有几种更改此属性的方法:

例如,它可以通过OS environment variablescommand-line arguments等来完成。

因此,可以通过在 Kube.netes 中为容器定义环境变量来设置此属性。 更多信息可以在这里找到

此外,Spring Cloud 支持从 Kube.netes ConfigMap 读取属性。 Spring Cloud Kube.netes描述了这个案例

你能试试这个吗

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: my-k8s-cluster.com
      http:
        paths:
          - path: /my-application/(.+)
            backend:
              serviceName: my-service
              servicePort: 80

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM