Spring-cloud-config: How to use config server to serve resource bundles to micro services.

Created on 25 Apr 2017  Â·  20Comments  Â·  Source: spring-cloud/spring-cloud-config

We are using Spring Cloud config server in our project which has multiple micro services. With config server in place, we are now storing all properties for all the micro services in a central git repository. Each micro service is able to pull his own properties file based on application name and profiles.
We are trying to store our resource bundles in the central repository and have config-server serve these resource bundles to micro services. Our resource bundles include properties files like messages_en.properties, messages_es.properties, messages_cn.properties, etc. We are able to load an resource bundle to config server using profile but it is acting as properties file.
We are using resource bundle in velocity template for GUI. Like

                <div class="top-content">
                    <div class="intro-box">
                        <h1 class="font-light">#springMessageText("welcome.text2","Welcome to <br/><strong>Our Website!")</strong>

Could someone suggest how they have done it ? or any link to use config server for loading n using resource bundle?

question waiting for feedback

Most helpful comment

The config server can serve files as plain text
http://cloud.spring.io/spring-cloud-static/Dalston.RELEASE/#_serving_plain_text

You should be able to do something like this

   @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:messages", configserverUrl + "/myapp/default/master/messages");
        return messageSource;
    }

All 20 comments

I am not clear on how you intend on then serving these resource bundles to the clients. Can you elaborate?

basically we want spring config server to read resource bundle(messages.properties) from git & then we can load it as resource bundle and use in our velocity template.

Till now we were keeping our resources files messages_en.properties & messages_es.properties in our project under “src\main\resouces” & loading these using following code.

   @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        return messageSource;
    }

Then these messages are being referred in our vm templates like

<p>#springMessageText("flow.text3","Please ensure that :")</p>
<ul>
             <li>
             <span href="" class="unpacked-icon">1. #springMessageText("flow.unpacked","Unpacked")</span>
                <img src="../Includes/images/welcome-unpacked.jpg"/>
</li>
           <li>
             <span href="" class="connected-icon">2. #springMessageText("flow.connected","Connected")</span>
                    <img src="../Includes/images/welcome-connected.jpg"/>
           </li>
</ul>

Now instead of keeping in our project we want messages_en.properties to be moved to git hub & we can read from there as resource bundle using config server OR without config server.

The config server can serve files as plain text
http://cloud.spring.io/spring-cloud-static/Dalston.RELEASE/#_serving_plain_text

You should be able to do something like this

   @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:messages", configserverUrl + "/myapp/default/master/messages");
        return messageSource;
    }

@ryanjbaxter i tried the way you have explained but still no luck. Giving more details on what i have tried, in case you can suggest what is wrong in my code or the way i am trying.

Our Config Server – application.yml

spring: 
  cloud: 
    config: 
      server: 
        git: 
          searchPaths: "*"
          uri: https://git server URL/config-test.git

Then in our application bootstrap.yml

spring:
  application:
    name: myApp
  profiles:
    active: qatest,messages_es

Then i tried accessing file, as you explained
configserverUrl + "/myapp/default/master/messages")

http:// configserverUrl/myapp/default/master/

{
name: " myApp",
profiles: [
"default"
],
label: "master",
propertySources: [
{
name: "https://git server URL/config-test.git/application.properties",
source: {
Spring.cloud.config.failFast: "true ",
eureka.server.max-time-for-replication: "6000",

http:// configserverUrl/myapp/default/master/messages
I get 404.

Then I tried
http:// configserverUrl/myapp/ /messages_es/master/

{
name: " myApp",
profiles: [
"messages_es"
],
label: "master",
propertySources: [
{
name: "https://git server URL/config-test.git/myApp/myApp-messages_es.properties",
source: {
Spring.cloud.config.failFast: "true ",
eureka.server.max-time-for-replication: "6000",

http:// configserverUrl/myapp/ /messages_es/master/messages
I again get 404

Could you please suggest on this?

The correct URL would be http://configserverUrl/myapp/default/master/messages_es.properties. However you can leave off the _es.properties when using ReloadableResourceBundleMessageSource since it will add the correct locale automatically.

Thanks Ryan for reply, when I tried opening below URLS in browser, these did not work. Both returned 404 error.
http:// configserverUrl/myapp/default/master/messages_es.properties
http:// configserverUrl/myapp/messages_es/master/messages_es.properties

But when i ran below one, i get following
http:// configserverUrl/myapp/messages_es/master/

{
name: " myApp",
profiles: [
"messages_es"
],
label: "master",
propertySources: [
{
name: "https:// git server URL/config-test.git/myApp/myApp-messages_es.properties",
source: {
Spring.cloud.config.failFast: "true ",
eureka.server.max-time-for-replication: "6000",

For http:// configserverUrl/myapp/default/master/ i can see only application.properties

{
name: " myApp",
profiles: [
"default"
],
label: "master",
propertySources: [
{
name: "https://git server URL/config-test.git/application.properties",
source: {
Spring.cloud.config.failFast: "true ",
eureka.server.max-time-for-replication: "6000",

And when i am running through app using below code, i get following error

@Bean public ReloadableResourceBundleMessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasenames("classpath:messages","http://username:password@sconfig-server-URL/myApp/default/master/messages"); return messageSource; }

org.springframework.context.support.ReloadableResourceBundleMessageSource.refreshProperties - Could not parse properties file [.properties] ERROR_DETAILS: java.io.IOException: Server returned HTTP response code: 500 for URL: http://username:password@sconfig-server-URL/myApp/default/master/messages/.properties

The URLs will depend on the structure of your git repository. Is your git repo public? Could you provide a sample the reproduces what you are seeing?

our git repository is not public, but our git config structure is somethign like this
https://gitbaseURL /config-test/tree/master/myApp
inside this we have all config files like
myApp-messages_en.properties
myApp-messages_es.properties
myApp-qatest.properties

Let me know if you need any other info

myApp is a folder in the git repo?

yes it is in git repo & then inside myApp we have all properties file

Then the url should look like http://<configserverUrl>/myapp/default/master/myApp/myApp-messages.

i tried this URL too, in app & browser too but getting 404 page. i tried with both profile default & messages_es.
Just fyi, we are using messages_es profile for loading message_es to config server.

That url won't work in a browser. It would only work with a ReloadableResourceBundleMessageSource.

A url like http://<configserverUrl>/myapp/default/master/myApp/myApp-messages_en.properties should work.

At this point, I don't know what else we can do to help you without an actual sample application to work on.

We tried to expose resource files(messages_es.properties, messages_en.properties) from temporary application & read it through URL in our main application. we were able to do it successfully & resources are working fine. Here is sample code we used to access it.

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

        messageSource.setBasenames("classpath:messages",  "URL:http://10.160.200.46:9000/user/messages",)
        return messageSource;
    }

But we want to read it through config server & we are not able to make proper config server URL to pass it to messageSource.setBasenames . we have tried following ones

      messageSource.setBasenames("classpath:messages"
                ,"http://base-config-servername/myApp/default/master/myApp/myApp-messages"
            ,"http://base-config-servername/myApp/default/master/myApp/messages"
                ,"http://base-config-servername/myApp/default/master/myApp/myApp-messages_es.properties"
)

Could you please suggest any other URL we can try, please let us know if you need any other specific information to make any other suggestion for solution.

As @spencergibb said we have done all we can at this point. For us to help you any further provide a sample application that reproduces the problem you are experiencing.

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spencergibb This issue is still exist . there is n o proper solution to externalize message propeties
I have tried all the above options . it is good help from you If you can help us by creating a simple app which will internationalized message and property will read from git repo . either by config server or not

@Alok86JavaPro we need a sample app to reproduce the problem. Please provide one if you think this problem exists.

@ryanjbaxter , i am trying to integrate localization with cloud config but getting some issue, I am sharing the sample code which i was trying, Can you please help me out.

GIT- URL :: https://github.com/vimalsingh56/springboot-config.git

localization.zip
my-config-server.zip

Please let me know if am doing any mistake. Thanks in advanced.

@vimalsingh56 please open a separate issue with a description of the problem you are facing and include the samples.

Was this page helpful?
0 / 5 - 0 ratings