【问题标题】:Spring Boot Application on Kubernetes How to use external message.properties file for supporting i18n and l10n?Kubernetes 上的 Spring Boot 应用程序如何使用外部 message.properties 文件来支持 i18n 和 l10n?
【发布时间】:2019-03-30 18:54:48
【问题描述】:

我们有一个部署到 Kubernetes 的 Spring Boot 应用程序。我们正在向此应用程序添加 i18n 功能,并希望将 messages.properties 文件放在应用程序 jar/war 之外。我已经能够在春季启动时做到这一点。当我在 Kubernetes 上部署它时,它将如何工作?我需要使用配置映射吗?以下是代码sn -p

@Configuration
public class AppConfig {
@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    //Path to the messages.properties files
    messageSource.setBasenames("file:/messages/messages", "classpath:messages");
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setCacheSeconds(60);
    return messageSource;
}
}

【问题讨论】:

    标签: java spring-boot kubernetes internationalization


    【解决方案1】:

    是的,您可以使用 configmap 执行此操作。这与访问外部 application.properties 文件非常相似。首先你可以create a ConfigMap directly from the file或者创建一个ConfigMap representing the file

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: treasurehunt-config
      namespace: default
    data:
      application.properties: |
        treasurehunt.max.attempts=5
    

    然后在您的 kubernetes 部署中创建Volume for the ConfigMapmount that into the Pod under the directory you use for the external configuration

              volumeMounts:
              - name: application-config
                mountPath: "/config"
                readOnly: true
          volumes:
          - name: application-config
            configMap:
              name: treasurehunt-config
              items:
              - key: application.properties
                path: application.properties
    

    这些 sn-ps 来自 application.properties 文件的 example of mounting a Volume from ConfigMap,因此它们使用弹簧引导 default external properties file path/config。您可以set that in the yaml for the mount,这样您就可以挂载文件以使用在 kubernetes 之外运行时已经使用的相同相对路径。

    【讨论】:

    • Ryan,如果我有 10 种语言的 messages.properties,我应该为所有这些语言创建配置映射吗?例如:一个用于messages.properties,一个用于messages_fr_FR.properties,依此类推。必须对应用程序代码进行哪些更改?我的应用程序如何理解它必须从 configMap 文件中读取,或者它对此不可知?我是 Kubernetes 新手,找不到这方面的信息
    • 您可以在同一个 configmap 中包含多个文件,它们都将被挂载到目录中。要向上面的示例添加更多文件,您需要将它们放在与 application.properties 相同的 yaml 缩进级别。应用程序可以像访问任何其他目录一样访问已安装的目录。它不需要知道目录是如何到达那里的。
    • Spring-boot 在启动时会在当前工作目录中愉快地抓取 application.properties 文件。获得该行为的正确 mountPath 或其他 K8S 魔法咒语是什么 - mountPath 是否应该只是“/”而不是“/config”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多