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

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

Related

How to stop Spring Cloud Config Cloning a new version of a Repo every time the Config Server is started

Upon running a Spring Cloud Config application with a Git backend a clone is performed into the default location of c:/user/AppData/Temp/local/<config-repo-1>. Cloning can take a long time for larger applications (currently one of my teams takes ~15 min).
When the Config Server is stopped and restarted again instead of using the same local repo another clone is performed, c:/user/AppData/Temp/local/<config-repo-2>.
Is there a way to change this behavior so that restarting the Config Server does not do a new clone but instead just does a fetch for the existing clone.
Thanks in advance for any assistance provided.
spring:
config:
server:
git:
clone-on-start: false
I saw this option somewhere, but couldn't figure out where I got it. Unfortunately, the official spring documentation does not refer to this option. This will not clone the rep when starting.
The other Option from the Spring Documentation is:
spring:
config:
server:
git:
force-pull: false
As documented:
... there is a force-pull property that makes Spring Cloud Config Server force pull from the remote repository if the local copy is dirty

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

How to prevent Spring Config to reset my local git repository to origin/master

I am using Spring Config to share in a git server the configuration for some Spring Boot microservices.
It works great but when I am traveling I have to work offline sometimes.
I have configured Spring Config microservice local profile to get the config from my local git, (file:) and not to be HTTP git server, so I can change config and test without needing access to main git server.
The problem is that as I am not able to do a "git push" to push the change to main repository, Spring Confgig notes it and shows this message:
The local repository is dirty or ahead of origin. Resetting it to origin/master.
and resets it deleting my last local commit with last config changes.
How can I make Spring Config just to get the last committed configuration in my local git ignoring if it is pushed or not to the main server?
I had a similar problem.
(the difference is that I am not using Spring Config microservice, but runnging spring configuration server as a stand alone application)
But this should work for you as well:
Instead of launching local configuration server with a local git repository (file:) I have launched it with native spring profile.
spring:
profiles:
active: native
cloud:
config:
server:
native:
searchLocations: file:///path/to/local/git/repository
So now content of local repository is served and no reset is done on access
Actually going through spring-cloud-config source code you will see:
if (!isClean(git, label)) {
this.logger.warn(
"The local repository is dirty or ahead of origin. Resetting"
+ " it to origin/" + label + ".");
resetHard(git, label, LOCAL_BRANCH_REF_PREFIX + label);
}
Solution
So it will always try to do this and the only work around will be similar to this answer:
How spring cloud config use local property override remote property
You will need to commit:
spring:
cloud:
config:
allowOverride: true
overrideNone: false
To the configuration properties you are using. e.g.:local/my-app.properties. If you do have a remote repo for storing all the properties, make sure it is merged to master.
Then you could change any application property as you wish in application.yml/application.properties in your Spring Boot app. It won't get overriden by the remote properties file.
Alternative
Alternatively, you could just remove the config file for the environment you are working on as dev environment. In my example above, you could just remove local/my-app.properties from remote repo and commit master. So that it will never bother overriding local application properties from there because no properties files exist for cloud-config.
Please comment if anything is unclear so that I could improve the instruction.
You can use an identical directory as not only a file system backend and also a git repository for properties files. You should make 2 bootstrap.properties files for config server. One is for the file backend and the other is for the git repository. For example, I want to use a file system backend for develop and a git repository for production. I made bootstarp-dev.properties and bootstrap-prd.properties.
## bootstarp-dev.properties
server.port=8887 // If you want to launch both a config server for dev and a config server for prd, you must assign different ports.
spring.cloud.config.server.native.searchLocations=file://PATH_OF_YOUR_DIRECTORY
## bootstarp-prd.properties
server.port=8888
spring.cloud.config.server.git.uri=YOUR_GIT_URL
If you launch your config server with -Dspring.profiles.active=dev,native parameter, the config sever access your properties files as local files.
If you launch your config server with -Dspring.profiles.active=prd parameter, the config sever access your properties files as git files.
Done.
How can I make Spring Config just to get the last committed configuration in my local git ignoring if it is pushed or not to the main server?
This is what --assume-unchaged is for.
--assume-unchaged
Raise the --assume-unchaged flag on this file so it will stop tracking changes on this file
--[no-]assume-unchanged
When this flag is specified, the object names recorded for the paths are not updated.
Instead, this option sets/unsets the assume unchanged bit for the paths.
When the assume unchanged bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).
Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

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.

Config server not able to find the remote application file

I'm having issue with referring the application/config file on a config server.
I already have a GAIA DEV Pool ready.
This is what I have done so far -
Created an application.yml file with required config property
hello-service:
message: Hello World From Spring Cloud Config Server
Checked it in git repo - feature/cs-hello branch.
Logged in to dev pool cf login -a [dev-pool-url]
Created the config server :
cf create-service -c "{\"git\": { \"label\": \"feature/cs-hello\", \"uri\": \""[GIT HTTPS URL]\", \"username\": \"[USERNAME]\", \"password\": \"[PASSWORD]\" } }" p-config-server standard config-server
Now, my referring [https://docs.pivotal.io/spring-cloud-services/1-4/common/config-server/configuring-with-git.html] - since my config file is not on the master branch, I used label.
Somehow my code is not finding the application file on config server.
Anyone here who can point me a direction?
Once you are done with config server creation, you need to bind that service with your application and you can need to provide following information in bootstrap.yml
spring:
application:
name: appName
There is a way to check either your config server is connecting to repository or not. Go to
PCF manager -->Services--> Config Server--> Manage
and then you should be able to see
Config server is online

Resources