24

I have been playing around with the Spring Cloud project on github located here: https://github.com/spring-cloud/spring-cloud-config

However I have been running into some problems getting it to read a local properties file instead of pulling the properties from github. It seems that spring is ignoring the local file even when I remove all the references to github. There is a similar question posted here: Spring-Cloud configuration server ignores configuration properties file

But I haven't seen any good answers yet. I'm wondering if anyone can point me to an example of this? I'd like to set my properties locally instead of using a git repo of any kind. I assume someone has encountered this before, and if there is an example of it somewhere, I'd really like to see it so that I can get moving in the right direction.

Community
  • 1
  • 1
user3270760
  • 1,444
  • 5
  • 23
  • 45

11 Answers11

25

All my code is here https://github.com/spencergibb/communityanswers/tree/so27131143

src/main/java/Application.java

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

src/main/resources/application.yml

spring:
  application:
     name: myconfigserver
  profiles:
     active: native

my:
  property: myvalue

src/main/resources/myapp.yml

my:
  otherprop: myotherval

To get the properties for an app named myapp, do the following.

curl http://localhost:8080/myapp/default

{
     "name": "default",
     "label": "master",
     "propertySources": [
          {
                "name": "applicationConfig: [classpath:/myapp.yml]",
                "source": {
                     "my.otherprop": "myotherval"
                }
          },
          {
                "name": "applicationConfig: [classpath:/application.yml]",
                "source": {
                     "spring.application.name": "myconfigserver",
                     "spring.profiles.active": "native",
                     "my.property": "myvalue"
                }
          }
     ]
}
Dimitri Hautot
  • 438
  • 5
  • 12
spencergibb
  • 24,471
  • 6
  • 69
  • 75
  • 1
    That is really cool. It works great! Thank you! My next question is: to get a specific value, could you specify it in either application.yml or some properties file like this: `foo=bar` and then get the value of `foo` using some curl command? Like `curl http://localhost:8080/myapp/foo` ? – user3270760 Dec 01 '14 at 12:53
  • 3
    Currently individual values aren't available via the http api. If you are using `spring-cloud-config-client` in your Spring application Environment. The values are available as YAML or Java Properties documents via `http://localhost:8080/myapp-default.yml` and `http://localhost:8080/myapp-default.properties` respectively. – spencergibb Dec 01 '14 at 16:52
  • 2
    Currently, with Spring Boot 2.x you will not get application.properties in the response. As per documentation of Spring Cloud Config: "The default value of the searchLocations is identical to a local Spring Boot application (...) This does not expose the application.properties from the server to all clients, because any property sources present in the server are removed before being sent to the client." – BartoszMiller Nov 20 '18 at 21:53
13

I am able to read configuration for apple-service(Test Micro Service) using Spring config server.

Example application.yml of spring config application

spring:
    profiles:
        active: native
    cloud:
        config:
            server:
                native:
                    searchLocations: classpath:config/
server:
  port: 8888


endpoints:
    restart:
      enabled: true

Put your .properties or .yml files inside src\main\resources\config folder. Make sure name of this files should match spring.application.name of your micro service.

For example if spring.application.name=apple-service then property file should be apple-service.properties in src\main\resources\config folder.

Example bootstrap.yml of apple-service:

spring:
  application:
    name: apple-service

cloud:
  config:
    uri: http://localhost:8888
nolt2232
  • 2,594
  • 1
  • 22
  • 33
rish1690
  • 281
  • 1
  • 3
  • 13
4

Here is what I did:

following https://medium.com/@danismaz.furkan/spring-cloud-config-with-file-system-backend-c18ae16b7ad5

1) !!dont forget your VM options!!:

-Dspring.profiles.active=native

(in Netbeans : configserver->project->properties>Run->VM options

2) Either in application.properties

#spring.cloud.config.server.native.searchLocations=file:///C:/tmp/config

spring.cloud.config.server.native.searchLocations=classpath:/config

or applications.yml

spring:

    cloud:

         config:

            server:

                native:

                    search-locations  : file:///C:/tmp/config
                    #search-locations : classpath:/config

Notes:

n1: search-locations and searchLocations both work

n2: file:/// => hard file path

Like

c:/temp/config/
           app-admin.yml
           app-admin-develop.yml
           .... 

n3: for your profile config files

classpath:/

Like

Other sources/src/main/resources/config/app-admin.yml

n4: I couldnt made file paths work without setting the vm options. Dont forget! Just setting spring.profiles.active=native in your application config does not do the trick for me

n5: example http queries

http://localhost:8998/app-admin/default/yml

gives app-admin.yml

http://localhost:8998/app-admin/develop/yml

gives app-admin-develop.yml

Roland Roos
  • 1,003
  • 10
  • 4
3

Using spring.profiles.active=native is that what Spring documentation seems to suggest, but I couldn't get it to work either. My application.properties file is

server.port=8888
spring.cloud.config.profiles=native 

but the response from the URL

http://localhost:8888/config-server/env

is

{"name":"env","label":"master","propertySources":[{"name":"https://github.com/spring-cloud-samples/config-repo/application.yml","source":{"info.url":"https://github.com/spring-cloud-samples","info.description":"Spring Cloud Samples"}}]}

which indicates that native profile was ignored and the server still considering github as property source.

A small additional problem I encountered is the config service default port. According to the Sprin Cloud Config documentation it should be 8888. If I remove server.port=8888 from my application.properties the config server starts on port 8080 which is default Spring Boot port, but not the one config server should use.

MrkK
  • 873
  • 3
  • 12
  • 22
  • 1
    Did you find solution to this ? The port and native profile thing. – Kumar Sambhav May 19 '15 at 07:29
  • you can put spring.profiles.active=native to bootstrap.properties and put it to src/main/resources or when you start your spring boot you can do it like this: mvn spring-boot:run -Drun.profiles=native now give it a try... – Sal Prima Nov 01 '17 at 06:31
  • Hi it's working fine , but while injecting using @Value("${info.description}") in client application, it's not working, getting error as Could not resolve placeholder 'message' in value "${info.description}" . I am not using using file system . – Rajanikanta Pradhan Oct 06 '18 at 19:14
3

I had the same problem when running the Configuration Server in Mac OS environment. This did not happen in Linux or Windows.

I had the native property set in the bootstrap.yml file like this:

spring:
  profiles:
    active: native

Finally the way it worked for me on the mac was to pass the active profile to the jar file, like this.

java -jar config-server.jar --spring.profiles.active=native

I still don't know why it behaves differently in Mac OS.

Oreste
  • 436
  • 1
  • 6
  • 16
3

The config server will read the local properties files if the application.properties of config server contains:

spring.profiles.active=native
**spring.cloud.config.server.native.searchLocations=file:/source/tmp**

at /source/tmp directory, you store the local property file for client, for example:

http://localhost:8888/a-bootiful-client/default

you will get:

{"name":"a-bootiful-client","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[{"name":"file:/source/tmp/a-bootiful-client.properties","source":{"message":"Kim"}}]}
SiHa
  • 7,830
  • 13
  • 34
  • 43
Kim Huang
  • 53
  • 1
  • 5
3

I've had this issue on Mac when there is a space in a path to a file:

spring.cloud.config.server.native.search-locations=file:///Users/.../Development/Folder with a space /prj

Also pay attention that there are 3 slashes before Users:

spring.cloud.config.server.native.search-locations=file:///Users/...

or I use:

spring.cloud.config.server.native.search-locations=file://${user.home}/Desktop

The native property is:

spring.profiles.active=native
Kirill Ch
  • 5,496
  • 4
  • 44
  • 65
1

I was able to get it work with local repo and bootstrap configuration:

java -jar spring-cloud-config-server-1.0.0.M3-exec.jar --spring.config.name=bootstrap

The bootstrap.yml file is placed in ./config/ folder.

server:
  port: 8080
spring:
  config:
    name: cfg_server
  cloud:
    config:
      server:
       git:
        uri: /home/us/config_repo
        searchPaths: pmsvc,shpsvc
Genadyk
  • 23
  • 5
1

I had the same issue on my local machine, but it works fine on my remote server.

@Oreste's answer does work for me. So I checked the error log again, And I found

 2018-06-25 16:09:49.789  INFO 4397 --- [           main] t.p.a.s.api.user.UserServiceApplication  : The following profiles are active:  someProfileISetBefore

So, the root reason for my case is I set an environment variable before, but I forget to remove it. And it overrides the application properties files config.

Hope you guys won't make the silly mistake like me. Fixed by:

 unset spring_profiles_active 
EdwinSZ
  • 11
  • 1
0

I added following two properties and it stared working:

spring.cloud.config.server.git.default-label=main

spring.cloud.config.server.git.try-master-branch=true

Sachin Ambalkar
  • 353
  • 2
  • 7
-1

I am using spring boot version 2.6.0-SNAPSHOT

If you are trying to use a local repo in a .properties file, then include these in your application.properties file of the server

server.port=8888
spring.profiles.active=git
spring.cloud.config.server.git.uri=file:/link-to-local-git-repo

You can get the link to the local git repo by typing pwd(ubuntu) on the terminal while in the git repo directory.

Emphasis: be sure the value of spring.profiles.active is git not native

Then type

http://localhost:8888/<name-of-file>/default

In the browser tab

Babi B
  • 183
  • 1
  • 2
  • 10