Kubernetes上的Spring Boot应用程序如何使用外部message.properties文件来支持i18n和l10n? [英] Spring Boot Application on Kubernetes How to use external message.properties file for supporting i18n and l10n?

查看:328
本文介绍了Kubernetes上的Spring Boot应用程序如何使用外部message.properties文件来支持i18n和l10n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a spring boot application that is deployed to Kubernetes. We are adding i18n capabilities to this application and want to place the messages.properties file outside the application jar/war. I have been able to do that in spring boot. How will this work when I deploy it on Kubernetes? Do I need to use the configmaps? Following is the code snippet

@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;
}
}

推荐答案

是的,您可以使用configmap来做到这一点.它与访问外部application.properties文件几乎相同.首先,您可以创建一个直接从文件中获取ConfigMap 或创建 ConfigMap表示文件:

Yes you can do this with a configmap. It is much the same as accessing an external application.properties file. First you can create a ConfigMap directly from the file or create a ConfigMap representing the file:

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

然后在您的kubernetes部署中,创建一个将其安装到在用于外部配置的目录下的Pod :

Then in your kubernetes Deployment you create a Volume for the ConfigMap and mount 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

这些摘录来自从ConfigMap 作为application.properties文件,因此它们使用spring boot 在yaml中将其设置为挂载,因此您可以挂载文件以使用在kubernetes之外运行时已经使用的相对路径.

These snippets come from an example of mounting a Volume from ConfigMap for an application.properties file so they use the spring boot default external properties file path of /config. You can set that in the yaml for the mount so you could mount the file to use the same relative path that you are already using when running outside of kubernetes.

这篇关于Kubernetes上的Spring Boot应用程序如何使用外部message.properties文件来支持i18n和l10n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆