Spring Config Server properties ordering when directories - spring

We are running Spring Cloud Config Server v1.3.1.
We have Bitbucket hosting Git. The Git environment has a configuration folder where we hold our files. There are a number of subdirectories under the configuration folder. eg
environment/configuration
application-dev.yml
my-service.yml
my-service-ci.yml
my-service.dev.yml
...
environment/configuration/datasources
application-ci.yml
application-dev.yml
...
In the Spring Config Server the application and config server are configured as below:
spring:
application:
name: "#project.name#"
cloud:
config:
server:
git:
uri: https://xxxxx/scm/dep/environment
basedir: ${baseDirectory}/work
searchPaths: configuration, configuration/*
When loading the my-service Spring Boot app with dev profile I would expect the app specific config files (my-service) to load first. ie
environment/configuration/my-service-dev.yml
environment/configuration/my-service.yml
environment/configuration/configuration/application-dev.yml
environment/configuration/application-dev.yml
In reality it loads the folders in reverse order so configuration/* comes first with its subdirectories loaded in alphabetical order. Next comes the files
sitting directly under configuration. This gives below order
environment/configuration/application-dev.yml
environment/configuration/configuration/application-dev.yml
environment/configuration/my-service-dev.yml
environment/configuration/my-service.yml
Spring Boot loads these as a Map containing a list of PropertySources. It iterates from the start and returns the first match. In this case a property in application-dev.yml
would trump the same property in my-service-dev.yml.
Is this intended behaviour or is there a bug when directories are involved?

Related

Should I use only native profile if I want to use local config files in spring boot?

I'm setting local development environment to test using spring boot ver 2.7.1, and I want to use local file hierarchy as config server.
So, I use spring.cloud.config.server.native.searchLocations: ~~~.
I want to boot this project using profile named something other except 'native' (application-native.yml). But it is failed to load the server.
My Question:
When I want to use local files as config files, is it only possible to use 'native' profile?
I tried :
spring:
profiles:
active: local
cloud:
config:
server:
native:
searchLocations:
~~~~
still failed.

spring config server unable to find local git repository when directory name is different

I have a config store repository http://gitea-server:3000/demoadmin/demo-school-config-store.git.
That repository is cloned and synced in a directory config-store. Please note git repository name is different than directory name
And my config server application is like this:
spring:
application.name: config-server
output.ansi.enabled: ALWAYS
cloud.config.server.git:
search-paths: '{application}'
uri: file://${user.home}/source/demo-school-config-store
Now when I try to check my config server by fetching config with following url:
http://localhost:8312/teacher-service/local
It gives me following error:
Caused by: java.lang.IllegalStateException: No directory at file:///Users/mafzal/source/demo-school-config-store
After doing lot of debugging I found that when I rename directory to demo-school-config-store, everything works well.
Is there a way to make config server run with local config repository having different directory name than it's repository name?
https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html :
2.1.2 Version Control Backend Filesystem Use
With VCS-based backends (git, svn), files are checked out or cloned to the local filesystem. By default, they are put in the system temporary directory with a prefix of config-repo-. On linux, for example, it could be /tmp/config-repo-<randomid>. Some operating systems routinely clean out temporary directories. This can lead to unexpected behavior, such as missing properties.
To avoid this problem, change the directory that Config Server uses by setting spring.cloud.config.server.git.basedir or spring.cloud.config.server.svn.basedir to a directory that does not reside in the system temp structure.
As for the searchPaths :
Every repository can also optionally store config files in sub-directories, and patterns to search for those directories can be specified as searchPaths.
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
searchPaths: foo,bar*
In the preceding example, the server searches for config files in the top level and in the foo/ sub-directory and also any sub-directory whose name begins with bar.
UPDATE:
I missed the part that the config server git backend is configured to get the configuration from the local repository, not the remote one you specified. If you want it to retrieve configuration from the config-store directory, then just clone your repo to ${user.home}/source/config-store instead (or rename the existing ${user.home}/source/demo-school-config-store directory) and use that in the config server configuration:
spring:
cloud.config.server.git:
uri: file://${user.home}/source/config-store

How to configure Spring Cloud Config Server to use cloned configuration files

My application.properties files of my Cloud Config Server looks like this.
config.source=Local Cloud Server #just to check where config come from
server.port=8012
encrypt.key=xxxxxxxxxxxxxx
spring.profiles.active=staging
spring.application.name=my-config-server
# Git Backend
spring.cloud.config.server.git.username=MY_USERNAME
spring.cloud.config.server.git.password=ghp_MY-DEV-ACCESS-TOKEN
spring.cloud.config.server.git.uri=https://github.com/my-username/app-config
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.basedir=file://${user.dir}/cloned_configurations
On startup a new folder "cloned_configuration" is created inside of Cloud Config Server folder. I see all of my propertie files cloned form github with correct values.
But for some reason none of these are used. For e.g config.source must have the value "GitHub" because the cloned application.repository has an entry
config.source=GitHub
But on application start I see "Local Cloud Server". All other settings are also not used from cloned properties.
With Postman I can receive all Configs without any issues. But none of theme are used by my Config Server or any of my other webservices.All webservices and the config server using their own application.properties file.
What I do wrong?
You probably need to move your config server properties into your bootstrap.properties file instead of application.properties.
spring.cloud.config.server.git.username=MY_USERNAME
spring.cloud.config.server.git.password=ghp_MY-DEV-ACCESS-TOKEN
spring.cloud.config.server.git.uri=https://github.com/my-username/app-config
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.basedir=file://${user.dir}/cloned_configurations
https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html

Spring cloud config server git-uri local file resolves to master

In my app's bootstrap.yml file i have placed this configurations
spring:
application:
name: arun-test
cloud:
config:
server:
bootstrap: ${LOCAL_CLOUD_CONFIG_BOOTSTRAP:false}
git:
uri: file:${LOCAL_CLOUD_CONFIG_DIR}
I clone my properties folder from Github and then before starting the app, i turn my spring-boot app in to config server with below commands
export LOCAL_CLOUD_CONFIG_BOOTSTRAP=true
export LOCAL_CLOUD_CONFIG_DIR=/Users/arun/arunLabs/app-properties
./gradlew bootrun // to start
This works exactly fine.. But when i create a new branch and change the properties there to test something, the app always resolves to master-branch only and then it is still using my old configurations. Not going in to my branch
Not only that, it also replaces my file in the new branch with the one that is in master.
How to test it against my branch ?
Actually what you're looking for is a client side request. Cloud config will serve all branches in your repository.
You can read in the documentation.
You can either set the label value on your spring cloud Config Client
spring.cloud.config.label=newBranch
Or you can request directly to cloud config service
localhost:8080/{application}/{profile}/newBranch

Why Spring Boot Cloud Config Server properties not picked from GIT

I have the following issue. Just with the basic Spring Boot Config Server, I would like to read property file from my Github (i tried also Gitlab). Everything works localy, the property file is nicely loaded from Github, but after deploying application on any other environment, for example on Heroku or on dedicated Linux server, the property file is not loaded. I am checking http://localhost:8101/employee/service1
Loading the property file from local files (classpath:config/local) works very well both locally and on other environments. What I am doing wrong? I have created simple project on Github (https://github.com/troger19/config-server), if anybody can check. Also app is deployed on Heroku: https://protected-savannah-48323.herokuapp.com/employee/service1
The application.yml looks like this
server:
port: 8101
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/troger19/config-server
search-paths: src\main\resources\config\dev
And the employee-service1.yml is in resources/config/dev
I havent find anything in the logs so far. Thank you for any suggestion.
Please change use "/" for linux environment as "\" will only work on window environment.
Please change your search-paths: src\main\resources\config\dev to /src/main/resources/configdev
Good idea to use different-2 profile in yml file for different environment so such can be avoided.

Resources