Spring Boot Application on Kubernetes How to use external message.properties file for supporting i18n and l10n?

倖福魔咒の 提交于 2020-06-25 21:45:19

问题


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

回答1:


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

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

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.



来源:https://stackoverflow.com/questions/53001506/spring-boot-application-on-kubernetes-how-to-use-external-message-properties-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!