简体   繁体   中英

Spring Cloud Configuration Server not working with local properties file

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.

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"
                }
          }
     ]
}

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

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.

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.

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"}}]}

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

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 

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM