Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
}
like image 835
Josh Avatar asked Oct 26 '18 04:10

Josh


People also ask

How do I use message properties in Spring Boot?

Adding Message Properties Into The Spring Boot Project In order to do that, just create two files in src/main/resources/messages folder with naming as below, api_error_messages. properties. api_response_messages.

Where can the message properties be configured to simplify the internationalization in Spring Boot?

By default, a Spring Boot application will look for message files containing internationalization keys and values in the src/main/resources folder. The file for the default locale will have the name messages. properties, and files for each locale will be named messages_XX. properties, where XX is the locale code.

How do you override a Spring Boot property in Kubernetes?

To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.


1 Answers

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.

like image 152
Ryan Dawson Avatar answered Sep 25 '22 09:09

Ryan Dawson